gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 02:02:11 29 4
gpt4 key购买 nike

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

这是我的媒体选择器 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)

}



}

最佳答案

MPMediaPickerControllerUIViewController .
所以不要在屏幕上显示 currentViewController.view.addSubview(picker.view) .

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

在委托(delegate)方法中,当您调用 mediaPicker.dismissViewControllerAnimated(true, completion: nil) , 你还有 mediaPicker.view作为 currentViewController.view 的 subview .

关于ios媒体选择器仅显示白屏/卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30568933/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com