我必须在前一个视频中继的顶部画一个标签或按钮,留下评论.视频列表有它,一旦用户从表中选择一个项目,它需要播放,一旦播放器播放完成,那些按钮或标签应该出现在视频的顶部
这是我的代码:
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: #selector(CommentsTableViewController.playerDidFinishPlaying(_:)),name: AVPlayerItemDidPlayToEndTimeNotification,object: playerItem) comPlayerControl.delegate = self 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.layer.zPosition = 1; comPlayerControl.view.addSubview(DynamicView) }
像这样的要求
解决方法
您正在使用AVPlayerViewController,因此没有理由像Alessandro Ornano的回答那样访问您的应用程序窗口.为什么重新发明轮子?每个AVPlayerViewController都有一个
contentOverlayView
属性,允许您在播放器和控件之间放置视图.
首先,创建一个新的AVPlayerItem并侦听该项目上的AVPlayerItemDidPlayToEndTimeNotification通知.将项目加载到播放器中并开始播放.
项目完成后,将调用您指定用于侦听AVPlayerItemDidPlayToEndTimeNotification通知的选择器.在该选择器中,直接访问contentOverlayView并添加按钮:
在某些视图控制器或其他对象中:
let playerVC = AVPlayerViewController() // ... func setupPlayer { let playerItem = AVPlayerItem(...) playerVC.player?.replaceCurrentItemWithPlayerItem(playerItem) NSNotificationCenter.defaultCenter().addObserver(self,selector: #selector(VC.itemFinished),object: playerItem) self.presentViewController(playerVC,animated: true) { self.playerVC.player?.play() } } func itemFinished() { let btn = UIButton(type: .System) btn.addTarget(self,action: #selector(VC.buttonTapped),forControlEvents: .TouchUpInside) self.playerVC.contentOverlayView?.addSubview(btn) } func buttonTapped() { print("button was tapped") // replay/comment logic here }
如注释(和rejected edit)中所述,按钮可能无法在contentOverlayView中使用.有关替代解决方案,请参阅Pyro’s answer.
您也可以将AVPlayerViewController子类化并在子类的实例中执行所有操作,但Apple warns against that:
Do not subclass
AVPlayerViewController
. Overriding this class’s methods is unsupported and results in undefined behavior.