Working on a simple space shooter and currently implementing sound using the SDL. Got it working but encountered the following problem:
I have two sound files, one for the laser cannon and one for explosions. When I start the game both play fine and and at the right time. But after about 15 or so shots and explosions the sounds stop being played (the game essentially becomes silent). Everything else (movement, firing, asteroid-spawning) keeps working, but there is no more sound. Any idea?
Yeah, I hoped it might be something obvious as I'm still struggling with error-reporting aside from outputting it through the console at runtime. And unfortunately I can't output it through the console for this game. So I would have to build a complete error reporting to file system just to catch this problem, which is far out of scope of this little test.
Having tried to investigate the problem further, I can now at least say with certainty that the sound stops working after exactly 15 sounds have been played (it doesn't matter which sounds).
So it seems that something is running full or getting clogged. But after looking at several examples online as well as the SDL wiki, I feel that my code is complete. Still, obviously it's missing something.
I also tried playing the sound directly from within the Process_Shooting() and Check_Collisions() functions without calling the Play_Sound() function. Same result. 15 sounds are played, then silence.
So I cobbled together a simple output to file error detection and narrowed the problem down:
The code fails at line 13 "SDL_OpenAudioDevice". The error message reads: "Invalid audio device ID".
Unfortunately, googling this problem does not return useful results. So, any idea as to why the hell the program fails to return the correct audio device id after exactly 15 successful tries?
A few additional notes:
The program cannot wait for a sound to be played completely, and depending on how fast the player pushes buttons, sound can (and does) overlap. So any solution that calls for a wait until current sound is played completely is not going to work. I have already tried that and all it does is pausing the program until the sound is played.
However, how fast or slow the 15 sounds are played does not matter. The audio stops working after 15 played sounds no matter how much time passes in between.
Well, actually I need the third option:
- Let the current sound finish, but start the new one while the current is still playing
Also, explosions might occur while the player is shooting or two explosions might happen at the same time, so these too can (and must be able to) overlap (with each other and laser) as well.
From my testing, it seems like it is already working that way as I did not hear any sound cut-offs. And rapidly hitting the fire button seems to overlap multiple laser-sounds as well.