gpt4 book ai didi

ios - 检测以纵向或横向全屏播放的视频

转载 作者:搜寻专家 更新时间:2023-10-31 22:06:46 25 4
gpt4 key购买 nike

我搞了这两天多。我找不到任何解决方案。

我的要求是视频应该播放 Portrait inline、full screen、landscape。我这里的问题是如何识别视频是全屏横向播放还是纵向播放。我已经实现了 viewWillTransitionToSize 方法。但是 AVPlayer 有全屏箭头按钮。我如何识别用户点击了该选项。

第二个要求,一旦视频完成,在视频重播或下一个或上一个选项之上创建 View 。

这是我的代码;

 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)
}

enter image description here

最佳答案

为了检测全屏或视频大小的变化,你可以为 AVPlayerViewController 使用 KVO,代码可以是这样的:

[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)")
}
}

change字典中可以查看视频帧/边界变化的变化

对于您提到的旋转,您可能必须使用 viewWillTransitionToSizeUIDeviceOrientationDidChangeNotification 通知,或者您可以使用 UIDevice.currentDevice().orientation 检查当前方向 因为我没有找到任何方法可以检查正在进行的视频中的变化

不过,如果您需要检测视频方向,您可以查看此链接:https://stackoverflow.com/a/25833399/4557505 ,以及该链接中的其他答案

对于视频的重复,您可以在代码中像这样将时间设置为零:

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()

您可以尝试在 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,您可以尝试这样的操作

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
}
}
}
}

关于ios - 检测以纵向或横向全屏播放的视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36323259/

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