Audio stop working

Hi all, I have some difficulties to make work audio in my game properly.

After some time, all sounds in my game stop, both on simulator and on device. I use a module for a sound table like this:

[lua]

local soundTable={

    

    –//game.lua//--

    waveSound = audio.loadSound( “sounds//victory3.wav” ),

    loopSound = audio.loadSound( “sounds//loop.wav” ),

    

    --//asteroidsModule//–

    boomSound = audio.loadSound( “sounds//boom4.wav” ),

    scoreSound = audio.loadSound( “sounds//score.wav” ),

    

    --//invadersModule//–

    splatStrisciaSound = audio.loadSound( “sounds//splatStriscia.wav” ),

    

    --//shipModule//–

    laserSound = audio.loadSound( “sounds//blast.wav” ),

    engineSound = audio.loadSound( “sounds//jet3.wav” ),

    nukeSound = audio.loadSound( “sounds//nuke.wav” ),

    brakeSound = audio.loadSound( “sounds//teleport.wav” ),

    resetSound = audio.loadSound( “sounds//gameover.wav” ),

    

    --//ufoModule//–

    ufoBoomSound = audio.loadSound( “sounds//ufoBoom.wav” ),

    laserUfoSound = audio.loadSound( “sounds//ufoBlast.wav” ),

}

return soundTable

[/lua]

and when I have to play a sound I use for example:

[lua]

    local clearSound=function() audio.stop( loopChannel )end

    local loopChannel = audio.play( soundTable[“loopSound”],{onComplete=clearSound} )

[/lua]

On simulator I see this errors:

audio error: No channels available for playingWarning (a lot of this in sequence)

and

 audio.stop() called with nil. To stop all channels, call audio.stop() with no parameters

Corona version 2014.2393 (2014.8.5)

Hi @marcoguerrera84,

For the first issue, you’re simply trying to play more than 32 channels of sounds and you need to find a way to minimize that amount. 32 channels is generally more than enough for any application (32 sounds playing at the same time) so perhaps you accidentally are duplicating the same sound in your code. Check carefully that you’re managing the sounds properly.

For the second issue, Lua does not know what “loopChannel” is, by order of how you’ve set up the variables (you declare “loopChannel” after the function).

You could do an upvalue, but if your purpose is simply to stop the audio channel which that specific sound is playing on, then you can use the “event.channel” property to get the channel in the completion listener function.

http://docs.coronalabs.com/api/event/audio/channel.html

Like this:

[lua]

local clearSound = function( event )

   audio.stop( event.channel )

end

local loopChannel = audio.play( soundTable[“loopSound”], { onComplete=clearSound } )

[/lua]

Take care,

Brent

If the same sound is played before the previous is finished, does corona duplicate it? Like blaster sound for example.

The purpose of stop instruction in my mind was for free the channel and avoid to get all 32 channels.

Hi again,

Unless you manually handle channels and tell Corona to play a sound on a specific channel, then the audio system will just look for a free channel and play the sound there. I’m not sure how you’re setup is, but how many “laser” sounds could be playing at the same time? I would imagine no more than 10, leaving 22 channels for other audio?

In any case, I believe the audio system automatically frees up a channel when the sound is finished playing on it, so there’s not a need to use a stop() on a sound completion. If you’re still getting a warning about exceeding 32 channels, I think there’s something else going on, because it’s rare to even approach the limit of 32 channels in my experience.

Best regards,

Brent

I realized that the laser key occupied a new channel every time user press it down, now I fixed it and the sound no longer crashes, even on some time, such as when the shuttle drops the bomb and all asteroids explode simultaneously, yet I get this error, but since sound no longer disappears, I think it’s ok.

Hi @marcoguerrera84,

For the first issue, you’re simply trying to play more than 32 channels of sounds and you need to find a way to minimize that amount. 32 channels is generally more than enough for any application (32 sounds playing at the same time) so perhaps you accidentally are duplicating the same sound in your code. Check carefully that you’re managing the sounds properly.

For the second issue, Lua does not know what “loopChannel” is, by order of how you’ve set up the variables (you declare “loopChannel” after the function).

You could do an upvalue, but if your purpose is simply to stop the audio channel which that specific sound is playing on, then you can use the “event.channel” property to get the channel in the completion listener function.

http://docs.coronalabs.com/api/event/audio/channel.html

Like this:

[lua]

local clearSound = function( event )

   audio.stop( event.channel )

end

local loopChannel = audio.play( soundTable[“loopSound”], { onComplete=clearSound } )

[/lua]

Take care,

Brent

If the same sound is played before the previous is finished, does corona duplicate it? Like blaster sound for example.

The purpose of stop instruction in my mind was for free the channel and avoid to get all 32 channels.

Hi again,

Unless you manually handle channels and tell Corona to play a sound on a specific channel, then the audio system will just look for a free channel and play the sound there. I’m not sure how you’re setup is, but how many “laser” sounds could be playing at the same time? I would imagine no more than 10, leaving 22 channels for other audio?

In any case, I believe the audio system automatically frees up a channel when the sound is finished playing on it, so there’s not a need to use a stop() on a sound completion. If you’re still getting a warning about exceeding 32 channels, I think there’s something else going on, because it’s rare to even approach the limit of 32 channels in my experience.

Best regards,

Brent

I realized that the laser key occupied a new channel every time user press it down, now I fixed it and the sound no longer crashes, even on some time, such as when the shuttle drops the bomb and all asteroids explode simultaneously, yet I get this error, but since sound no longer disappears, I think it’s ok.