ios 媒体选择器只显示白色 screen/stuck

ios media picker shows only white screen/stuck

所以我目前正在为我的项目制作一个音乐应用程序,它允许用户创建自己的音乐播放列表。然而,当我点击 show media picker button 它只显示白屏,当包含媒体选择器的视图是 Initial 时不会发生查看控制器。但是当我从 segue 转到那个媒体选择器视图控制器时,如果我点击按钮,它只会显示白屏。

这是我的媒体选择器视图控制器中的代码:

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)

        }



}

MPMediaPickerControllerUIViewController.
所以不要在 currentViewController.view.addSubview(picker.view).

的屏幕上显示它

当你用presentViewController(picker, animated: true, completion: nil)显示时,这是正确的方式。

在委托方法中,当您调用 mediaPicker.dismissViewControllerAnimated(true, completion: nil) 时,您仍然有 mediaPicker.view 作为 currentViewController.view 的子视图。

在Info.Plist中添加隐私以解决此问题