Monday, June 19, 2017

C++ program to play music and compile in GCC compiler

Here is the simplest program to play music in C++.

Here you have to do a setting if you are using and UI based IDE like code block or Dev C++. You have to go to setting and add  lwinmm in linker setting. lwinmm is a linker used while compilation of program containing sound.
with this program only wav music file format can be played. You can convert the music file to wav format online by vising this link -- http://media.io/

The code goes as below::

#include <windows.h>
#include <iostream>
#include <mmsystem.h>
using namespace std;
int main()
{
  system("clear");
  cout<<"Sound playing... enjoy....!!!"<<endl;
  PlaySound("C://music1.wav", NULL, SND_SYNC); //SND_FILENAME or SND_LOOP
  return 0;
}

If you are using gcc compiler and compiling using command line then your life will be easy. you need you write below command for compilation. 

Let say I have saved my program as sound.cpp. So command to compile will be-
$ g++ sound.cpp -lwinmm -o sound

After successful compilation to execute the file, write as below-
$ ./sound

No comments:

Post a Comment