1

In my program, currently I am able to play background music. My game is based off a shooting game, how can I make it where if I press a button, let's say the up arrow, it ail play a sound and right after it plays the sound, it continues where we left off in the background music? Here is my code:

#include <iostream>
#include <Windows.h>
#include <conio.h>
#pragma comment (lib , "winmm.lib") // Used for sound

using namespace std;

// Define key
#define KEY_UP 72       

int main()
{
    int key = 0;

    while(true)
    {
        PlaySound(TEXT("Background Music.wav"), NULL, SND_FILENAME|SND_ASYNC);
        key = _getch();

        if (key == KEY_UP)
            PlaySound(TEXT("GunSound.wav"), NULL, SND_FILENAME|SND_ASYNC);
    }
    return 0;
}
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
  • It looks like the behavior you want is not attainable by using the PlaySound() function. If you want that behavior you'll probably need to rewrite your program to use a more flexible sound-output API (such as DirectX or waveOut, see this question: http://stackoverflow.com/questions/2010400/best-api-for-low-level-audio-in-windows ) – Jeremy Friesner May 13 '15 at 03:42

1 Answers1

0

If you want your background music to play continuously, you need use the SND_LOOP parameter. The problem is that in this mode the API goes into a mode where the music won't stop until you call with with a NULL wave file name. I bet this can work if you put the background music sound on a separate thread. If you do this then rest of your while loop ought to work.

DirectSound is definitely your best bet if you start getting serious with your video game. There are lots of sample apps on CodeGuru to do this.

Ron Kuper
  • 817
  • 5
  • 14