Alternating Music

hello!

I have here 3 music tracks and i want one of them to start playing randomly. I have coded the first run and it works, however, i want this cycle to keep repeating, that is, i want the function to keep repeating as soon as the first song ends

here is what i have by far:

local bgmusic1 = audio.loadStream( “OctoBlues.wav” )

local bgmusic2 = audio.loadStream( “WotM.wav” )

local bgmusic3 = audio.loadStream( “DistrictFour.wav” )

local musicReference

local bgmusicChannel

 musicReference = math.random(0, 2)

 if ( musicReference == 0 ) then bgmusicChannel = audio.play( bgmusic1) end

  if ( musicReference == 1 ) then bgmusicChannel = audio.play( bgmusic2) end

   if ( musicReference == 2 ) then bgmusicChannel = audio.play( bgmusic3) end

Hi @xxiihalo.devs,

I would suggest that you only load (audio.loadStream()) one music track, not all at the same time. Simply change the code to something like this:

[lua]

math.randomseed( os.time() )  – Seed random number generator

local musicReference = math.random( 0, 2 )

if ( musicReference == 0 ) then

   bgmusic = audio.loadStream( “OctoBlues.wav” )

elseif ( musicReference == 1 ) then

   bgmusic = audio.loadStream( “WotM.wav” )

elseif ( musicReference == 2 ) then

   bgmusic = audio.loadStream( “DistrictFour.wav” )   

end

local bgmusicChannel = audio.play( bgmusic, { loops = -1 } )  – Play music and repeat (loop) track forever

[/lua]

Hope this helps,

Brent

Thanks for the help!

Just in case, what do i do when, i play a random sound and it finishes and i want another random sound to play, that is, if i want to loop this code over and over.

Thanks again  :smiley:

Hello again,

So I assume you want to randomly choose a music track, play it just once, then randomly choose another? In that case, a little function-wrapping would do the trick. I also added some extra code to ensure the same track is not played more than once before a new track is randomly picked.

[lua]

math.randomseed( os.time() )  – Seed random number generator

local currentTrack

local bgmusic

local bgmusicChannel

local function playNewTrack()

   local musicReference = math.random( 0, 2 )

   if musicReference == currentTrack then

      playNewTrack()

   else

      currentTrack = musicReference

      audio.stop( 1 )

      audio.dispose( bgmusic )

      bgmusic = nil

      if ( musicReference == 0 ) then

         bgmusic = audio.loadStream( “OctoBlues.wav” )

      elseif ( musicReference == 1 ) then

         bgmusic = audio.loadStream( “WotM.wav” )

      elseif ( musicReference == 2 ) then

         bgmusic = audio.loadStream( “DistrictFour.wav” )   

      end

      bgmusicChannel = audio.play( bgmusic, { channel=1, onComplete=playNewTrack } )

   end

end

playNewTrack()  – Start playing a random track

[/lua]

Hi @xxiihalo.devs,

I would suggest that you only load (audio.loadStream()) one music track, not all at the same time. Simply change the code to something like this:

[lua]

math.randomseed( os.time() )  – Seed random number generator

local musicReference = math.random( 0, 2 )

if ( musicReference == 0 ) then

   bgmusic = audio.loadStream( “OctoBlues.wav” )

elseif ( musicReference == 1 ) then

   bgmusic = audio.loadStream( “WotM.wav” )

elseif ( musicReference == 2 ) then

   bgmusic = audio.loadStream( “DistrictFour.wav” )   

end

local bgmusicChannel = audio.play( bgmusic, { loops = -1 } )  – Play music and repeat (loop) track forever

[/lua]

Hope this helps,

Brent

Thanks for the help!

Just in case, what do i do when, i play a random sound and it finishes and i want another random sound to play, that is, if i want to loop this code over and over.

Thanks again  :smiley:

Hello again,

So I assume you want to randomly choose a music track, play it just once, then randomly choose another? In that case, a little function-wrapping would do the trick. I also added some extra code to ensure the same track is not played more than once before a new track is randomly picked.

[lua]

math.randomseed( os.time() )  – Seed random number generator

local currentTrack

local bgmusic

local bgmusicChannel

local function playNewTrack()

   local musicReference = math.random( 0, 2 )

   if musicReference == currentTrack then

      playNewTrack()

   else

      currentTrack = musicReference

      audio.stop( 1 )

      audio.dispose( bgmusic )

      bgmusic = nil

      if ( musicReference == 0 ) then

         bgmusic = audio.loadStream( “OctoBlues.wav” )

      elseif ( musicReference == 1 ) then

         bgmusic = audio.loadStream( “WotM.wav” )

      elseif ( musicReference == 2 ) then

         bgmusic = audio.loadStream( “DistrictFour.wav” )   

      end

      bgmusicChannel = audio.play( bgmusic, { channel=1, onComplete=playNewTrack } )

   end

end

playNewTrack()  – Start playing a random track

[/lua]

1 Like