Querying Music
The IMediaLibrary interface provides methods to retrieve music metadata from the device library.
Get All Tracks
Section titled “Get All Tracks”Returns every music track on the device:
var tracks = await _library.GetAllTracksAsync();
foreach (var track in tracks){ Console.WriteLine($"{track.Title} by {track.Artist} ({track.Duration:mm\\:ss})");}Search Tracks
Section titled “Search Tracks”Search by title, artist, or album name:
var results = await _library.SearchTracksAsync("beethoven");
foreach (var track in results){ Console.WriteLine($"{track.Title} - {track.Album}");}The search is case-insensitive and matches partial strings against the title, artist, and album fields.
MusicMetadata
Section titled “MusicMetadata”Each track is represented by a MusicMetadata record with the following properties:
| Property | Type | Description |
|---|---|---|
Id | string | Platform-specific unique identifier. On Android, this is the MediaStore row ID. On iOS, it is the MPMediaItem persistent ID. |
Title | string | The track title. |
Artist | string | The artist or performer. |
Album | string | The album name. |
Genre | string? | The genre, or null if unavailable. |
Duration | TimeSpan | The playback duration. |
AlbumArtUri | string? | URI to album artwork. Available on Android via MediaStore; null on iOS where artwork is accessed through MPMediaItem.Artwork. |
IsExplicit | bool? | Whether the track is marked as explicit content. iOS only via MPMediaItem.IsExplicitItem; always null on Android. |
ContentUri | string | URI used for playback and file operations. On Android, this is a content:// URI. On iOS, this is an ipod-library:// asset URL. Empty for DRM-protected Apple Music subscription tracks. |
ContentUri and DRM
Section titled “ContentUri and DRM”The ContentUri property is critical for understanding what operations are available for a track:
var tracks = await _library.GetAllTracksAsync();
foreach (var track in tracks){ if (string.IsNullOrEmpty(track.ContentUri)) { // DRM-protected Apple Music track // Cannot be played via AVAudioPlayer or copied Console.WriteLine($"⚠️ {track.Title} - DRM protected, playback/copy unavailable"); } else { // Locally synced or purchased track — full access Console.WriteLine($"✅ {track.Title} - available for playback and copy"); }}Platform Details
Section titled “Platform Details”Android
Section titled “Android”- Tracks are queried from
MediaStore.Audio.MediaviaContentResolver. - Only items flagged as music (
IsMusic != 0) are returned — ringtones, notifications, podcasts, and videos are excluded. - Results are sorted alphabetically by title.
- Tracks are queried using
MPMediaQueryfrom theMediaPlayerframework. - Only items with
MPMediaType.Musicare returned — podcasts, audiobooks, and movies are excluded.