Contents

Playing audio using the built-in music player

Create a media player inside your app to play audio from the user’s media library.

Overview

There are many reasons you might want to let users play music from their music library inside your app. For example, you might want them to listen to their own music while playing a particular level in your game. Add a built-in music player to quickly play audio tracks from the user’s music library while your app is running.

Specify a usage description string

To give the user control over their personal data, apps must obtain permission to access the person’s music library. The first time your app tries to access someone’s media library, the system displays an alert that prompts them to grant or deny access to their music. The alert message includes your app’s usage-description string, which is a text description of how you plan to use the music.

To specify the usage-description string for your app, add the NSAppleMusicUsageDescription key to your app’s Info.plist file. Set the value of the key to your description of how you intend to use the music. Make your description as concise as possible, and no more than one or two sentences. For example, the usage-description string for a game might explain that its soundtrack incorporates the person’s music at different points in the gameplay.

Users can deny access to their media library, so design your app to run even without that access. If your app requires music, devise a backup plan that doesn’t involve asking the person to reconsider their decision. For example, include some default music in your app’s bundle.

Configure the media player contents

To play the user’s music, your app needs an MPMusicPlayerController object. The Media Player framework defines several player controllers, each of which serves a different purpose. To manage a queue of songs that’s specific to your app, create an application queue player, and fill it with one or more songs.

To access the songs in the user’s music library, construct an MPMediaQuery object for the items you want. The class has constructors that let you request the user’s playlists, podcasts, songs, and other media content. To get the current songs, use the songs() constructor, as shown in the following example.

// Get the music player.
let musicPlayer = MPMusicPlayerApplicationController.applicationQueuePlayer
// Add a playback queue containing all songs on the device.
musicPlayer.setQueue(with: .songs())

If you don’t want all the items in a collection, filter the list by adding one or more predicates to your MPMediaQuery. For example, you might specify a predicate to match only songs with a specific word in the title. For more information about configuring media queries, see MPMediaQuery.

Play and pause the music

After you add one or more songs to your music player, begin playback by calling the play() method. Media players adopt the MPMediaPlayback protocol and support all of the normal methods to play, pause, and stop playback. You can also use the methods of that protocol to change the playback speed and seek within a media item.

For more information about controlling the playback of your items, see MPMediaPlayback.

See Also

Related Documentation

Built-in music playback