An object used to play audio media items from the device's Music app library.
SDKs
- iOS 3.0+
- Mac Catalyst 13.0+
Framework
- Media Player
Declaration
class MPMusicPlayerController : NSObject
Overview
Create an instance of a music player to play media items in your app. There are two types of music player:
An application music player plays music locally within your app. It is not aware of the Music app’s now-playing item, nor does it affect the Music app’s state. There are two application music players:
application
andMusic Player application
. The application queue player provides greater control over the contents of the queue and is the preferred player.Queue Player The system music player employs the built-in Music app on your behalf. On instantiation, it takes on the current Music app state, such as the identification of the now-playing item. If a user switches away from your app while music is playing, that music continues to play. The Music app then has your music player’s most recently-set repeat mode, shuffle mode, playback state, and now-playing item.
Creating a new instance of MPMusic
and not specifying the player type returns a system music player.
Important
You must use a music player only on your app’s main thread.
Home Sharing and System Music Players
The built-in Music and Videos apps can play media from shared libraries using Home Sharing. However, third-party apps using the Media Player framework still have access only to the device Music library. This means that your app cannot display the title of a home-shared song in your user interface. Specifically, if the Music app is playing a home-shared song, and you are using an Music music player, the value of the now
property of your music player is nil
. Other playback information is available, however, when playing shared media. For example, the system does update the value of the playback
property when the system music player is playing a shared item.
Using Remote-Control Events with a Music Player
Users can initiate audio playback commands through an external headset or accessory. These commands are sent to the app as remote-control events. To receive remote-control events in your app, you must use an application music player. If you use the system music player, your app uses the Music app to play audio, which means that the Music app is the “now playing” app. The Music app receives the remote control events, not your app. However, the Music app responds to remote control events on your behalf. For example, if your app plays audio using the system music player, and you switch from your app to the iOS device’s Now Playing controls, the controls work as expected. That is, you can play or pause audio or skip to the next and previous items.