Why doesn't this play list work?

to me I think this code should work but it doesn’t. I am not shure what to do

[code]

song = math.random (1, 5)

function play1 (event)
if (song==1) then
song1 = audio.loadStream(“Maximegame.m4a”)
audio.play(song1)
timer.performWithDelay( 10000, play2s)
end
end

function play2 (event)
if (song==2) then
song2 = audio.loadStream(“Checkmate.mp3”)
audio.play(song2)
timer.performWithDelay( 10000, play3s)
end
end

function play3 (event)
if (song==3) then
song3 = audio.loadStream(“08TechNStuff.mp3”)
audio.play(song3)
timer.performWithDelay( 10000, play4s)
end
end

function play4 (event)
if (song==4) then
song4 = audio.loadStream(“06RatherEscape.mp3”)
audio.play(song4)
timer.performWithDelay( 10000, play5s)
end
end

function play5 (event)
if (song==5) then
song5 = audio.loadStream(“05CrazyPlane.mp3”)
audio.play(song5)
timer.performWithDelay( 10000, play1s)
end
end

function play1s (event)
song1 = audio.loadStream(“Maximegame.m4a”)
audio.play(song1)
timer.performWithDelay( 10000, play2s)
audio.pause(song5)
end

function play2s (event)
song2 = audio.loadStream(“Checkmate.mp3”)
audio.play(song2)
timer.performWithDelay( 10000, play3s)
audio.pause(song1)
end

function play3s (event)
song3 = audio.loadStream(“08TechNStuff.mp3”)
audio.play(song3)
timer.performWithDelay( 10000, play4s)
audio.pause(song2)
end

function play4s (event)
song4s = audio.loadStream(“06RatherEscape.mp3”)
audio.play(song4)
timer.performWithDelay( 10000, play5s)
audio.pause(song3)
end

function play5s (event)
song5 = audio.loadStream(“05CrazyPlane.mp3”)

audio.play(song5)
timer.performWithDelay( 10000, play1s)
audio.pause(song4)
end

timer.performWithDelay( 1, play1)
timer.performWithDelay( 1, play2)
timer.performWithDelay( 1, play3)
timer.performWithDelay( 1, play4)
timer.performWithDelay( 1, play5)
[import]uid: 23689 topic_id: 17367 reply_id: 317367[/import]

why all the times starting at the same time 1 millisecond
why event in function
please describe what your trying to do [import]uid: 7911 topic_id: 17367 reply_id: 65718[/import]

It looks like you’re trying to play a preview of each track for 10 seconds in a loop right?

The following is a quick code-snippet I jotted down and should get you on the right track. (no pun intended :smiley: )
It makes it easy to add/remove tracks just by editing the trackList table.
[lua]local trackList =
{
“gamemusic1.mp3”,
“gamemusic2.mp3”,
“gamemusic3.mp3”,
“gamemusic4.mp3”,
“gamemusic5.mp3”
}

local trackHandle; – handle for playing track
local currentTrack; – track to start with
local isPreview = false; – only play preview
local PREVIEW_LENGTH = 10000; – length of preview in ms

– forward declarations
local playList; – function for starting playlist
local onTrackComplete = function(event)
audio.dispose(trackHandle); --free up resources used by handle
trackHandle = nil;

currentTrack = currentTrack + 1;

if (currentTrack > #trackList) then
currentTrack = 1;
end

playList(currentTrack, isPreview);
end

playList = function(trackNumber, preview)
local audioParams =
{
onComplete = onTrackComplete;
}

if (preview) then
isPreview = true;
audioParams.duration = PREVIEW_LENGTH;
else
isPreview = false;
end

– load new track and start playing
trackHandle = audio.loadStream(trackList[trackNumber]);
audio.play(trackHandle, audioParams);
end
–initialize random number generator
math.randomseed(system.getTimer());

currentTrack = math.random(1, #trackList);
playList(currentTrack, true);[/lua]

If you want to play the full length of each track just call

playList(currentTrack, false), or just

playList(currentTrack)

NOTE;
If you’re trying this on a device with iOS5, then you must compile this with build 645 or later. There’s a bug in iOS5 that has audio problems, and build 645 works around these bugs. [import]uid: 70847 topic_id: 17367 reply_id: 65763[/import]

This is great, but it’s not as random as i would like it to be. Is there any way i could make it more random ? Thanks so much for your response, its great! Basically every time you relaunch the simulator it is on a random song and it plays random songs every time.

Thanks so much! [import]uid: 23689 topic_id: 17367 reply_id: 65796[/import]

Well, it kinda makes for less randomization if there are only 5 songs. If you could have more songs it might be more random. What you could do is use Ice to choose a different song each time the game is run.

Good lucy,
J.K. [import]uid: 66117 topic_id: 17367 reply_id: 65801[/import]

But it doesn’t feel random at all, it looks like its going in a pattern just 1 to last then loops i guess. [import]uid: 23689 topic_id: 17367 reply_id: 65804[/import]

Well, like the previous poster said, you can’t get much randomness with 5 tracks.
Judging from your first code I thought that you wanted to just choose the starting track at random and then play through the list in order.
However if you want to have more randomness after every track just modify the onComplete function to set currentTrack to a random track. [import]uid: 70847 topic_id: 17367 reply_id: 65815[/import]

math.randomseed(os.time()) may perhaps give more randomness… perhaps [import]uid: 84637 topic_id: 17367 reply_id: 66067[/import]