检测以纵向或横向格式全屏显示的视频

时间:2016-03-31 02:47:43

标签: ios iphone swift video

我参与了这两天多。我不能得到任何解决方案。

我要求视频应该是肖像内联,全屏,风景。我的问题是如何识别视频正在播放全屏风景或人像。我已经实现了viewWillTransitionToSize方法。但是AVPlayer有全屏箭头按钮。如何识别用户点击该选项。

第二个要求,一旦视频完成,就可以在视频节目重播或下一个或上一个选项之上创建视图。

这是我的代码;

 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

2 个答案:

答案 0 :(得分:6)

对于检测全屏或视频大小更改,您可以使用KVO作为AVPlayerViewController,代码可以是这样的:

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

答案 1 :(得分:0)

如果您查看AVPlayerItem的{​​{1}},那么它的AVPlayer设置为videoComposition - 您可以检查renderSize { {1}}和renderSize确定您是在风景画还是肖像画。