playsound function

1

I am aa program a game for school work in C ++, and I have a game that during its execution starts a song, then when I click on the space key to fire the music stops and you only hear the gun shot. / p>

I wanted to be able to play both the background and the game.

Here is my code:

private: System::Void GameForm_Load(System::Object^  sender, System::EventArgs^  e) 
{
    PlaySound(TEXT("../Musicas/Game.wav"), NULL, SND_ASYNC | SND_LOOP);
}

This song starts when I open the page. And this when I load in space:

private: System::Void GameForm_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) 
{
    switch (e->KeyCode)
    {
    case Keys::A:
        Nave->Left -= 20;
        break;
    case Keys::D:
        Nave->Left += 20;
        break;
    case Keys::W:
        Nave->Top -= 20;
        break;
    case Keys::S:
        Nave->Top += 20;
        break;
    case Keys::Space://Carrego no espaço e o som e executado
        PlaySound(TEXT("../Musicas/Laser.wav"), NULL, SND_ASYNC);
        break;
    }
}

The background music for when I run the space. I wanted both songs at the same time.

    
asked by anonymous 29.10.2016 / 16:51

2 answers

2

Instead of using flag SND_ASYNC , use SND_NOSTOP .

According to documentation : p>

  

SND_NOSTOP

     

The specified sound event will yield to another sound event that is   already playing in the same process. [...].

     

If this flag is not specified, PlaySound attempts   to stop any sound that is currently playing in the same process .   Sounds played in other processes are not affected.

Use this:

PlaySound(TEXT("../Musicas/Laser.wav"), NULL, SND_NOSTOP);
    
29.10.2016 / 17:02
1

The answer to your question is extremely simple: you have to create two constructors for each of the sound layers you want to play simultaneously, and define the context in which they are to be instantiated. Only in this way can you run two objects simultaneously, so that when the second object is instantiated, an override of the first one is not made. To give an answer with more specific examples, I would need to access the project, but at the beginning it is this, since I myself went through an identical situation to instantiate two voices of an algorithm to generate midi procedurally, in c ++, going against the aesthetics of steve reich's music, starting from code that had been implemented in MaxMSP. good luck

    
29.10.2016 / 17:03