Hi all,
Here is my little trick to loop any sound music file on you app.
1) Select a loop audio file in caf format, I took one from GarageBand loop library
2) See exactly how long the loop is in seconds or milliseconds (milliseconds is best, but anyway)
3) Ensure that .caf file is converted with atfconvert to an appropriate format to work in the real device. See CORONA documentation for this.
4) In your app, at the begin add this code:
local electro=media.newEventSound("electro.caf") -- in this case audio file is electro.caf
5) Then, in your in game or main menu function or where you want to add the sound in loop add the following line:
musicHandler=timer.performWithDelay(1950,loop,0) -- in this case, will get a sound/music handler to invoke timer every 1,950 sec (which is the duration of my loop) calling a function loop infinitely, that's why we use the third parameter equal to 0)
6) Then you have to add loop function in some part of your code, doesn't matter where you will put this piece of code:
function loop:timer(event)
media.playEventSound(electro) -- play the sound from cache
return event.source -- this return the timer object to our musicHandler variable, we needed for next step!
end
7) Finally, you would wish to stop that sound/music loop from somewhere in your code, so let's do it with this simple sentence:
timer.cancel(musicHandler)
And that's all, you have your background music synchronized without compromising complex operations in LUA and getting the benefit of Timer user, which run on separated thread. And the best part of it, you can also play another sound, like effect over your background music, but you can explore it by yourself.
Well I hope this would be useful for CORONA community.
Flavio.
Thanks Flavio for sharing this.