-1

I'm trying to play the song in simple console executable. However after running the .exe the song doesn't play and the windows notification sound appears instead in loop. The resources are created properly (l am using Codeblocks so l can see them in /obj/debug/) so l suppose that's because the PlaySound() path that l set doesn't lead to the right directory.

How to set the path to resources in PlaySound() function? Thank you in advance

play/main.cpp:

#include <iostream>
#include <thread>
#include <windows.h>
#include <mmsystem.h>

void play() {
  PlaySound(TEXT("a18.wav"), NULL, SND_FILENAME|SND_LOOP|SND_ASYNC);
}

int main() {
  std::thread t(play);
  system("pause>>nul");
  t.join();

  return 0;
}

play/song.rc:

song WAVE "/rsrc/a18.wav"
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Ключ
  • 11
  • 5
  • 4
    The [documentation](https://learn.microsoft.com/en-us/previous-versions//dd743680(v=vs.85)) explains how to do this and provides an example. – Retired Ninja Dec 10 '22 at 18:34
  • @RetiredNinja l already tried `PlaySound(MAKEINTRESOURCE(IDR_WAVE1), GetModuleHandle(NULL), SND_RESOURCE);` but as you can expect it didn't work. Could you please explain it to me? – Ключ Dec 10 '22 at 18:57
  • 1
    How are you compiling this program? Are you sure the resources are being included in the executable correctly? Using Visual Studio you could open the executable file and view the resources. You could also use a program like ResourceHacker. http://www.angusj.com/resourcehacker/ I would change the flags to `SND_RESOURCE | SND_NODEFAULT` and check the return value from `PlaySound`. If it is false you'll probably get an error of 1812 - 1815 which you can look up here: https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--1700-3999- – Retired Ninja Dec 10 '22 at 19:21
  • @selbie maybe you need to refresh the page, the rc is in the question – Remy Lebeau Dec 10 '22 at 19:44
  • @RetiredNinja as l said l use Codeblocks + mingw compiler and l am sure that the resources were included correctly. Apologies for the stupid question but l didn't know that as Remy Lebeau said "`MAKEINTRESOURCE(IDR_WAVE1)` will not work with non-numeric IDs." – Ключ Dec 10 '22 at 20:06

1 Answers1

1

SND_FILENAME is the wrong flag to use when the audio is in a resource. You need to use the SND_RESOURCE flag instead.

The .rc you have shown is assigning a non-numeric ID song to the resource, you need to use that ID to play the resource. There is even an example of this in PlaySound's documentation: Playing WAVE Resources. In comments, you say you tried MAKEINTRESOURCE(IDR_WAVE1) as the resource ID to play, but MAKEINTRESOURCE() works only with numeric IDs.

Also, you are using the SND_ASYNC flag, which means PlaySound() will exit immediately, thus your thread will terminate immediately, making it useless. You don't really need the thread at all, unless you remove the SND_ASYNC flag.

Try this instead:

#include <windows.h>
#include <mmsystem.h>
#include <cstdlib>

int main() {
  PlaySound(TEXT("song"), GetModuleHandle(NULL), SND_RESOURCE|SND_LOOP|SND_ASYNC);
  std::system("pause>>nul");
  PlaySound(NULL, NULL, 0);

  return 0;
}

Alternatively:

#include <thread>
#include <atomic>
#include <cstdlib>
#include <windows.h>
#include <mmsystem.h>

std::atomic_bool stop(false);

void play() {
  while (!stop.load()) {
    PlaySound(TEXT("song"), GetModuleHandle(NULL), SND_RESOURCE);
  }
}

int main() {
  std::thread t(play);
  std::system("pause>>nul");
  stop.store(true);
  t.join();

  return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770