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, and create a global instance of the music player.

class ViewController: UIViewController, MPMediaPickerControllerDelegate {

    //Creates a global instance of the system music player
    var myMediaPlayer = MPMusicPlayerController.systemMusicPlayer

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 UIButton object.

@IBAction func chooseSongsButtonPressed(_ sender: UIButton) {
    let myMediaPickerVC = MPMediaPickerController(mediaTypes:
    myMediaPickerVC.allowsPickingMultipleItems = true
    myMediaPickerVC.popoverPresentationController?.sourceView = sender
    myMediaPickerVC.delegate = self
    self.present(myMediaPickerVC, animated: true, completion: nil)

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 Music.

  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 chooseSongsButtonPressed: from the menu that appears.

The Xcode Outline View and Canvas displayed side-by-side. A black box is centered along the top that contains the chooseSongsButtonPressed function.

The Select Music 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) {
    myMediaPlayer.setQueue(with: mediaItemCollection)
    mediaPicker.dismiss(animated: true, completion: nil)

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

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.