Discord
Login
Community
DARK THEME

Change music when song finishes.

Hello,

I was doing some in depth testing on the Play Music functions to see what was possible. I was trying to make my music play a new song when the current song finishes, but it didn't seem to work as I thought it would.

Example:

initMusic = function()
  intro = true
  volume = .5 
end

updateMusic = function()
  if intro == true then
    local music = audio.playMusic("theme_intro")
    music.setVolume(volume)
    if music.getPosition() == music.getDuration() then
      music.stop()
      intro = false
    end
  end
  if intro == false then
    local music = audio.playMusic("theme_loop")
    music.setVolume(volume)
  end
end

This code seems to ignore my argument if music.getPosition() == music.getDuration() then, and just continually loops "theme_intro".

The Microstudio API documentation(https://microstudio.dev/documentation/API/) leads me to believe that my argument should work in the way I am implementing it.

noLoop = 0   
music = audio.playMusic("theme_intro", volume, noLoop )

So if I understand you correctly, because my track is set to loop, music.getDuration() is infinite?

Here is my new code with hard coded loop info with no change in results. The "theme_intro" keeps looping.

initMusic = function()
  intro = true
  volume = .5 
end

updateMusic = function()
  if intro == true then
    local music = audio.playMusic("theme_intro", volume, 0)
    if music.getPosition() == music.getDuration() then
      music.stop()
      intro = false
    end
  end
  if intro == false then
    local music = audio.playMusic("theme_loop", volume, 1)
  end
end
initMusic = function()
  intro = true
  volume = .5 
end

updateMusic = function()
  if intro == true then
    intro = false
    track_music = audio.playMusic("theme_intro", volume, 0 )
  end
  print( track_music.getPosition() )
  if (intro == false) and ( track_music.getPosition() == track_music.getDuration()) then
      intro = 2
      track_music.stop()
      track_music = audio.playMusic("theme_loop", volume, 1)
  end
end

Thanks Loginus, I'm guessing my mistake had to do with my argument placement. lol.

Before I saw your message, I got the same affect by creating a timer using after 60 seconds do

Happy Coding

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community