Astra Child

MusicPlaylist

Estimated reading: 4 minutes 0 views

The MusicPlaylist class represents a playlist of music tracks (MusicIds) that can be played by a MusicPlayer in Unity projects. It offers a convenient way to manage and control the sequence of music tracks to be played during gameplay or other interactive experiences.

Class Structure

The MusicPlaylist class contains the following public properties and methods:

Properties

  • ids: A list of MusicId objects representing the music tracks in the playlist.
  • playedIds: A list of MusicId objects representing the music tracks that have been played. This acts like a history of played songs.
  • playMode: A property to set or get the play mode for the MusicPlayer.
  • loopPlaylist: A property to enable or disable looping of the entire playlist (works only in Sequential play mode).
  • loopSong: A property to enable or disable looping of the current song.

Methods

  • Reset(): Resets the playlist to its default state, clearing the history and current music track.
  • Validate(): Removes any null or invalid music tracks from the playlist.
  • RemoveNulls(): Removes all null music tracks from the playlist (tracks that exist but have not been assigned).
  • RemoveInvalids(): Removes all invalid music tracks from the playlist (tracks that are null, empty, or have ‘None’ library and music names).
  • SetNext(int index): Sets the current music track to the one at the given index in the playlist.
  • SetNext(string libraryName, string musicName): Sets the current music track to the one with the specified library name and music name.
  • Contains(string libraryName, string musicName): Checks if the playlist contains a music track with the given library name and music name.
  • GetMusicId(string libraryName, string musicName): Retrieves the MusicId object with the specified library name and music name.
  • GetMusicId(int index): Retrieves the MusicId object at the given index in the playlist.
  • LoadNext(): Loads the next music track in the playlist based on the current play mode and returns it.
  • LoadPrevious(): Loads the previous music track in the playlist, if available, and returns it.
  • SetPlayMode(PlayMode value): Sets the play mode of the playlist.
  • SetLoop(bool value): Sets whether the playlist should loop when it reaches the end.
  • SetLoopCurrentSong(bool value): Sets whether the current song should be looped.
  • Clear(): Clears the playlist and resets the current music track and index.
  • Add(MusicId musicId): Adds a music track to the playlist. If the music track already exists, it will not be added again.
  • Add(string libraryName, string musicName): Adds a music track to the playlist with the specified library name and music name.
  • AddNew(): Adds a new music track to the playlist with default library name and music name.
  • Insert(int index, MusicId musicId): Inserts a music track at the specified index in the playlist.
  • Insert(int index, string libraryName, string musicName): Inserts a music track with the specified library name and music name at the specified index.
  • Remove(MusicId musicId): Removes the specified music track from the playlist, if it exists.
  • Remove(string libraryName, string musicName): Removes the first music track with the given library name and music name from the playlist, if it exists.
  • RemoveAt(int index): Removes the music track at the specified index from the playlist.
  • IndexOf(MusicId musicId): Returns the index of the given music track in the playlist.
  • IndexOf(string libraryName, string musicName): Returns the index of the first music track with the given library name and music name in the playlist.

Example Usage

// Create a new MusicPlaylist
MusicPlaylist musicPlaylist = new MusicPlaylist();

// Add music tracks to the playlist
musicPlaylist.Add("MusicLibrary", "MainTheme");
musicPlaylist.Add("MusicLibrary", "Victory");

// Set the playlist play mode and looping options
musicPlaylist
    .SetPlayMode(PlayMode.Sequential)
    .SetLoop(true)
    .SetLoopCurrentSong(false);

// Validate and remove any invalid music tracks from the playlist
musicPlaylist.Validate();

// Load and play the next music track in the playlist
musicPlaylist.LoadNext();Code language: JavaScript (javascript)

For more detailed information, please refer to the official API documentation available at https://api.doozyui.com.

Share this Doc
CONTENTS
Scroll to Top