MusicPlayer
The 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.
Getting Started
Follow these steps to set up and use the MusicPlayer
in your project:
- Attach the
MusicPlayer
component to a GameObject in your scene. - Create a
MusicPlaylist
asset, and associate it with theMusicPlayer
by setting thePlaylist
field in the inspector. - Customize the various properties of the
MusicPlayer
to 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
MusicPlaylist
with one or moreMusicIds
, which represent your music tracks. - Use the provided methods to control the music playback, such as
Play()
,Stop()
,Pause()
, andUnPause()
.
Features
Auto-Advance
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.
Cross-Fading
The 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.
Playlist Management
The 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.
Event Handling
The 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
The 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.
Code Example
// Create a new MusicPlayer component
MusicPlayer musicPlayer =
MusicPlayer.Get();
// Customize the settings
musicPlayer
.SetAutoAdvance(true)
.SetCrossFade(true)
.SetCrossFadeDuration(2f)
.SetDonDestroyOnSceneChange(true);
// Add music tracks to the playlist
musicPlayer
.AddToPlaylist("LibraryName", "MusicName1")
.AddToPlaylist("LibraryName", "MusicName2");
// 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.