Search code examples
c++playsound

Let the C++ playSound run with the main function simultaneously


Here is my code:

    DemoAMain oMain;
    char buf[1024];
    PlaySound(TEXT("bgm.wav"), NULL, SND_FILENAME);
    sprintf( buf, "My Demonstration Program : Size %d x %d", BASE_SCREEN_WIDTH, BASE_SCREEN_HEIGHT );
    iResult = oMain.Initialise( buf, BASE_SCREEN_WIDTH, BASE_SCREEN_HEIGHT, "Cornerstone Regular.ttf", 24 );
    iResult = oMain.MainLoop();
    oMain.Deinitialise();
    return iResult;

There is a problem that only if the PlaySound has finished it's music, can the program continue. What should I do to let the PlaySound run with the following code simultaneously? Many thanks.


Solution

  • You can do it by adding SND_ASYNC flag to the third argument like this:

    PlaySound(TEXT("bgm.wav"), NULL, SND_FILENAME | SND_ASYNC);