我正在使用SplitViewController和Swift-YouTube-Player在应用中播放YouTube视频。它在iPhone上完美运行但在iPad上无效。
在iPhone上,当我触发播放器时,它会全屏打开MPMoviewPLayer并播放视频,但在iPad上它会在后台播放我认为,我可以播放视频的声音,但视频不会出现。< / p>
我无法找到解决方案。你有什么建议让玩家在splitviewcontroller下玩吗?
这是触发代码
timer = NSTimer.scheduledTimerWithTimeInterval(4, target: self, selector: "alertVideo", userInfo: nil, repeats: false)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "doneButton", name: UIWindowDidBecomeHiddenNotification, object:nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "startVideo", name: UIWindowDidBecomeVisibleNotification , object:nil)
let a = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
a.dimBackground = true
collectionView.deselectItemAtIndexPath(indexPath, animated: true)
let cell = collectionView.cellForItemAtIndexPath(indexPath)
let circleView : YouTubePlayerView? = (cell!.contentView.viewWithTag(10)) as? YouTubePlayerView
circleView?.play()
这是我使用的YouTubePLayer: https://github.com/gilesvangruisen/Swift-YouTube-Player
答案 0 :(得分:0)
解决了这个问题。
我用YKMediaPlayerKit更改了库。我拿着玩家的实例并展示它:
let youtube : YKYouTubeVideo = YKYouTubeVideo()
var videoArray : [String] = []
self.youtube.contentURL = (NSURL(string: "http://www.youtube.com/watch?v=" + videoArray[indexPath.row]))
youtube.parseWithCompletion({ (error) -> Void in
if self.youtube.videos["hd720"] != nil {
let url = NSURL(string: (self.youtube.videos["hd720"] as! String))
self.timer.invalidate()
MBProgressHUD.hideHUDForView(self.view, animated: true)
let player = MPMoviePlayerViewController()
player.moviePlayer.contentURL = url
self.presentMoviePlayerViewControllerAnimated(player)
player.moviePlayer.play()
} else if self.youtube.videos["medium"] != nil {
let url = NSURL(string: (self.youtube.videos["medium"] as! String))
self.timer.invalidate()
MBProgressHUD.hideHUDForView(self.view, animated: true)
let player = MPMoviePlayerViewController()
player.moviePlayer.contentURL = url
self.presentMoviePlayerViewControllerAnimated(player)
player.moviePlayer.play()
} else if self.youtube.videos["small"] != nil {
let url = NSURL(string: (self.youtube.videos["small"] as! String))
self.timer.invalidate()
MBProgressHUD.hideHUDForView(self.view, animated: true)
let player = MPMoviePlayerViewController()
player.moviePlayer.contentURL = url
self.presentMoviePlayerViewControllerAnimated(player)
player.moviePlayer.play()
} else {
let refreshAlert = UIAlertController(title: "Error", message: "Video Not Found!", preferredStyle: UIAlertControllerStyle.Alert)
presentViewController(refreshAlert, animated: true, completion: nil)
}
答案 1 :(得分:0)
我的视频仍然在iPhone上全屏播放,但它并没有在iPad上全屏播放。我必须在YouTubePlayer.swift中进行更改。如果您在此部分中将allowsInlineMediaPlayback更改为false:
fileprivate func buildWebView(_ parameters: [String: AnyObject]) {
webView = UIWebView()
webView.allowsInlineMediaPlayback = false
webView.mediaPlaybackRequiresUserAction = false
webView.delegate = self
webView.scrollView.isScrollEnabled = false
}
它现在将在iPad上全屏播放。或者至少在编写本文时它正在工作。我希望有所帮助。