Only Showing contentOverlayView in AVPlayerViewController when controls are showing

Brian F Leighty

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?

Tony Destro

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Disabling controls when showing loading bar in AngularJS

From Dev

Radio controls not showing in WebBrowser

From Dev

Camera controls not showing on device

From Dev

Always showing video controls

From Dev

Camera controls not showing on device

From Dev

UIImagePickerController trim controls not showing

From Dev

AVPlayerViewController not playing video and showing Quicktime logo

From Dev

Controls not showing on HTML5 Video when using javascript

From Dev

Gridsplitter not showing nor working only when horizontal

From Dev

Why the animation is showing only on hover and not when leaves?

From Java

VS Code not showing subfolders when there is only one

From Dev

wordpress pagination showing when there is only one post

From Dev

Krypton Toolkit controls not showing in form

From Dev

Video player controls not showing in sharepoint

From Dev

Metro UI controls not showing on form

From Dev

No controls showing upon opening form

From Dev

Image showing only in debug

From Dev

CodeLens only showing references?

From Dev

Showing only one DialogFragment

From Dev

DefinitelyTyped only showing Namespace

From Dev

showing only one coordinate

From Dev

Only showing part of string

From Dev

Only the first element is showing

From Dev

showing only one coordinate

From Dev

Bootstrap only showing navbar

From Dev

Only Showing 1 Row

From Dev

Psensor only showing graph

From Dev

map on android device showing only one marker from url but showing all the values when I use Toast

From Dev

Adapter-Showing view only when there is a change in a value