0

i`m trying to implement a background music loop and do other sounds that wont cut it when I use the "PlaySound" function again.

    void CcpPongView::playBGsound(int num)
{
    //added in the linker to additional name : winmm.lib
    if(num)
        PlaySound(L"sound.wav", NULL, SND_LOOP | SND_ASYNC);
}

void CcpPongView::initGame()
{
    std::thread test (playBGsound, 1);
    test.join();
}

this is the second place I use Playsound

    void Ball::playSound()
{
    switch (rand()%2) {
    case 0:PlaySound(sounds[0], NULL, SND_FILENAME | SND_ASYNC);
        break;
    case 1:PlaySound(sounds[1], NULL, SND_FILENAME | SND_ASYNC);
        break;
}

im not sure where I need to place the thread or how to make a thread in mfc Single Document.. (the "num" was to test)

enter image description here

enter image description here if there is another way to do it i`m all ears thanks for the help every one :)

Cc4Dayz
  • 29
  • 4

1 Answers1

3

Your creation of the std::thread needs to be more like this instead:

std::thread test (&CcpPongView::playBGsound, this, 1);

Note that calling join() on the thread immediately after creating it is a waste of a thread. You may as well just call playBGSound() directly. If you want to create a thread and let it keep running after the std::thread is destroyed, call detach() instead of join(). Otherwise, don't destroy the std::thread until after the thread has stopped running (ie, store it in a member of your CcpPongView class), in which case you can then use join().

And, you can't play multiple sounds simultaneously with PlaySound() using the SND_ASYNC flag.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770