Playback
The IMusicPlayer interface provides full playback control for music tracks from the device library.
Playing a Track
Section titled “Playing a Track”var tracks = await _library.GetAllTracksAsync();await _player.PlayAsync(tracks[0]);Calling PlayAsync stops any currently playing track, loads the new one, and begins playback immediately.
Pause, Resume, and Stop
Section titled “Pause, Resume, and Stop”// Pause the current track_player.Pause();
// Resume from where it was paused_player.Resume();
// Stop completely — releases the track_player.Stop();Pause()has no effect if the player is not in thePlayingstate.Resume()has no effect if the player is not in thePausedstate.Stop()returns the player to theStoppedstate and releases the current track.
Seeking
Section titled “Seeking”// Seek to 1 minute 30 seconds_player.Seek(TimeSpan.FromMinutes(1.5));Reading Playback State
Section titled “Reading Playback State”// Current state: Stopped, Playing, or PausedPlaybackState state = _player.State;
// Currently loaded track (null if stopped)MusicMetadata? track = _player.CurrentTrack;
// Current position and total durationTimeSpan position = _player.Position;TimeSpan duration = _player.Duration;Events
Section titled “Events”StateChanged
Section titled “StateChanged”Raised whenever the playback state transitions:
_player.StateChanged += (sender, newState) =>{ Console.WriteLine($"Playback state: {newState}"); // e.g., Playing → Paused, Paused → Playing, Playing → Stopped};PlaybackCompleted
Section titled “PlaybackCompleted”Raised when a track finishes playing naturally (i.e., reaches the end). This is not raised when you call Stop() manually.
_player.PlaybackCompleted += (sender, args) =>{ Console.WriteLine("Track finished — play the next one?");};Disposing
Section titled “Disposing”IMusicPlayer implements IDisposable. Call Dispose() to stop playback and release all platform resources:
_player.Dispose();If you register the player as a singleton in DI, it will be disposed when the app shuts down.
Platform Details
Section titled “Platform Details”Android
Section titled “Android”- Playback uses
Android.Media.MediaPlayerwith content URIs from MediaStore. - Audio attributes are set to
ContentType.MusicwithUsage.Media. - Seeking uses millisecond precision.
- Playback uses
AVFoundation.AVAudioPlayerwith the track’sipod-library://asset URL. - The
AVAudioSessioncategory is set toPlaybackto support background audio (if configured). - Seeking uses second precision.