I am trying to allow the user to pick a song from their library and play it across different view controllers. I am able to pick songs currently and play them, but I don't know how to play them in the background. Thanks!

class ViewController: UIViewController,
MPMediaPickerControllerDelegate, AVAudioPlayerDelegate {

    var myMusicPlayer: MPMusicPlayerController?
    var mediaPicker: MPMediaPickerController?
    var backgroundMusicPlayer:AVAudioPlayer = AVAudioPlayer()

    @IBAction func musicBtn(sender: AnyObject) {

    override func viewDidLoad() {

        // Do any additional setup after loading the view.


    override func didReceiveMemoryWarning() {
        // Dispose of any resources that can be recreated.

    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.

    func musicPlayerStateChanged(notification: NSNotification){

        print("Player State Changed")

        /* Let's get the state of the player */
        let stateAsObject =
                as? NSNumber

        if let state = stateAsObject{

            /* Make your decision based on the state of the player */
            switch MPMusicPlaybackState(rawValue: state.integerValue)!{
            case .Stopped:
                /* Here the media player has stopped playing the queue. */
            case .Playing:
                /* The media player is playing the queue. Perhaps you
                 can reduce some processing that your application
                 that is using to give more processing power
                 to the media player */
            case .Paused:
                /* The media playback is paused here. You might want
                 to indicate by showing graphics to the user */
            case .Interrupted:
                /* An interruption stopped the playback of the media queue */
            case .SeekingForward:
                /* The user is seeking forward in the queue */
                print("Seeking Forward")
            case .SeekingBackward:
                /* The user is seeking backward in the queue */
                print("Seeking Backward")


    func nowPlayingItemIsChanged(notification: NSNotification){

        print("Playing Item Is Changed")

        let key = "MPMusicPlayerControllerNowPlayingItemPersistentIDKey"

        let persistentID =
            notification.userInfo![key] as? NSString

        if let id = persistentID{
            /* Do something with Persistent ID */
            print("Persistent ID = \(id)")


    func volumeIsChanged(notification: NSNotification){
        print("Volume Is Changed")
        /* The userInfo dictionary of this notification is normally empty */

    func mediaPicker(mediaPicker: MPMediaPickerController,
                     didPickMediaItems mediaItemCollection: MPMediaItemCollection){

        print("Media Picker returned")

        /* Instantiate the music player */

        myMusicPlayer = MPMusicPlayerController()

        if let player = myMusicPlayer{


            /* Get notified when the state of the playback changes */
                                                             selector: "musicPlayerStateChanged:",
                                                             name: MPMusicPlayerControllerPlaybackStateDidChangeNotification,
                                                             object: nil)

            /* Get notified when the playback moves from one item
             to the other. In this recipe, we are only going to allow
             our user to pick one music file */
                                                             selector: "nowPlayingItemIsChanged:",
                                                             name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification,
                                                             object: nil)

            /* And also get notified when the volume of the
             music player is changed */
                                                             selector: "volumeIsChanged:",
                                                             name: MPMusicPlayerControllerVolumeDidChangeNotification,
                                                             object: nil)

            /* Start playing the items in the collection */

            /* Finally dismiss the media picker controller */
            mediaPicker.dismissViewControllerAnimated(true, completion: nil)



    func mediaPickerDidCancel(mediaPicker: MPMediaPickerController) {
        /* The media picker was cancelled */
        print("Media Picker was cancelled")
        mediaPicker.dismissViewControllerAnimated(true, completion: nil)

    func stopPlayingAudio(){


        if let player = myMusicPlayer{


    func displayMediaPickerAndPlayItem(){

        mediaPicker = MPMediaPickerController(mediaTypes: .AnyAudio)

        if let picker = mediaPicker{

            print("Successfully instantiated a media picker")
            picker.delegate = self
            picker.allowsPickingMultipleItems = true
            picker.showsCloudItems = true
            picker.prompt = "Pick a song please..."

            presentViewController(picker, animated: true, completion: nil)

        } else {
            print("Could not instantiate a media picker")


Question author Brandex07 | Source



You can use singleton to achieve it like this:

import AVFoundation

class MusicHelper {
    static let sharedHelper = MusicHelper()
    var audioPlayer: AVAudioPlayer?

    func playBackgroundMusic() {
        let pickedSong = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("userPickedSong", ofType: "mp3")!)
        do {
            audioPlayer = try AVAudioPlayer(contentsOfURL:aSound)
            audioPlayer!.numberOfLoops = -1
        } catch {
            print("Cannot play the file")

And you can use it like this:


This is just one example how you can do it. Actually there are more and there are already answers on StackOverflow. But with the way I showed you can call it in any class.

Answer author Tarvo-m%c3%a4esepp

Ask about this question here!