I have a problem when i press the button to play the sound clip/fx after couple of tries it just stops playing the sound. I am using SDL for this app bellow is the class sound.h with the sound.cpp
class Sound
{
private:
SDL_AudioDeviceID deviceId;
SDL_AudioSpec wavSpec;
Uint32 wavLength;
Uint8* wavBuffer;
public:
Sound() {
// SDL audio device init
SDL_Init(SDL_INIT_AUDIO);
}
SDL_AudioDeviceID getID() { return deviceId; }
Uint8* loadSound(const std::string& sound);
bool play();
};
sound.cpp:
#include "Classes/Sound.h"
Uint8* Sound::loadSound(const std::string& sound)
{
SDL_LoadWAV(sound.c_str(), &wavSpec, &wavBuffer, &wavLength);
return wavBuffer;
}
bool Sound::play()
{
SDL_AudioDeviceID deviceId;
deviceId = SDL_OpenAudioDevice(NULL, 0, &wavSpec, NULL, 0);
int success = SDL_QueueAudio(deviceId, wavBuffer, wavLength);
SDL_PauseAudioDevice(deviceId, 0);
//SDL_FreeWAV(wavBuffer);
//SDL_Delay(1);
return success;
}
the declaration:
// sounds
Sound sndStart, sndStop, sndClick, sndCancel, sndWin;
SDL_AudioDeviceID deviceId = sndStart.getID();
Uint8* sndBufStart = sndStart.loadSound("gamedata/sounds/start.wav");
Uint8* sndBufStop = sndStop.loadSound("gamedata/sounds/stop.wav");
Uint8* sndBufClick = sndClick.loadSound("gamedata/sounds/click.wav");
Uint8* sndBufCancel = sndCancel.loadSound("gamedata/sounds/cancel.wav");
Uint8* sndBufWin = sndWin.loadSound("gamedata/sounds/win.wav");
and the call inside the main.cpp where the actual button is pressed:
if (button(screen, &spinButton))
{
sndStart.play();
clearItems();
guessing->found(countLines);
guessing->spin();
clearItems();
playAnimation(guessing, itemsRect);
if (guessing->getLastWinning() > 0)
sndWin.play();
}
Thanks for any answer regarding my problem!