Sometimes audio.play doesn't play sounds... just sometimes

I’m facing a really weird issue… Sometimes when my code calls audio.play, no sound is played.  BUT only sometimes… Seems like 80% of the time it works as expected, but every once in a while there is just no sound…

I have a function playBackgroundMusic which I call at the start of the game to play the background music… see below: 

However, every once in a while no sound is played.  It seems to happen more often upon the first launch of the game, but not consistently.  Maybe 2 out of 10 times there is no sound, and the other 8 times it will work just fine. 

In my destroy when I change scenes, I call disposeBackgroundMusic() which is also below. 

I also do an audio.stop with a pairs loop to dispose of other sound effects that are unrelated to the background music. 

What I don’t understand is why it works most of the time, but occasionally I get no sound.  Does anyone see any issues?

local function playBackgroundMusic() local randomSong = math.random(1,6) if(randomSong == 1) then print("playing aurea...") backgroundMusic = audio.loadStream("sounds/aurea.mp3") audio.play( backgroundMusic, { channel=1, loops=-1 } ) elseif(randomSong == 2) then print("playing carefree...") backgroundMusic = audio.loadStream("sounds/carefree.mp3") audio.play( backgroundMusic, { channel=1, loops=-1 } ) elseif(randomSong == 3) then print("playing fretless...") backgroundMusic = audio.loadStream("sounds/fretless.mp3") audio.play( backgroundMusic, { channel=1, loops=-1 } ) elseif(randomSong == 4) then print("playing rainbows...") backgroundMusic = audio.loadStream("sounds/rainbows.mp3") audio.play( backgroundMusic, { channel=1, loops=-1 } ) elseif(randomSong == 5) then print("playing upbeat...") backgroundMusic = audio.loadStream("sounds/upbeat.mp3") audio.play( backgroundMusic, { channel=1, loops=-1 } ) elseif(randomSong == 6) then print("playing wallpaper...") backgroundMusic = audio.loadStream("sounds/wallpaper.mp3") audio.play( backgroundMusic, { channel=1, loops=-1 } ) end end

local function disposeBackgroundMusic() audio.stop(1) audio.dispose(backgroundMusic) backgroundMusic = nil end

Is it not working in the simulator or on device?

Is there any chance that the “playBackgroundMusic” function is getting triggered more than once?  You could try looking for a free audio channel as well.

[lua]

local availableChannel = audio.findFreeChannel()

audio.play(backgroundMusic, {channel = availableChannel, loops=-1})

[/lua]

Can you confirm that you have reserved channel 1?

One guess is that another sound might already be playing there

at the time you start this. That would stop the music from playing.

perhaps it’s just lag? You could load all audio beforehand and call them when needed

@pixec - good point

@nml2727 - try preloading all of your sounds on main.lua.  If that fixes it you can try preloading them in a module for more control.

It seems you guys were correct that the audio channel was already being used by other sounds.   I had not been reserving channel 1.  I added    audio.reserveChannels(1)   in main.lua and it seems the sound is playing every time since then.

Thank you andrake12 for the suggestion!

Thanks sporkfin and pixec for chiming in as well!

Yeah, that’s why I use

[lua]

local availableChannel = audio.findFreeChannel()

[/lua]

to avoid reserved channels

Do you specify channels every time you play a sound effect?  I have a lot of other sounds where I’m simply doing 

audio.play(sounds.blue\_button\_sound)

Is it fine to just let corona grab an open channel for me?  

Yes, that should be fine.  Looking back now I see that I added audio.findFreeChannel( ) to my sound module as a way to track which channel each sound is assigned to.