AVPlayer UITapGestureRecognizer not working

I have this code for my AVPlayerViewController .

 UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAvPlayer)]; [self.avPlayerViewController.view addGestureRecognizer:tap]; 

but this does not work ..: S, I tried setting

 [self.avPlayerViewController.view setUserInteractionEnabled:YES]; 

still no good ..

The only working solution is to use the UIGestureRecognizer and implement its shouldReceiveTouch delegate and check if the av player is affected .. but the problem is that we don’t want to capture the “release release” event .. because if the av player view just touched, it immediately executes the code, and that’s not what we wanted ...

Please help us with this problem.

Thanks!

+7
source share
7 answers

Or you can subclass AVPlayerViewController and use the folowwing method to

get control: -

1.touchBegan 2.touchEnd

Make sure you set this flag self.avPlayerContr.showsPlaybackControls = false

Hope this works for you.

0
source

That should do it. Instead, add a recognizer to the player subview:

 [videoPlayerViewController.view.subviews[0] addGestureRecognizer:tap] 

It was a quick fix for a complex user interface, and gestures peeked into the contentOverlayView, see TyR's answer

+6
source

The right place to handle gestures on the AVPlayerViewController is inside the contentOverlayView controller.

contentOverlayView is a read-only property of AVPlayerViewController. This is a view that appears on top of the video, but under the controller. Just the perfect place for sensory processing. You can add routines or gesture handlers to it at boot time.

The following code provides support for touching your video controller in two ways to demonstrate both gesture recognition and UIButton's approach.

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"mySegue"]) { // Set things up when we're about to go the the video AVPlayerViewController *avpvc = segue.destinationViewController; AVPlayer *p = nil; p = [AVPlayer playerWithURL:[NSURL URLWithString:@"https://my-video"]]; avpvc.player = p; // Method 1: Add a gesture recognizer to the view UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myAction:)]; avpvc.contentOverlayView.gestureRecognizers = @[tap]; // Method 2: Add a button to the view UIButton *cov = [UIButton buttonWithType:UIButtonTypeCustom]; [cov addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside]; cov.frame = self.view.bounds; } } 

(not really for writing the Swift version at the moment, sorry. :)

+6
source

As far as I know, what you really want

 UILongPressGestureRecognizer 

Because

 UITapGestureRecognizer 

Has no type of release event if you add

 UILongPressGestureRecognizer 

to

 avPlayerViewController 

Then in

 -(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { if ( [gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] ) { } return YES; } 

Then you can set the minimum gestures to make you feel better.

 [holdGesture setMinimumPressDuration:0.01] 

Then you can implement the method,

 - (void)holdAction:(UILongPressGestureRecognizer *)holdRecognizer 

Then check the status of the recognizer and perform the required functionality

0
source

You can add a transparent view to the avplayerViewController and add a tapgesturerecognizer to this view. hope this helps :)

0
source

An easy solution adds the look of gestures to the same vision of the movie holder after adding a video player. No need to add a gesture recognizer directly to the player.

  [self.movieHolderView addSubview:self.moviePlayer.view]; [self.movieHolderView addSubview:self.gestureView]; [self.movieHolderView bringSubviewToFront:self.gestureView]; 

Now we can add our gesture recognizer in the form of gestures like this

 self.fullScreenTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandler:)]; self.fullScreenTapGesture.numberOfTapsRequired = 1; [self.gestureView addGestureRecognizer:self.fullScreenTapGesture]; 
0
source

Gesture Recognition must be added differently for OS 11 and earlier:

Universal solution (Swift 4):

 let tap = UITapGestureRecognizer(target: self, action: #selector(videoTap)) if #available(iOS 11.0, *) { playerVC?.contentOverlayView?.gestureRecognizers = [tap] } else { playerVC?.view.subviews.first?.addGestureRecognizer(tap) } @objc func videoTap(_ button: UIButton) { print("Tapped on video") } 
0
source

All Articles