Raspberry Pi Sound issues- trying to fix it

In working through my Linux/Raspberry Pi eBook(Yes – the second eBook!), I’m up to the chapter where sounds are introduced using the SDL_mixer library. And I’ve hit two sets of problems.
It sometimes refuses to initialize the sound code. This code below hits the LogError line:
int success=Mix_OpenAudio(22050, AUDIO_S16LSB, 2, 8192);
if (success==-1 ) {
LogError("InitSetup failed to init audio");
}
The other day it was working but not today. Now I have updated the Pi’s code (sudo apt update etc) but I wouldn’t have expected that to break it. I’ve been looking on the internet and find the whole thing a bit complicated.
I’ve got my Pi running 64-bit Raspberry Pi OS. I’ve changed the output device to headphones which plug into the headphone socket. If I run the VLC media player and tell it to play into the headphones, it will happily play the .wav files I’ve got for the asteroids game.
But if I run speaker-test, a terminal application with this command line
speaker-test -c2 -twav -l7 plughw:1,0
I get
speaker-test 1.2.4 Playback device is default Stream parameters are 48000Hz, S16_LE, 2 channels WAV file(s) Setting of hwparams failed: Invalid argument
By running this command:
aplay -L
I got 71 lines of output but of these these below are the most important
output hw:CARD=Headphones,DEV=0 bcm2835 Headphones, bcm2835 Headphones
and the speaker-test command using the device parameter –Dhw:Headphones now worked. I’ve highlighted the bits in the aplay output needed to identify the device.
The new command is
speaker-test -c2 -twav -l7 -Dhw:Headphones
I can now hear a female voice saying front left then front right a few times in my headphones.
So my Pi’s sound device is working; I just have to figure why SDL_mixer isn’t always. I’ll keep looking.
And the second problem which only occurs when the mixer is working, is when you play a lot of sounds. The PlayASound() function checks the result. On Windows it never had a problem but on Raspberry Pi, when you blow up a lot of asteroids say at one time, it plays a number of explosions then returns an error for each explosion after that. I think there’s only so many channels; that’s an easy fix; just ignore the error and return as if it succeeded.