[Resolved] Try catch syntax help need

I am new on Corona, need some help.
My game tries to play music.
media.playSound( “song.mp3” ) – I wish the program can continue even if the song.mp3 is not there.

My game stops here when song.mp3 does not exist in file, but I wish the program can continue.

How can I have a try catch to let it go? or any other suggestion?

thank you for help!
Bill [import]uid: 150242 topic_id: 28805 reply_id: 328805[/import]

This snippet checks to see if the file (song.mp3) exists before attempting to play it. See if that helps out.

[code]
local path = system.pathForFile( “song.mp3”, system.DocumentsDirectory )
local fhd = io.open( path )

– Determine if file exists
if fhd then
print( “File exists” )
media.playSound( “song.mp3” )
else
print( “File does not exist!” )
end
[/code] [import]uid: 58885 topic_id: 28805 reply_id: 116037[/import]

This is a smart approach! I will try it. Thank you! [import]uid: 150242 topic_id: 28805 reply_id: 116063[/import]

No problem, just happy I could help :slight_smile: [import]uid: 58885 topic_id: 28805 reply_id: 116068[/import]

Please use the audio.* APIs instead of the media.* APIs for audio unless you have a very good reason. The media.* APIs are eventually going to be deprecated.

audio.loadSound and audio.loadStream should return nil if the file can’t be loaded and avoid halting your program unlike media.playSound.

[import]uid: 7563 topic_id: 28805 reply_id: 116108[/import]

@ewing would this be the correct way of doing it using the audio API?

local mySongSound = audio.loadStream("song.mp3") --Returns nil if file doesn't exist  
  
--Only play if the file exists  
if(mySongSound~=nil) then   
 audio.play(mySongSound)  
end  

-Edited to use loadStream instead of loadSound because song.mp3 is probably a larger file used for bkg music. [import]uid: 58885 topic_id: 28805 reply_id: 116112[/import]

Yes, that should work. Though, if your “song.mp3” is a long/large file, you probably should consider using audio.loadStream instead of audio.loadSound.

[import]uid: 7563 topic_id: 28805 reply_id: 116113[/import]

great to know, thank you all! [import]uid: 150242 topic_id: 28805 reply_id: 116378[/import]