MusicPlayer is a powerful audio component designed to play one or more Soundy music tracks using a
MusicPlaylist, which contains one or more
MusicIds. It utilizes
AudioPlayers to manage the playback of music in Unity projects. The
MusicPlayer provides a range of functionalities, including auto-advancing to the next track, cross-fading between tracks, and more.
Follow these steps to set up and use the
MusicPlayer in your project:
- Attach the
MusicPlayercomponent to a GameObject in your scene.
- Create a
MusicPlaylistasset, and associate it with the
MusicPlayerby setting the
Playlistfield in the inspector.
- Customize the various properties of the
MusicPlayerto suit your requirements, such as enabling auto-advancing, cross-fading, and specifying the cross-fade duration.
- Optionally, set a follow target (a Transform) for the music to move along with a GameObject in the scene, creating dynamic audio experiences.
- Populate the
MusicPlaylistwith one or more
MusicIds, which represent your music tracks.
- Use the provided methods to control the music playback, such as
When enabled, the
MusicPlayer automatically advances to the next music track in the playlist after the current one finishes playing. This feature is particularly useful for creating seamless music experiences in games and applications.
MusicPlayer supports smooth cross-fading between music tracks when auto-advance is enabled, and the fade-in and fade-out durations are greater than 0. This ensures a seamless transition between different tracks in the playlist.
MusicPlayer allows you to easily manage the
MusicPlaylist by providing methods to add, remove, and insert music tracks. You can organize your playlist to play tracks in a specific order, or shuffle the playlist for a randomized music experience.
MusicPlayer provides event handling for various playback states, allowing you to respond to specific events during music playback. For example, you can register callbacks for when a track finishes playing, starts fading in or out, or encounters an error.
Muting and Unmuting
You can mute and unmute the
MusicPlayer to control the audio output without stopping the music playback. This feature is useful for implementing in-game volume controls and dynamic audio adjustments.
Persistence Across Scenes
MusicPlayer can be configured to persist between scene changes using the
DontDestroyOnSceneChange setting. This ensures that the music playback remains uninterrupted as players transition between different scenes.
// Create a new MusicPlayer component
MusicPlayer musicPlayer =
// Customize the settings
// Add music tracks to the playlist
// Play the music
musicPlayer.Play();Code language: C# (cs)
For more detailed information, please refer to the official API documentation available at https://api.doozyui.com.