audio dispose

I have an app that loads some sounds when the app launches then loads sounds as new screens come on to screen. I am trying to remove sounds from memory when the screen is done. On my android phone if I go into data/data/com…/files/coronaResources I see all the sounds I loaded and can not seem to get them removed. I would assume that if I was removing sounds correctly then the sound files should be removed from the directory when in use.

Here is some code pieces, any help would be appreciated, this has been driving me insane.

loading initial sounds (I would like these to stay in memory)

local yeaSounds = {}  
 yeaSounds[1] = audio.loadSound("congratulations.mp3")  
 yeaSounds[2] = audio.loadSound("goodjob.mp3")  
 yeaSounds[3] = audio.loadSound("smart.mp3")  
 yeaSounds[4] = audio.loadSound("wow.mp3")  
 yeaSounds[5] = audio.loadSound("yaya.mp3")  
local wrongSounds = {}  
 wrongSounds[1] = audio.loadSound("goodtry.mp3")  
 wrongSounds[2] = audio.loadSound("oops.mp3")  
 wrongSounds[3] = audio.loadSound("sorry.mp3")  
 wrongSounds[4] = audio.loadSound("tryagain.mp3")  

So if the user gets the correct item then that specific sound is loaded and played

 local i = math.random(5)  
 audio.play( yeaSounds[i], {onComplete=playWord} )  
function playWord()  
 if soundOn == true then  
 audioFile = audio.loadSound( content[playOrder[currQuestion]].id..".mp3" )  
 audio.play( audioFile, { channel=29, onComplete=disposeSound } )  

Once the sound is played it goes to disposeSound which I would like to unload the sound from memory

function disposeSound ( event )  
 print("dispose start fucntion")  
 audio.stop( 29 )  
 audio.dispose( 29 )  
 audioFile = nil  
 showWord()  
 print("dispose function")  
end  

It seems to complete becuase it calls the disposeSound as well as prints out the to print commands but doesnt seem to remove sound from memory.

[import]uid: 73307 topic_id: 20923 reply_id: 320923[/import]

You are using it wrong. You dispose your audioFile, not the channel. (The API allows you to play/share the same audioFile on multiple channels simultaneously.)

If you are going to be reusing these sounds often, it is more performant to keep these sounds in memory instead of loading and unloading all the time.
[import]uid: 7563 topic_id: 20923 reply_id: 82514[/import]

Thank you for your response. I will give it a try. I was trying to dispose the audioFile at fist but was having issues and found a form that lead me to believe that I needed to dispose the channel. If I am not mistaken audio.play picks any open channel so I also do not need to tell it which channel to use.

I will give it a try tonight.

Thanks [import]uid: 73307 topic_id: 20923 reply_id: 82523[/import]

Yes, audio.play will automatically pick a free channel if you don’t specify one.
[import]uid: 7563 topic_id: 20923 reply_id: 82528[/import]

OK so I have changed my code to the following as suggested.

function playWord()  
 if soundOn == true then  
 audioFile = audio.loadSound( content[playOrder[currQuestion]].id..".mp3" )  
 audio.play( audioFile, { onComplete=disposeSound } )  
function disposeSound ( event )  
 print("dispose start function")  
 audio.stop( audioFile )  
 audio.dispose( audioFile )  
 audioFile = nil  
 showWord()  
 print("dispose function")  
end  

After running through the app then navigating to the following directory on my Android phone (/data/data/com…/files/coronaResources) I still see the loaded audioFile. It seems to run through because I see my two print functions. Am I mistaken in thinking that they should be removed from the coronaResources directory?

Thanks again for your time and help [import]uid: 73307 topic_id: 20923 reply_id: 82596[/import]

I misunderstood your original question. dispose() frees RAM. You want to delete the externalized file. No, the file doesn’t get deleted. It will only be deleted when you upgrade your app with new assets (replaced technically) or you uninstall the app. We have to do this extra copy because of Android 2.2 limitations. When we can finally drop 2.2, we can stop copying this file and read it directly from your APK.

P.S. You have a bug with audio.stop(). Stop takes a channel number, not an audioFile. Also, since you are in the onComplete callback, you are guaranteed that the audio has stopped for that particular channel so you don’t need to call it. Finally, you can use event.channel to check the channel (in case you reuse the same callback for multiple plays) and event.handle (which is your audioFile).
[import]uid: 7563 topic_id: 20923 reply_id: 82597[/import]

Ahhh, thank you for explaining! I have been trying to get this data file deleted for a few days now:) I thought that the coronaResources folder was the “RAM” as files were loaded. I know that seems stupid because I know what RAM is but I thought thats how mobile devices dealt with files loaded into RAM.

Thanks also for the tip about audio.stop. I was trying everything I could find to get that file deleted.

Thanks again! [import]uid: 73307 topic_id: 20923 reply_id: 82607[/import]