problem reusing an audio channel with audio.play doesn't work

audio.play doesn’t work as advertised


– initialize and play song1 on channel 1
local bm=audio.loadStream(“song1.mp3”,system.ResourceDirectory)
local pbm=audio.play( bm, { channel=1, loops=-1, fadein=5000 } )

– time passes and later you’d like to stop and dispose of the song on channel 1,
– so that you can reuse the same channel 1 for another song (BUT THIS DOESN’T WORK’)

audio.stop(pbm)
pbm=nil
audio.dispose(bm)
bm=nil
local bm2=audio.loadStream(“song2.mp3”,system.ResourceDirectory)
local pbm2=audio.play( bm2, { channel=1, loops=-1, fadein=2000 } )

After much work on this, I found a serious flaw in the ‘fadein’ option feature of the ‘audio.play’ command. To work around this flaw, set the volume of the channel to 0 before playing audio and then use audio.fade to increase to the desired volume (just as audio.play is supposed to do) - see below)


– initialize and play song1 on channel 1
local bm=audio.loadStream(“song1.mp3”,system.ResourceDirectory)
audio.setVolume(0,{channel=1})
local pbm=audio.play( bm, { channel=1, loops=-1, fadein=0 } )
audio.fade({channel=1, time=5000, volume=1})

– then later when you want to use channel 1 again for
– something else, performing the proper stop and dispose
– works just fine

audio.stop(pbm)
pbm=nil
audio.dispose(bm)
bm=nil
– in this example, I can then immediately play song2 on the
– same channel successfully
audio.setVolume(1,{channel=1})
local bm2=audio.loadStream(“song2.mp3”,system.ResourceDirectory)
local pbm2=audio.play( bm2, { channel=1, loops=-1, fadein=2000 } )

I hope this helps someone - it would have helped me.

After much work on this, I found a serious flaw in the ‘fadein’ option feature of the ‘audio.play’ command. To work around this flaw, set the volume of the channel to 0 before playing audio and then use audio.fade to increase to the desired volume (just as audio.play is supposed to do) - see below)


– initialize and play song1 on channel 1
local bm=audio.loadStream(“song1.mp3”,system.ResourceDirectory)
audio.setVolume(0,{channel=1})
local pbm=audio.play( bm, { channel=1, loops=-1, fadein=0 } )
audio.fade({channel=1, time=5000, volume=1})

– then later when you want to use channel 1 again for
– something else, performing the proper stop and dispose
– works just fine

audio.stop(pbm)
pbm=nil
audio.dispose(bm)
bm=nil
– in this example, I can then immediately play song2 on the
– same channel successfully
audio.setVolume(1,{channel=1})
local bm2=audio.loadStream(“song2.mp3”,system.ResourceDirectory)
local pbm2=audio.play( bm2, { channel=1, loops=-1, fadein=2000 } )

I hope this helps someone - it would have helped me.