Add the ability to find and play media items including songs, audio podcasts, and audio books from within your app.
- iOS 2.0+
- macOS 10.12.1+
- tvOS 9.0+
Use the MediaPlayer framework to enable users to play music, audio podcasts, and audio books from within your app. For example, you give users the option to play their own music from within your app while they try to complete a particular game level. Through MediaPlayer, your app can access media items synced from iTunes, iCloud Music Library, and Apple Music Subscription. It can also access non-Apple media items that users have stored on their devices. After being granted access to a user's media items, you can play them and add more to the user's device. Library access is read-only.
You can incorporate one of two types of media player into your app, depending on your goal. Use a system player if you want changes made in your app to also affect the Music app. Use an application player if you want to leave the Music app as is while playing audio. After deciding on the player, use media queries to retrieve media items that will populate the player.
Apps that play audio or video content receive player events to start and stop playback, change tracks, and even rate an item. All media apps should support these events.
To receive player events, do the following:
• Use the shared
MPRemoteCommandCenter object to register handlers for the events you wish to handle and to disable the events you are not interested in receiving.
• Begin playing audio. Your app must be the Now Playing app. An app does not receive remote control events until it begins playing audio. Test that your app is properly receiving and handling remote control events with Control Center, which you access by swiping up from the bottom edge of your screen. These controls send remote control events to the app that is currently or was most recently playing audio. You can also access the playback controls from the lock screen of the device.