2

I'm trying to make a specific .wav file play that I have added as a resource in my project. And when I run the program no sound is played.

I've tried doing:

PlaySound(TEXT("SystemStart"), NULL, SND_ALIAS);

Which works and plays correctly.

Then I tried to define the location of the wav file on my computer with:

PlaySound(TEXT("D:/TestSounds/sound1.wav"), NULL, SND_ASYNC);

Which also works correctly!

But the problem starts when I try to run the file from a resource

I've tried doing:

PlaySound(MAKEINTRESOURCE(IDR_WAVE1), GetModuleHandle(NULL), SND_RESOURCE);

And also tried using

BOOL PlayResource(LPCWSTR lpName)
{
    BOOL bRtn;
    HANDLE hRes;

    // Find the WAVE resource. 
    HRSRC hResInfo = FindResource(NULL, lpName, TEXT("WAVE"));
    if (hResInfo == NULL)
        return FALSE;

    // Load the WAVE resource. 
    hRes = LoadResource(NULL, hResInfo);
    if (hRes == NULL)
        return FALSE;


    // Lock the WAVE resource and play it. 

    const char* lpRes = (const char*)LockResource(hRes);
    if (lpRes != NULL) {
        bRtn = PlaySound((LPCUWSTR)lpRes, NULL, SND_MEMORY | SND_ASYNC);
        UnlockResource(hRes);
    }
    else
        bRtn = 0;

    // Free the WAVE resource and return success or failure. 

    FreeResource(hRes);
    return bRtn;
}

Any help is appreciated

MRav4
  • 21
  • 2
  • Show the line in .rc file where the WAV file is added to resources. – Igor Tandetnik Jun 06 '20 at 19:49
  • @IgorTandetnik IDR_WAVE1 WAVE "D:\\TestSounds\\sound1.wav" – MRav4 Jun 06 '20 at 19:57
  • 1
    Is `IDR_WAVE1` defined as a macro when .rc is compiled? See what happens if you do `PlaySound(TEXT("IDR_WAVE1"), GetModuleHandle(NULL), SND_RESOURCE);` (that is, pass `IDR_WAVE1` as a string rather than as int). – Igor Tandetnik Jun 06 '20 at 19:59
  • It's defined as #define IDR_WAVE1 101 in resource.h I also tried doing it as a as a string, but still no sound – MRav4 Jun 06 '20 at 20:08
  • In `PlayResource`, you are supposed to call `sndPlaySound`, not `PlaySound`. Though the one-liner `PlaySound` call should also work; I'm not sure why it doesn't. – Igor Tandetnik Jun 06 '20 at 20:12
  • @IgorTandetnik Tried switching PlaySound to sndPlaySound in PlayResource to `bRtn = sndPlaySound((LPCUWSTR)lpRes, SND_MEMORY);` But still no sound played – MRav4 Jun 06 '20 at 20:25
  • Do all the preceding calls, like `FindResource`, succeed? – Igor Tandetnik Jun 06 '20 at 20:26
  • When I check now, FindResource is NULL – MRav4 Jun 06 '20 at 20:50
  • Then you haven't added the resource to the executable after all; or added it under a name different from the one you think. – Igor Tandetnik Jun 06 '20 at 20:53

0 Answers0