Audio stops after multiple times of pressing the button SDL C++

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!

I think the problem is that you open too many audio devices.

will creating a function for stopping the audio will fix the error and call it ?

SDL seems to have a limit of at most 16 open audio devices at the same time. After that it will simply fail to open new ones (SDL_OpenAudioDevice will return 0). When you no longer need an audio device you should close it using SDL_CloseAudioDevice.

I don’t know how lightweight the audio devices are, or whether it will cause performance problems to constantly open and close them for each sound effect. The impression that I’ve got is that one should normally only open one audio device and keep using it for the rest of the program but I’m not sure what the best way to handle that in SDL2 is. There are some things that I don’t know too much about, like SDL_AudioStream and SDL_QueueAudio, that might be useful.

Otherwise I suggest you use SDL_mixer. It’s an extension library to SDL that makes it easy to play music and sound effects. It also has support for additional file formats like MP3. Lazy Foo’s tutorial could help you get started (Note that the complete code for the program is available for download at the bottom of the page. It’s part of a series so you might want to start from the beginning)