c++playsound

#pragma comment( lib, "Winmm.lib" ) Not working


I am trying to use PlaySound and I put

#include <windows.h>
#include <mmsystem.h>
#pragma comment( lib, "Winmm.lib" )
using namespace std;

int main()
{

PlaySound(L"C:\\Users\\iD Student\\Downloads\\HarryPotter.mp3", 0, SND_FILENAME);

}

and instead of playing the sound I wanted it to, it played some default Windows sound.


Solution

  • PlaySound does not support .mp3 files. It only supports .wav files.

    This is the simple code for playing sounds:

    #include <windows.h>
    #include <mmsystem.h>
    #pragma comment( lib, "Winmm.lib" )
    using namespace std;
    
    int main()
    {
    //Replace C:\\Users\\iD Student\\Downloads\\HarryPotter.wav with the location of your file
    PlaySound(L"C:\\Users\\iD Student\\Downloads\\HarryPotter.wav", 0, SND_FILENAME);
    
    }