math.randomseed( os.time() ) -- Seed random number generator local musicReference = math.random( 1,7 ) if ( musicReference == 1 ) then bgmusic = audio.loadStream( "sound1.mp3" ) elseif ( musicReference == 2 ) then bgmusic = audio.loadStream( "sound2.mp3" ) elseif ( musicReference == 3 ) then bgmusic = audio.loadStream( "sound3.mp3" ) elseif ( musicReference == 4 ) then bgmusic = audio.loadStream( "sound5.mp3" ) elseif ( musicReference == 5 ) then bgmusic = audio.loadStream( "sound6.mp3" ) elseif ( musicReference == 6 ) then bgmusic = audio.loadStream( "sound7.mp3" ) elseif ( musicReference == 7 ) then bgmusic = audio.loadStream( "sound8.mp3" ) end function playbtn:tap( event ) local bgmusicChannel = audio.play( bgmusic, { loops = -1 } ) end playbtn:addEventListener( "tap", playbtn ) function stopbtn:tap( event ) audio.stop( 1 ) end stopbtn:addEventListener( "tap", stopbtn )
Right now I currently have this and once you open up the app it will change a song but as long as I keep the app open it will repeat that same song. How do I make it so it will shuffle and play different songs after every song and is there a way to make a skip button to go to the next song? Thank you for all the help!
math.randomseed( os.time() ) -- Seed random number generator local currentTrack local bgmusic local bgmusicChannel --1,2,3,5,6,7,8 - the tracks I wanted to use local function playNewTrack() local musicReference = math.random( 1, 7 ) if musicReference == currentTrack then playNewTrack() else currentTrack = musicReference audio.stop( 1 ) audio.dispose( bgmusic ) bgmusic = nil if ( musicReference == 1 ) then bgmusic = audio.loadStream( "sound1.mp3" ) elseif ( musicReference == 2 ) then bgmusic = audio.loadStream( "sound2.mp3" ) elseif ( musicReference == 3 ) then bgmusic = audio.loadStream( "sound3.mp3" ) elseif ( musicReference == 4 ) then bgmusic = audio.loadStream( "sound5.mp3" ) elseif ( musicReference == 5 ) then bgmusic = audio.loadStream( "sound6.mp3" ) elseif ( musicReference == 6 ) then bgmusic = audio.loadStream( "sound7.mp3" ) elseif ( musicReference == 7 ) then bgmusic = audio.loadStream( "sound8.mp3" ) end end end playNewTrack() -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc local stopbtn = display.newImageRect(sceneGroup, "stop.png", 40, 40 ) stopbtn.x = 285 stopbtn.y = 25 local playbtn = display.newImageRect( sceneGroup, "play.png", 40, 40 ) playbtn.x = 40 playbtn.y = 25 ---------------------------------------------------------- function trump:tap( event ) bgmusicChannel = audio.play( bgmusic, { channel=1, onComplete=playNewTrack } ) end playbtn:addEventListener( "tap", playbtn ) function stopbtn:tap( event ) audio.stop( 1 ) end stopbtn:addEventListener( "tap", stopbtn )
Nvm again I figured it out! Here’s the code I used in case anyone else ever runs into this. It’s pretty much a button that plays music a button that stops music and it shuffles all your songs.
Just as a small change to your code, I would replace this…
if ( musicReference == 1 ) then bgmusic = audio.loadStream( "sound1.mp3" ) elseif ( musicReference == 2 ) then bgmusic = audio.loadStream( "sound2.mp3" ) elseif ( musicReference == 3 ) then bgmusic = audio.loadStream( "sound3.mp3" ) elseif ( musicReference == 4 ) then bgmusic = audio.loadStream( "sound5.mp3" ) elseif ( musicReference == 5 ) then bgmusic = audio.loadStream( "sound6.mp3" ) elseif ( musicReference == 6 ) then bgmusic = audio.loadStream( "sound7.mp3" ) elseif ( musicReference == 7 ) then bgmusic = audio.loadStream( "sound8.mp3" ) end
with this…
local audioFile = "sound"..musicReference..".mp3" bgmusic = audio.loadStream (audioFile)
Won’t improve performance but is more compact and less typing
[edit]
I’ve just noticed that in your code you don’t have a ‘sound4.mp3’. I don’t know if that’s a typo on your part or deliberate. If it’s deliberate then my code change won’t work due to the offset between musicReference and some of the filenames.
math.randomseed( os.time() ) -- Seed random number generator local currentTrack local bgmusic local bgmusicChannel --1,2,3,5,6,7,8 - the tracks I wanted to use local function playNewTrack() local musicReference = math.random( 1, 7 ) if musicReference == currentTrack then playNewTrack() else currentTrack = musicReference audio.stop( 1 ) audio.dispose( bgmusic ) bgmusic = nil if ( musicReference == 1 ) then bgmusic = audio.loadStream( "sound1.mp3" ) elseif ( musicReference == 2 ) then bgmusic = audio.loadStream( "sound2.mp3" ) elseif ( musicReference == 3 ) then bgmusic = audio.loadStream( "sound3.mp3" ) elseif ( musicReference == 4 ) then bgmusic = audio.loadStream( "sound5.mp3" ) elseif ( musicReference == 5 ) then bgmusic = audio.loadStream( "sound6.mp3" ) elseif ( musicReference == 6 ) then bgmusic = audio.loadStream( "sound7.mp3" ) elseif ( musicReference == 7 ) then bgmusic = audio.loadStream( "sound8.mp3" ) end end end playNewTrack() -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc local stopbtn = display.newImageRect(sceneGroup, "stop.png", 40, 40 ) stopbtn.x = 285 stopbtn.y = 25 local playbtn = display.newImageRect( sceneGroup, "play.png", 40, 40 ) playbtn.x = 40 playbtn.y = 25 ---------------------------------------------------------- function trump:tap( event ) bgmusicChannel = audio.play( bgmusic, { channel=1, onComplete=playNewTrack } ) end playbtn:addEventListener( "tap", playbtn ) function stopbtn:tap( event ) audio.stop( 1 ) end stopbtn:addEventListener( "tap", stopbtn )
Nvm again I figured it out! Here’s the code I used in case anyone else ever runs into this. It’s pretty much a button that plays music a button that stops music and it shuffles all your songs.
Just as a small change to your code, I would replace this…
if ( musicReference == 1 ) then bgmusic = audio.loadStream( "sound1.mp3" ) elseif ( musicReference == 2 ) then bgmusic = audio.loadStream( "sound2.mp3" ) elseif ( musicReference == 3 ) then bgmusic = audio.loadStream( "sound3.mp3" ) elseif ( musicReference == 4 ) then bgmusic = audio.loadStream( "sound5.mp3" ) elseif ( musicReference == 5 ) then bgmusic = audio.loadStream( "sound6.mp3" ) elseif ( musicReference == 6 ) then bgmusic = audio.loadStream( "sound7.mp3" ) elseif ( musicReference == 7 ) then bgmusic = audio.loadStream( "sound8.mp3" ) end
with this…
local audioFile = "sound"..musicReference..".mp3" bgmusic = audio.loadStream (audioFile)
Won’t improve performance but is more compact and less typing
[edit]
I’ve just noticed that in your code you don’t have a ‘sound4.mp3’. I don’t know if that’s a typo on your part or deliberate. If it’s deliberate then my code change won’t work due to the offset between musicReference and some of the filenames.