Play Music in C++

In this tutorial, we will learn how to play .wav audio files using the PlaySound function in C++.

Steps to follow for implementation

  1. Open Dev C++ application. ( I’m using Dec C++, but you may use any IDE you like. The options will be almost the same for other IDEs too. )
  2. Click on File – > New -> Project.
  3. Select “Empty Project”.
  4. Type project name and “click Ok”.
  5. Change the file extension to “.cpp” (if it is not “.cpp”) and click on “Save”.
  6. Right-click on the project name and select “Project Options”.
  7. Go to the “Parameters” tab.
  8. Type “-lwinmm” under the “linker” section and click Ok.
  9. Type the below code and save it.
  10. Run the code and enjoy the music 🙂

(*Note: Audio file must be an original .wav file and it must be in the folder where your program is saved).

Program to Play Music in C++ using PlaySound function

#include<iostream>
#include<windows.h>		//used for PlaySound function
#include<fstream>		//used for file handling

using namespace std;

int main()
{
  char audiofile_char[50];
  string audiofile_str;
  fstream fp;
  
  //Enter name of a 'wav' audio file with .wav extension and make sure it is present in the folder where your program is saved.
  cout<<"Enter the name of an audio file: ";
  cin>>audiofile_char;
  
  fp.open(audiofile_char, ios::in);
    if (fp != NULL)
    {
    	/* open function doesn't work with a variable of string type and PlaySound function doesn't work with char array hence first we used char array and here we are converting it to a string. so that we can use it in PlaySound function */ 
    	string audiofile_str =audiofile_char;	
    	
  /* PlaySound function is used to play a sound specified by the given file name. */
  /* SND_SYNC : play sound synchronously */
  /* .c_str() function is used to return a pointer to an array that contains a null-terminated sequence of characters representing the current value of the string object. */
  PlaySound(audiofile_str.c_str(), NULL, SND_SYNC);
  }
  else
  {
    cout<<"\nAudio file couldn't run..!!";
    cout<<"\nCheck the following things: \n 1. Your file is an original .wav audio file. \n 2. You have mentioned '.wav' extension in the name of file. \n 3. It is present in the folder where your program is saved.";
  }	
  
  fp.close();
  
  return 0;
}

Output (On entering valid input):

Enter the name of an audio file: sample.wav

and you will hear the audio of your mentioned .wav file.

 

Output (On entering invalid input):

Enter name of audio file: sample

Audio file couldn't run..!!
Check the following things:
1. Your file is an original .wav audio file.
2. You have mentioned '.wav' extension in the name of file.
3. It is present in the folder where your program is saved.

Time Complexity

It has linear time complexity. i.e. O(1).




You may also read:

  1. Find the difference between two dates in C++
  2. How to create a CSV file in C++


Leave a Reply

Your email address will not be published. Required fields are marked *