Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only Showing contentOverlayView in AVPlayerViewController when controls are showing

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?

like image 635
Brian F Leighty Avatar asked Nov 10 '22 07:11

Brian F Leighty


1 Answers

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
}
}
like image 176
Tony Destro Avatar answered Nov 14 '22 22:11

Tony Destro