Skip to content

Copying Tracks

The IMediaLibrary.CopyTrackAsync method allows you to copy a music file from the device library to your app’s storage.

var destPath = Path.Combine(FileSystem.AppDataDirectory, "CopiedMusic", "track.m4a");
var success = await _library.CopyTrackAsync(track, destPath);
if (success)
Console.WriteLine($"Copied to {destPath}");
else
Console.WriteLine("Copy failed — track may be DRM-protected");

The method creates any missing parent directories automatically.

Before attempting to copy, check whether the track has a valid ContentUri:

if (string.IsNullOrEmpty(track.ContentUri))
{
// DRM-protected — cannot be copied (iOS only)
Console.WriteLine("This track cannot be copied.");
return;
}
var success = await _library.CopyTrackAsync(track, destinationPath);
  • Copies are performed by reading from the ContentResolver input stream and writing to the destination file.
  • All locally stored music files can be copied.
  • The copied file retains its original format (MP3, M4A, OGG, etc.).
  • Copies are performed using AVAssetExportSession with the AppleM4A preset.
  • The exported file is always in M4A format regardless of the original encoding.
  • DRM restrictions apply:
Track SourceCan Copy?Notes
iTunes purchases (DRM-free)Full access via AssetURL
Locally synced from computerFull access via AssetURL
Apple Music subscriptionAssetURL is null; track is DRM-protected
iTunes Match (cloud)⚠️Only if downloaded to device

CopyTrackAsync returns false rather than throwing for expected failures (DRM, missing asset). Unexpected I/O errors are also caught and return false:

try
{
var success = await _library.CopyTrackAsync(track, destPath);
if (!success)
{
// DRM-protected or export failed
await DisplayAlertAsync("Error", "Cannot copy this track.", "OK");
}
}
catch (Exception ex)
{
// Unexpected error (e.g., permissions revoked mid-operation)
await DisplayAlertAsync("Error", $"Error: {ex.Message}", "OK");
}