Skip to content

Music Player

MusicPlayer, a static global class, is the in-game music player. It allows you to control the music player in the same way that the in-game music player user interface does.

Example usage: MusicPlayer.repeat_track = true.

Member Variables

Like Object member variables, MusicPlayer has its own member variables. They allow for direct access to the MusicPlayer's property information without a helping function. Some are read-only.

Variable Description Type
repeat_track If the current audioclip should be repeated.
shuffle If the playlist should play shuffled.
playlistIndex Current index of the playlist. -1 if no playlist audioclip is playing.
loaded If all players loaded the current audioclip. Read only.
player_status The current state of the music player. Read only.
Options: "Stop", "Play", "Loading", "Ready".

Function Summary

MusicPlayer Functions

Functions that interact with the in-game music player.

Function Name Description Return
play() Plays currently loaded audioclip. Returns true if the music player is playing, otherwise returns false. (#play)
pause() Pauses currently playing audioclip. Returns true if the music player is paused, otherwise returns false. (#pause)
skipForward() Skips to the next audioclip in playlist if possible. Returns true if skip was successful, otherwise returns false. (#skipForward)
skipBack() Skips to the beginning of the audioclip or if the play time is less than 3 seconds to the previous audioclip in playlist if possible. Returns true if skip was successful, otherwise returns false. (#skipBack)
getCurrentAudioclip() Gets the currently loaded audioclip. (#getCurrentAudioclip)
setCurrentAudioclip() Sets the audioclip to be loaded. (#setCurrentAudioclip)
getPlaylist() Gets the current playlist. (#getPlaylist)
setPlaylist() Sets the current playlist. (#setPlaylist)

Function Details

play()

 Plays currently loaded audioclip. Returns true if the music player is playing, otherwise returns false.

--Example Usage
startLuaCoroutine(self, "PlayMusic")

--Plays currently loaded audioclip when everyone has loaded the audioclip.
function PlayMusic()
  --Wait for everyone to load the audioclip.
  while MusicPlayer.loaded == false do
      coroutine.yield(0)
  end

  --Play audioclip.
  MusicPlayer.play()

  return 1
end

pause()

 Pauses currently playing audioclip. Returns true if the music player is paused, otherwise returns false.

--Example Usage
startLuaCoroutine(self, "PauseMusic")

--Pauses the currently playing audioclip after 1000 frames.
function PauseMusic()
  --Wait 1000 frames
  for i=1,1000 do
    coroutine.yield(0)
  end

  --Pause audioclip
  MusicPlayer.pause()

  return 1
end

skipForward()

 Skips to the next audioclip in playlist if possible. Returns true if skip was successful, otherwise returns false.

--Example Usage
MusicPlayer.skipForward()

skipBack()

 Skips to the beginning of the audioclip or if the play time is less than 3 seconds to the previous audioclip in playlist if possible. Returns true if skip was successful, otherwise returns false.

--Example Usage
MusicPlayer.skipBack()

getCurrentAudioclip()

 Gets the currently loaded audioclip.

--Example Usage
currentAudioclip = MusicPlayer.getCurrentAudioclip()
--Example Returned Table
{url="Audioclip Url", title="Audioclip Title"}


setCurrentAudioclip(...)

 .Sets the audioclip to be loaded.

setCurrentAudioclip(parameters)

  • parameters: A Table containing the audioclip parameters.
    • parameters.url: Url for the new audioclip.
    • parameters.title: Title for the new audioclip.
--Example Usage
parameters =
{
    url="SOME URL HERE",
    title="SOME TITLE HERE"
}
MusicPlayer.setCurrentAudioclip(parameters)

getPlaylist()

 Gets the current playlist.

--Example Usage
playlist = MusicPlayer.getPlaylist()
--Example Returned Table
{
  {url="Audioclip Url 1", title="Audioclip Title 1"},
  {url="Audioclip Url 2", title="Audioclip Title 2"},
  {url="Audioclip Url 3", title="Audioclip Title 3"}
}


setPlaylist(...)

 .Sets the current playlist.

setPlaylist(parameters)

  • parameters: A Table containing the playlist parameters.
    • parameters: A Table containing the audioclip parameters.
      • parameters.url: Url for the new audioclip.
      • parameters.title: Title for the new audioclip.
--Example Usage
parameters =
{
  {url="SOME URL HERE 1",title="SOME TITLE HERE 1"},
  {url="SOME URL HERE 2",title="SOME TITLE HERE 2"},
  {url="SOME URL HERE 3",title="SOME TITLE HERE 3"}
}
MusicPlayer.setPlaylist(parameters)