SystemMusicPlayer PRO
The SystemMusicPlayer module provides control over the system-level music player. It is built on top of MPMusicPlayerController.systemMusicPlayer, which shares the playback queue and playback state with the system Music app.
This module allows scripts to:
- Control system music playback
- Set and manage the playback queue
- Control playback time, rate, repeat, and shuffle modes
- Observe playback state changes
- Access information about the currently playing item
Important notes:
- The user must grant media library access.
- Playing Apple Music content may require an active subscription.
- All operations affect the global system music player.
Data Models
PlaybackState
Represents the current playback state.
Descriptions:
stopped: Playback is stoppedplaying: Currently playingpaused: Pausedinterrupted: Interrupted by system events (e.g. phone call)seekingForward: Fast-forwardingseekingBackward: Rewinding
RepeatMode
Defines the repeat mode.
Descriptions:
none: No repeatone: Repeat current itemall: Repeat entire queuedefault: Use system default
ShuffleMode
Defines the shuffle mode.
Descriptions:
off: Shuffle disabledsongs: Shuffle songsalbums: Shuffle albumsdefault: Use system default
NowPlayingItem
Represents the currently playing item.
Field descriptions:
persistentID: Unique local media identifiertitle: Song titleplaybackDuration: Duration in secondsplaybackStoreID: Apple Music Store ID (if available)- Other fields are similar to
MediaLibrary.Item
Setting the Playback Queue
setQueueByStoreIDs
Sets the playback queue using Apple Music Store IDs.
Parameters:
storeIDs: Array of Apple Music Store IDsstartItemID: Optional ID of the item to start playback fromstartTime: Optional starting playback time in seconds
Example:
setQueueByPersistentIDs
Sets the playback queue using local media persistentID values.
Example:
Recommended usage:
- Combine with
MediaLibrary.getSongs()to build a queue from local content.
Playback Controls
prepare
Prepares the current playback queue.
Typically called before play().
play
Starts playback.
pause
Pauses playback.
stop
Stops playback.
skipToNextItem
Skips to the next item in the queue.
skipToPreviousItem
Skips to the previous item.
seek
Seeks to a specific time in the current item.
Parameter:
to: Time in seconds
Example:
setCurrentPlaybackTime
Sets the current playback time.
setCurrentPlaybackRate
Sets the playback rate.
Example:
setRepeatMode
Sets the repeat mode.
setShuffleMode
Sets the shuffle mode.
Playback State Accessors
indexOfNowPlayingItem
Returns the index of the current item in the queue.
getNowPlayingItem
Returns the currently playing item.
Example:
getPlaybackState
Returns the current playback state.
getCurrentPlaybackTime
Returns the current playback time in seconds.
getCurrentPlaybackRate
Returns the current playback rate.
getRepeatMode
Returns the current repeat mode.
getShuffleMode
Returns the current shuffle mode.
Event Handling
EventType
addEventListener
Adds an event listener.
Example:
removeEventListener
Removes an event listener.
Best Practices
- After setting a queue, call
prepare()beforeplay(). - Listen to
nowPlayingItemDidChangeto update UI reactively. - Use
persistentIDas a stable identifier rather than relying on queue index. - Avoid frequently resetting the queue with
setQueue..., as it replaces the entire playback queue. - Remember that this is a global system music player; changes affect system-wide playback.
