Kill one audio loop and play another

My app has an audio loop that plays during the main menu, and a different audio loop(s) for the game play. When going back to the main menu my app crashes when I do this:

playLoop("audio\_menuloop.mp3");  

the playLoop function looks like this:

function playLoop(snd)  
 audio.stop();  
 audio.dispose( loopMusic );  
 loopMusic = nil;  
  
 loopMusic = audio.loadStream ( snd );  
 audio.setVolume( 1, { channel=loopChannel } );  
 audio.play( loopMusic, { channel=loopChannel, loops=-1, fadeIn=500 } );  
  
 return;  
end  

Interestingly, it doesn’t crash when going from the main menu to the game play. What am I doing wrong? How do you guys handle this situation? Here’s some crash output:

Thread 0 Crashed: Dispatch queue: com.apple.main-thread 0 com.yourcompany.SDL-sound 0x001b2bfa Sound\_FreeSample + 93 1 net.playcontrol.ALmixer 0x00180e65 Internal\_FreeData + 48 [import]uid: 52127 topic_id: 12867 reply_id: 312867[/import]

You have your play and dispose in the same function. [import]uid: 40033 topic_id: 12867 reply_id: 47232[/import]

I tried disposing the audio in a different function, but got the same crash.

[code]
function playLoop(snd)
stopAllSounds();

loopMusic = audio.loadStream ( snd );
audio.setVolume( 1, { channel=loopChannel } );
audio.play( loopMusic, { channel=loopChannel, loops=-1, fadeIn=500 } );

return;
end

function stopAllSounds()
audio.stop();
audio.dispose( loopMusic );
return;
end
[/code] [import]uid: 52127 topic_id: 12867 reply_id: 47234[/import]

@jn19: I am having this same issue right now… Where you are to figure this out?

Thanks,

Willy J.
[import]uid: 66859 topic_id: 12867 reply_id: 52701[/import]

Have you considered using audio.stop() and passing it your explicit soundHandle as parameter, a soundHandle which you reassign a value when there’s a new sound, instead of using the dispose() method? [import]uid: 10284 topic_id: 12867 reply_id: 52769[/import]

What you describe should work. I just wrote my own simple test to make sure it isn’t a Corona bug. It worked fine for me (Mac Simulator). My guess is that you might be overwriting your global loopMusic variable somewhere else in your code so it isn’t what you think it is when you dispose the sound. Or maybe some where you disposed of the sound already and are re-disposing it which will also cause a crash.

(Code adapted from the ButtonEvents example.)

  
require("ui")  
  
loopMusic = nil  
musicSelection = 1  
  
function playLoop(snd)  
 audio.stop()  
 audio.dispose( loopMusic )  
 loopMusic = nil  
  
 loopMusic = audio.loadStream(snd)  
 audio.play( loopMusic, {channel=1, loops=-1, fadeIn=500})  
end  
  
local button1Press = function( event )  
 if musicSelection == 1 then  
 playLoop("background.mp3")  
 musicSelection = 2  
 else  
 playLoop("audio\_menuloop.mp3")  
 musicSelection = 1  
 end  
end  
  
local button1 = ui.newButton{  
 default = "buttonRed.png",  
 over = "buttonRedOver.png",  
 onPress = button1Press,  
-- onRelease = button1Release,  
 text = "Button 1 Label",  
 emboss = true  
}  
button1.x = 160; button1.y = 160  

[import]uid: 7563 topic_id: 12867 reply_id: 52782[/import]