Skip to content

Music Player

MusicPlayer is a static global class which allows you to control the in-game music playback i.e. the in-game "Music" menu.

Member Variables

Variable Description Type
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".
playlistIndex

Use playlist_index.

Current index of the playlist. -1 if no playlist audioclip is playing.
playlist_index Current index of the playlist. -1 if no playlist audioclip is playing.
repeat_track If the current audioclip should be repeated.
shuffle If the playlist should play shuffled.

Function Summary

Functions that interact with the in-game music player.

Function Name Description Return  
getCurrentAudioclip() Gets the currently loaded audioclip.
getPlaylist() Gets the current playlist.
pause() Pauses currently playing audioclip. Returns true if the music player is paused, otherwise returns false.
play() Plays currently loaded audioclip. Returns true if the music player is playing, otherwise returns false.
setCurrentAudioclip() Sets the audioclip to be loaded.
setPlaylist() Sets the current playlis
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.
skipForward() Skips to the next audioclip in playlist if possible. Returns true if skip was successful, otherwise returns false.

Function Details

getCurrentAudioclip()

Gets the currently loaded audioclip.

Returned table

  • Table describing the current audioclip.
    • url: The URL of the current audioclip.
    • title: The title of the current audioclip.

Example

Print the title of the current audioclip.

local clip = MusicPlayer.getCurrentAudioclip()
print("Currently playing '" .. clip.title .. "'")


getPlaylist()

Gets the current playlist.

Returned table

  • Playlist table, consisting of zero or more audioclip sub-tables.
    • Sub-table describing each audioclip.
      • url: The URL of the current audioclip.
      • title: The title of the current audioclip.

Example

Print the track number and title of each audioclip making up the playlist.

local playlist = MusicPlayer.getPlaylist()
for i, clip in ipairs(playlist) do
    print(i .. " - " .. clip.title)
end


pause()

Pause the current audioclip.

Returns true if the music player is/was paused, otherwise false.

Example

Pause the current track.

MusicPlayer.pause()


play()

Plays the current audioclip.

Returns true if the music player is/was playing, otherwise false.

Example

Play the current track.

MusicPlayer.play()


setCurrentAudioclip(...)

.Sets/loads the specified audioclip.

setCurrentAudioclip(parameters)

  • parameters: A table describing an audioclip.
    • url: The URL of the audioclip.
    • title: The title of the audioclip.

Example

Set the current track.

MusicPlayer.setCurrentAudioclip({
    url = "https://domain.example/path/to/clip.mp3",
    title = "Example"
})


setPlaylist(...)

Sets the current playlist.

setPlaylist(parameters)

  • parameters: A table containing zero or more audioclip sub-tables.
    • Sub-table describing each audioclip.
      • parameters.url: The URL of an audioclip.
      • parameters.title: The title of an audioclip.

Example

Set the current playlist to include three pieces of music.

MusicPlayer.setCurrentAudioclip({
    {
        url = "https://domain.example/path/to/clip.mp3",
        title = "Example"
    },
    {
        url = "https://domain.example/path/to/clip2.mp3",
        title = "Example #2"
    },
    {
        url = "https://domain.example/path/to/clip3.mp3",
        title = "Example #3"
    }
})


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

Skip backwards to either the beginning of the audioclip, or the prior audioclip in the playlist.

MusicPlayer.skipBack()


skipForward()

Skips to the next audioclip in the current playlist. If the current audioclip is the last of the playlist, loops around to the first audioclip in the playlist.

Returns true if skip was successful, otherwise returns false.

Example

Skip to the next audioclip.

MusicPlayer.skipForward()