home / skills / charleswiltgen / axiom / axiom-now-playing-musickit

This skill helps you rely on MusicKit to auto-update Now Playing info when playing Apple Music content, avoiding manual overrides.

npx playbooks add skill charleswiltgen/axiom --skill axiom-now-playing-musickit

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
3.7 KB
---
name: axiom-now-playing-musickit
description: MusicKit Now Playing integration patterns. Use when playing Apple Music content with ApplicationMusicPlayer and understanding automatic vs manual Now Playing info updates.
license: MIT
---

# MusicKit Integration (Apple Music)

**Time cost**: 5-10 minutes

## Key Insight

**MusicKit's ApplicationMusicPlayer automatically publishes to MPNowPlayingInfoCenter.** You don't need to manually update Now Playing info when playing Apple Music content.

## What's Automatic

When using `ApplicationMusicPlayer`:
- Track title, artist, album
- Artwork (Apple's album art)
- Duration and elapsed time
- Playback rate (playing/paused state)

The system handles all MPNowPlayingInfoCenter updates for you.

## What's NOT Automatic

- Custom metadata (chapter markers, custom artist notes)
- Remote command customization beyond standard controls
- Mixing MusicKit content with your own content

## GOOD Code (MusicKit Content)

```swift
import MusicKit

@MainActor
class MusicKitPlayer {
    private let player = ApplicationMusicPlayer.shared

    func play(song: Song) async throws {
        // ✅ Just play - MPNowPlayingInfoCenter updates automatically
        player.queue = [song]
        try await player.play()

        // ❌ DO NOT manually set nowPlayingInfo here
        // MPNowPlayingInfoCenter.default().nowPlayingInfo = [...] // WRONG!
    }
}
```

## Hybrid Apps (Own Content + Apple Music)

If your app plays both Apple Music and your own content:

```swift
import MusicKit

@MainActor
class HybridPlayer {
    private let musicKitPlayer = ApplicationMusicPlayer.shared
    private var avPlayer: AVPlayer?
    private var currentSource: ContentSource = .none

    enum ContentSource {
        case none
        case appleMusic      // MusicKit handles Now Playing
        case ownContent  // We handle Now Playing
    }

    func playAppleMusicSong(_ song: Song) async throws {
        // Switch to MusicKit
        avPlayer?.pause()
        currentSource = .appleMusic

        musicKitPlayer.queue = [song]
        try await musicKitPlayer.play()
        // ✅ MusicKit handles Now Playing automatically
    }

    func playOwnContent(_ url: URL) {
        // Switch to AVPlayer
        musicKitPlayer.pause()
        currentSource = .ownContent

        avPlayer = AVPlayer(url: url)
        avPlayer?.play()

        // ✅ Manually update Now Playing (Patterns 1-4)
        updateNowPlayingForOwnContent()
    }

    private func updateNowPlayingForOwnContent() {
        var nowPlayingInfo = [String: Any]()
        nowPlayingInfo[MPMediaItemPropertyTitle] = "My Track"
        // ... rest of manual setup from Patterns 1-4
        MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
    }
}
```

## Common Mistake

```swift
// ❌ WRONG - Overwrites MusicKit's automatic Now Playing data
func playAppleMusicSong(_ song: Song) async throws {
    try await ApplicationMusicPlayer.shared.play()

    // ❌ This clears MusicKit's Now Playing info!
    var nowPlayingInfo = [String: Any]()
    nowPlayingInfo[MPMediaItemPropertyTitle] = song.title
    MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}

// ✅ CORRECT - Let MusicKit handle it
func playAppleMusicSong(_ song: Song) async throws {
    try await ApplicationMusicPlayer.shared.play()
    // That's it! MusicKit publishes Now Playing automatically.
}
```

## When to Use Manual Updates with MusicKit

Only override MPNowPlayingInfoCenter if:
- You're mixing in additional metadata (e.g., podcast chapter markers)
- You're displaying custom content alongside Apple Music
- You have a specific reason to replace MusicKit's automatic behavior

**Default**: Let MusicKit manage Now Playing automatically.

## Resources

**Skills**: axiom-now-playing, axiom-now-playing-carplay

Overview

This skill documents MusicKit Now Playing integration patterns for TypeScript/modern xOS development using ApplicationMusicPlayer. It explains when the system automatically updates MPNowPlayingInfoCenter and when you must manage Now Playing manually. The guidance focuses on safe patterns for pure Apple Music playback and hybrid apps mixing your own content with MusicKit.

How this skill works

ApplicationMusicPlayer publishes Now Playing metadata to MPNowPlayingInfoCenter automatically when playing Apple Music content: title, artist, album, artwork, duration, elapsed time, and playback state. Manual updates are only required for custom metadata or when your app plays non-MusicKit content (for example, AVPlayer streams). The skill contrasts correct usage (let MusicKit own Now Playing) with the common mistake of overwriting MusicKit data.

When to use it

  • When playing Apple Music tracks with ApplicationMusicPlayer — rely on automatic Now Playing updates.
  • When you need to add custom metadata (chapter markers, custom annotations) alongside MusicKit data.
  • When your app mixes Apple Music with your own media playback — manage Now Playing for your own content.
  • When customizing remote command behavior beyond standard play/pause (use remote command APIs instead).
  • When integrating with CarPlay or external Now Playing consumers and you need consistent behavior across sources.

Best practices

  • Do not set MPNowPlayingInfoCenter.nowPlayingInfo after starting ApplicationMusicPlayer playback; it will overwrite MusicKit’s data.
  • Pause ApplicationMusicPlayer before switching to your own AVPlayer, and then set nowPlayingInfo for your custom content.
  • Only override Now Playing if you must add nonstandard metadata; prefer using MusicKit for core playback info.
  • Use clear source tracking in your player (e.g., enum for appleMusic vs ownContent) to decide who updates Now Playing.
  • Test hybrid flows thoroughly to ensure commands and artwork remain correct when toggling sources.

Example use cases

  • A music app that streams Apple Music songs: let ApplicationMusicPlayer handle Now Playing automatically.
  • A podcast app that also plays Apple Music tracks: use MusicKit for Apple Music playback and manually update Now Playing for podcast episodes with chapter markers.
  • A media player that alternates between AVPlayer streams and MusicKit songs: pause the other player and set nowPlayingInfo only for non-MusicKit streams.
  • CarPlay integration where Apple Music tracks should supply system metadata while custom streams provide additional fields.

FAQ

What metadata does MusicKit automatically publish?

MusicKit publishes title, artist, album, artwork, duration, elapsed time, and playback rate when using ApplicationMusicPlayer.

Can I mix MusicKit and manual Now Playing updates?

Yes, but keep them separated by source: let MusicKit manage Apple Music content and only set MPNowPlayingInfoCenter for your own media after pausing MusicKit.