ios媒体选择器只显示白屏/卡住

时间:2015-06-01 08:32:41

标签: ios iphone swift audio mpmediapickercontroller

所以我正在为我的项目制作音乐应用程序,它允许用户创建自己的音乐播放列表。但是当我点击s 媒体选择器按钮时它只显示白屏,当包含媒体选择器的视图是初始视图控制器 <时,不会发生这种情况/ strong>即可。但是当我从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)

        }



}

2 个答案:

答案 0 :(得分:0)

MPMediaPickerControllerUIViewController 因此,请勿使用currentViewController.view.addSubview(picker.view)在屏幕上显示该内容。

当您使用presentViewController(picker, animated: true, completion: nil)进行展示时,它是正确的方式。

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

答案 1 :(得分:0)

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