且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-06 13:56:04

用于检测全屏或视频大小改变你可以使用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://***.com/a/25833399/4557505 ,以及该链接中的其他答案

Still if you need to detect video orientation you may check this link : https://***.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
            }
        }
    }
}