检测以纵向或横向全屏播放的视频 [英] Detect Video playing full screen in Portrait or landscape
问题描述
我参与了这两天多。我无法得到任何解决方案。
I engaged into this more than two days. I couldn't get any solution please.
我要求视频应该是肖像内联,全屏,风景。我的问题是如何识别视频正在播放全屏风景或人像。我已经实现了 viewWillTransitionToSize
方法。但是 AVPlayer
有全屏箭头按钮。如何识别用户点击该选项。
I have requirement is video should play Portrait inline , full screen , landscape. My problem here is how to identify the video is playing full-screen landscape or Portrait. I have implemented viewWillTransitionToSize
method. But AVPlayer
has full-screen arrow button. How I can identify user clicked that option.
第二个要求,一旦视频完成,在视频节目重放或下一个或上一个选项之上创建视图。
The second requirement , once video complete, create view on top of video show replay or next or previous option.
这是我的代码;
if videoCellVal == nil {
videoCellVal = videoCell
comPlayerControl = AVPlayerViewController()
if let player = comPlayerControl {
let videoURL: String = "http://cdnapi.kaltura.com/p/11/sp/11/playManifest/entryId/"+selectedSubmission.transcodeRefId+"/format/applehttp/protocol/http/a.m3u8"
let playerItem = AVPlayerItem(URL: NSURL(string: videoURL)! )
commmentPlayer = AVPlayer(playerItem: playerItem)
player.player = commmentPlayer
player.view.frame = videoCell.frame
player.view.sizeToFit()
player.showsPlaybackControls = true
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:",
name: AVPlayerItemDidPlayToEndTimeNotification, object: playerItem)
videoCell.addSubview(player.view)
}
}
func playerDidFinishPlaying(note: NSNotification) {
print("Video Finished")
let DynamicView=UIView(frame: CGRectMake(100, 200, 100, 100))
DynamicView.backgroundColor=UIColor.greenColor()
DynamicView.layer.cornerRadius=25
DynamicView.layer.borderWidth=2
DynamicView.bringSubviewToFront(self.view)
self.view.addSubview(DynamicView)
}
推荐答案
用于检测全屏或视频大小改变你可以使用KVO作为AVPlayerViewController,代码可以是这样的:
For the detecting the Full screen or video size change you can use KVO for the AVPlayerViewController, code can be like this:
[comPlayerControl .addObserver(self, forKeyPath:"videoBounds" , options: NSKeyValueObservingOptions.New, context: nil)]
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
print("KeyPath \(keyPath)")
if keyPath == "videoBounds" {
print("New Video Bounds \(change)")
}
}
在更改
字典你可以检查视频帧/边界变化的变化
In the change
dictionary you can check the change in the video frame/bounds change
对于你提到的轮换你可能不得不使用 viewWillTransitionToSize
或 UIDeviceOrientationDidChangeNotification
通知或您可以使用 UIDevice.currentDevice()检查当前方向。orientation
因为我没有找到任何可以检查正在进行的视频中的更改的方法
For the rotation as you mentioned you may have to use viewWillTransitionToSize
or UIDeviceOrientationDidChangeNotification
notification or you can check the current orientation using UIDevice.currentDevice().orientation
as i didn't find any method which can check the change in the ongoing video
如果您需要检测视频方向,您可以查看以下链接:https://stackoverflow.com/a/25833399/4557505 ,以及该链接中的其他答案
Still if you need to detect video orientation you may check this link : https://stackoverflow.com/a/25833399/4557505 , and other answer in that link
要重复播放视频,您可以在代码中将时间设置为零:
For the repeat of the video you can set the time to zero like this in your code:
func playerDidFinishPlaying(notification: NSNotification) {
if let item = notification.object as? AVPlayerItem {
item.seekToTime(kCMTimeZero)
//you may directly play the video or can do further processing
}
}
在项目结尾处播放队列中的项目,您可以尝试使用 commmentQueuePlayer.advanceToNextItem()
to play item in the queue at the end of the item you may try with commmentQueuePlayer.advanceToNextItem()
您可以尝试在 contentOverlayView
let topView = UIView(frame: CGRectMake(0,0, comPlayerControl.view.bounds.width,44))
topView.backgroundColor = UIColor ( red: 0.5, green: 0.5, blue: 0.5, alpha: 0.379 )
let btnNext = UIButton(frame:CGRectMake(0,0,80,44))
btnNext.setTitle("Next", forState:.Normal)
btnNext.addTarget(self, action:"playNext", forControlEvents:.TouchUpInside)
btnNext.userInteractionEnabled = true
btnNext.enabled = true
topView.addSubview(btnNext)
comPlayerControl.contentOverlayView?.addSubview(topView)
更新
对于 observeValueForKeyPath
,您可以尝试这样的事情
For the observeValueForKeyPath
you may try something like this
var avWidth:CGFloat = 0
var avHeight:CGFloat = 0
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if keyPath == "videoBounds" {
let rect = change!["new"]! as! NSValue
if let newrect = rect.CGRectValue() as CGRect? {
if newrect.width > 0 || newrect.height > 0 {
if avWidth > 0 || avHeight > 0 {
if newrect.width > avWidth || newrect.height > avHeight {
print("Full Screen")
} else if newrect.width < avWidth || newrect.height < avHeight {
print("Normal screen")
} else {
}
}
avWidth = newrect.width
avHeight = newrect.height
}
}
}
}
这篇关于检测以纵向或横向全屏播放的视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!