Search code examples
iosiphoneswiftaudiompmediapickercontroller

ios media picker shows only white screen/stuck


So I'm currently making an music application for my project, and it allows user to create their own music playlist. However when I clicked on show media picker button it only shows white screen, It doesn't happen when the view that contains media picker is Initial View Controller. But when I come from segue to that media picker view controller it only shows white screen if I tapped the button.

here is the code from my media picker view controller:

var myMusicPlayer: MPMusicPlayerController?

override func viewDidLoad() {
    super.viewDidLoad()

    var buttonPickAndPlay = UIButton.buttonWithType(.System) as? UIButton

    if let pickAndPlay = buttonPickAndPlay{

        pickAndPlay.frame = CGRect(x: 0, y: 0, width: 200, height: 37)
        pickAndPlay.center = CGPoint(x: view.center.x, y: view.center.y - 50)
        pickAndPlay.setTitle("Pick and Play", forState: .Normal)
        pickAndPlay.addTarget(self,
            action: "displayMediaPickerAndPlayItem",
            forControlEvents: .TouchUpInside)

        view.addSubview(pickAndPlay)
    }
}

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

func displayMediaPickerAndPlayItem(){

    var mediaPicker = MPMediaPickerController(mediaTypes: .AnyAudio)

    if let picker = mediaPicker{

        println("Successfully instantiated a media picker")
        picker.delegate = self
        picker.allowsPickingMultipleItems = true
        picker.showsCloudItems = true
        picker.prompt = "Pick a song please..."
        self.view.addSubview(picker.view)
        presentViewController(picker, animated: true, completion: nil)
    } else {
        println("Could not instantiate a media picker")
    }

}

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

        println("Media Picker returned")

        /* Instantiate the music player */

       myMusicPlayer = MPMusicPlayerController()

        if let player = myMusicPlayer{
            player.beginGeneratingPlaybackNotifications()

            /* Get notified when the state of the playback changes */
            NSNotificationCenter.defaultCenter().addObserver(self,
                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 */
            NSNotificationCenter.defaultCenter().addObserver(self,
                selector: "nowPlayingItemIsChanged:",
                name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification,
                object: nil)

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

            /* Start playing the items in the collection */
            player.setQueueWithItemCollection(mediaItemCollection)
            User.mdci = mediaItemCollection
            player.play()

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

        }



}

Solution

  • MPMediaPickerController is a UIViewController.
    So don't show it in screen with currentViewController.view.addSubview(picker.view).

    When you show it with presentViewController(picker, animated: true, completion: nil), it's the correct way.

    In the delegate methods, when you were calling mediaPicker.dismissViewControllerAnimated(true, completion: nil), you still had mediaPicker.view as a subview of currentViewController.view.