Displaying a Media Picker from Your App

Let users choose the music they want to play by displaying a media picker interface from within your app.


Adding a media-picker view controller to your app allows users to choose music items from their Apple Music library without leaving your app. You can configure the media picker to accept single or multiple items from the user.

Adopt the Protocol, and Create the System Music Player

To enable your app to respond to user input, adopt the MPMediaPickerControllerDelegate protocol.

class ViewController: UIViewController, MPMediaPickerControllerDelegate {

Add the Media-Picker View Controller

Set the media-picker view controller as a popover presentation controller when you target an iPad. Set the source view and delegate, and present the media picker. In this example, the media picker allows the user to select multiple items, and the source view is a button object.

@IBAction func selectSongs(_ sender: UIButton) {
    let controller = MPMediaPickerController(mediaTypes: .music)
    controller.allowsPickingMultipleItems = true
    controller.popoverPresentationController?.sourceView = sender
    controller.delegate = self
    present(controller, animated: true)

Add a Select Music Button, and Target the Previously Created Function

To create a button that gives the user access to their Apple Music library from within your app:

  1. Add a UIButton object, and name it Select Songs.

  2. Attach restraints to center the button in the display.

  3. Control-drag from the new button object to View Controller in the Outline view, and select selectSongs: from the menu that appears.

The Xcode Outline View and Canvas displayed side-by-side.

The Select Songs button is the source view for the media-picker view controller, which is presented modally. It displays the user's Apple Music library, allowing them to choose the music they wish to play.

Implement the Protocol Methods

Implement two methods from the MPMediaPickerControllerDelegate protocol: mediaPicker(_:didPickMediaItems:) and mediaPickerDidCancel(_:). Inside these methods, add code to handle the user's choice of items to create a music queue, and code to dismiss the view controller. The following code shows how to set the music player queue, begin playing the user's choices, and dismiss the view controller if the user doesn't choose any media items:

func mediaPicker(_ mediaPicker: MPMediaPickerController,
                 didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
    // Get the system music player.
    let musicPlayer = MPMusicPlayerController.systemMusicPlayer
    musicPlayer.setQueue(with: mediaItemCollection)
    mediaPicker.dismiss(animated: true)
    // Begin playback.

func mediaPickerDidCancel(_ mediaPicker: MPMediaPickerController) {
    mediaPicker.dismiss(animated: true)

See Also

Media Player User Interface

class MPMediaPickerController

A specialized view controller that provides a graphical interface for selecting media items.

class MPVolumeView

A slider control used to set the system audio output volume, and a button for choosing the audio output route.

Global Volume Setting Methods

Global functions for setting the volume alert window status.