检测纵向或横向全屏播放的video

我参与了这两天以上。 我不能得到任何解决scheme,请。

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

第二个要求是,video完成后,在video节目重放或下一个或上一个选项之上创build视图。

这是我的代码;

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

在这里输入图像说明

为了检测全屏或video大小的变化,您可以使用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字典中,您可以检查video帧/边界更改中的更改

对于你提到的旋转,你可能不得不使用viewWillTransitionToSizeUIDeviceOrientationDidChangeNotification通知,或者你可以使用UIDevice.currentDevice().orientation检查当前的UIDevice.currentDevice().orientation因为我没有find任何方法可以检查正在进行的video中的变化

不过,如果你需要检测video方向,你可以检查这个链接: https : //stackoverflow.com/a/25833399/4557505 ,在该链接的其他答案

对于video的重复,您可以在代码中将时间设置为零:

 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上一个button

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

如果您查看AVPlayerItemAVPlayer那么它具有一个具有renderSizevideoComposition设置 – 您可以检查renderSize widthheight以确定您是横向还是纵向。