Playsound function

Asked

Viewed 274 times

1

I am programming 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 only hear the shot of the gun.

I wish I could play both the background music and the game music.

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 click 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.

2 answers

2

Instead of using the flag SND_ASYNC, use SND_NOSTOP.

According to the documentation:

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 like this:

PlaySound(TEXT("../Musicas/Laser.wav"), NULL, SND_NOSTOP);
  • I tried that option and it didn’t work

  • @kingwarrior05 Have tried so PlaySound(..., NULL, SND_ASYNC | SND_NOSTOP)?

  • yes I have tried, when I replace I am unable to hear the shots

  • 2

    @kingwarrior05 Searching deeper, it seems that the PlaySound does not support multiple sounds at the same time. (See this discussion). You may need to use a more sophisticated API like Directsound or Openal, or else the Xaudio2. :/

1

The answer to your question is extremely simple: you have to create two constructors for each of the sound layers you want to reproduce simultaneously, and define the context in which they will be instantiated. Only in this way can two objects run simultaneously, so that when the second object is instantiated, there is no override of the first. To give an answer with more specific examples, I would need to access the project, but that’s what it is, since I myself went through an identical situation to instantiate two voices of an algorithm to generate midi procedurally, in c++, going against the aesthetic of Steve Reich’s music, starting from code that had been implemented in Maxmsp. good luck

  • you’re saying you should make a class the part just for sound execution?

  • not what I would be saying is that I should have a constructor for each layer of trigated sound if the object in question did not support multiple file playback at the same time, but as it supports, I think the other solution is more feasible. I just thought analogously to how I solved a problem of sending multiple midi channels from c++ with the rtmidi library. however we are talking about different implementations, and supporting the object the firing of sounds without "re-trigger", ie without canceling the previously played sound, there is no need for this solution

  • the only context where it can make sense, is eventually in a code structuring logic, but this is up to you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.