Making a playlist

Hey there, I plan on making a playlist which plays random background music all the time. Here is the code written in main.lua:

--music algorithm local function onSystemEvent( event ) local eventType = event.type if ( eventType == "applicationStart" ) then --occurs when the application is launched and all code in "main.lua" is executed print("success?") playlist = { "e.ogg", "foria.ogg" } randomTrack = math.random( 1,2 ) if randomTrack == 1 then --e.ogg lasts for around 318 000 ms audio.play( audio.loadStream( "e.ogg" ) ) timer.performWithDelay( 318000, function() randomTrack = 2 end ) end if randomTrack == 2 then audio.play( audio.loadStream( "foria.ogg" ) ) --foria.ogg lasts for around 278 000 ms timer.performWithDelay( 278000, function() randomTrack = 1 end ) end end end Runtime:addEventListener( "system", onSystemEvent )

When I start the game, a random track is played, which is good. However, when that track finishes, no other track plays afterwards. I find it weird because I’m convinced that the game logic is flawless (apparently it isn’t). Really hope someone could guide me to the solution.

Thanks, Joe :slight_smile:

(note: ignore the “print(“success”)” used just for testing purposes)

Hi Joe,

This is basically just a matter of how you’ve structured the code. I see that you call a timer to determine when “e.ogg” is finished. That’s fine… and then I see you call a function to set “randomTrack” to 2. However, in Lua, nothing else will happen after that… it won’t continue to process lines after/outside that call, because it considers its job done, and it just bails out (so, nothing happens).

I suggest you structure your code more like this (pseudo-code):

– set a local variable “trackToPlay” with initial value of “e.ogg”

– function (i.e. “playTrack”) to play a music track

------ play the music track based on value of “trackToPlay”

------ start the timer

------ when the timer completes, set “trackToPlay” to something else (random?)

------ call this SAME function to begin playing the next track

– onSystemEvent function

------ set “trackToPlay” to a random track number

------ play first track when application starts by calling the “playTrack” function above

I converted the onSystemEvent() event function to a normal playMusic() function, then I removed the event listener and replaced it with a simple function call 

playMusic()

Finally I called the function after each time randomTrack is assigned another random value after the timer stopped like you told me. It worked :slight_smile: ! Tank you for your help.

Hi,

You might find some interesting info in this post as well. Look out though, because It is using Storyboard (pre-Composer).

Cheers.

Hi Joe,

This is basically just a matter of how you’ve structured the code. I see that you call a timer to determine when “e.ogg” is finished. That’s fine… and then I see you call a function to set “randomTrack” to 2. However, in Lua, nothing else will happen after that… it won’t continue to process lines after/outside that call, because it considers its job done, and it just bails out (so, nothing happens).

I suggest you structure your code more like this (pseudo-code):

– set a local variable “trackToPlay” with initial value of “e.ogg”

– function (i.e. “playTrack”) to play a music track

------ play the music track based on value of “trackToPlay”

------ start the timer

------ when the timer completes, set “trackToPlay” to something else (random?)

------ call this SAME function to begin playing the next track

– onSystemEvent function

------ set “trackToPlay” to a random track number

------ play first track when application starts by calling the “playTrack” function above

I converted the onSystemEvent() event function to a normal playMusic() function, then I removed the event listener and replaced it with a simple function call 

playMusic()

Finally I called the function after each time randomTrack is assigned another random value after the timer stopped like you told me. It worked :slight_smile: ! Tank you for your help.

Hi,

You might find some interesting info in this post as well. Look out though, because It is using Storyboard (pre-Composer).

Cheers.