I'm trying to add an extra button to AVPlayerViewController using the contentOverlayView. The issue is I'd like to only show the button when the player's controls are showing. I have feeling at this stage this isn't possible but wanted to make sure. Can someone confirm whether this is possible currently?
Asked
Active
Viewed 1,796 times
5
-
Did you got this thing worked? if so please let me know the solution. – Manikandan Apr 14 '16 at 07:37
-
No, never could get this working. – Brian F Leighty Apr 14 '16 at 14:54
1 Answers
1
Assuming you are running AVPlayerViewController full screen you can add a UITapGestureRecognizer to the controller's view. But some UIGestureRecognizerDelegate methods were needed. I add the subview to the view of the AVPlayerViewController not the contentOverlayView.
let controller = AVPlayerViewController()
controller.player = self.player
let yourView = controller.view
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(VideoPlayer.onCustomTap(_:)))
tapGestureRecognizer.numberOfTapsRequired = 1;
tapGestureRecognizer.delegate = self
yourView.addGestureRecognizer(tapGestureRecognizer)
self.yourSubView = YourView()
controller.view.addSubview(yourSubView)
parentViewController.modalPresentationStyle = .FullScreen;
parentViewController.presentViewController(controller, animated: false, completion: nil)
Add UIGestureRecognizerDelegate to your class and check that the height and width of any touches view is the screen height and width. This, while obviously hacky is the only way I've found to get this close to working. The player controls like to disappear 5 seconds into play and reappear at the end... this might be solvable with observers on AVPlayer. I tried going this route and things like the ffwd button are going to make me abandon this for a custom player.
extension VideoPlayer: UIGestureRecognizerDelegate {
func onCustomTap(sender: UITapGestureRecognizer) {
if yourSubView.alpha > 0{
UIView.animateWithDuration(0.5, animations: {
self.yourSubView.alpha = 0;
})
} else {
UIView.animateWithDuration(0.5, animations: {
self.yourSubView.alpha = 1;
})
}
}
public func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
if let _touchView = touch.view {
let screenRect:CGRect = UIScreen.mainScreen().bounds
let screenWidth :CGFloat = screenRect.size.width;
let screenHeight:CGFloat = screenRect.size.height;
if _touchView.bounds.height == screenHeight && _touchView.bounds.width == screenWidth{
return true
}
}
return false
}
public func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}

Tony Destro
- 198
- 2
- 6
-
While not a perfect answer. There is no true way to do this besides what you say even though like you say it's hacky. Thanks. – Brian F Leighty Jun 07 '16 at 17:30