Avplayer Uitapgesturerecognizer Not Working

AVPlayer UITapGestureRecognizer not working

Or you can subclass the AVPlayerViewController and use folowwing method to

get the control : -

1.touchBegan
2.touchEnd

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

Hope this works for you.

AVPlayerViewController UITapGestureRecognizer not working in ios 11

Looking at your code it looks like you subclass AVPlayerViewController. This is not recommended. From Apple documentation:

Important

Do not subclass AVPlayerViewController. Overriding this class’s methods is unsupported and results in undefined behavior.

But, if you still want to subclass AVPlayerViewController you can get touches by overriding touchesBegan:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touchesBegan")
}

Cannot touch the UIView containing AVPlayerLayer

Check were have you added the AVPlayerLayer. Add layer at index O instead of adding it to the top.

Use insertSublayer(_:at:) instead of addSublayer(_:)

class ViewController: UIViewController, UIGestureRecognizerDelegate {
@IBOutlet weak var imageView: UIImageView!

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.imageView.isUserInteractionEnabled = true
self.addPlayer()
}

func addPlayer() {
if let url = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4") {
let player = AVPlayer(url: url)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.imageView.bounds
playerLayer.videoGravity = .resizeAspect
self.imageView.layer.insertSublayer(playerLayer, at: 0)
player.play()
}

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapHandler(_:)))
tapGesture.delegate = self
self.imageView.addGestureRecognizer(tapGesture)
}

@objc func tapHandler(_ sender: UIGestureRecognizerDelegate) {
print("view tapped")
}
}

Now, whenever you tap on the view, tapHandler(_:) method will be called.

Don't forget to enable userInteraction of imageView manually.

self.imageView.isUserInteractionEnabled = true

unable to catch swipe gesture on AVPlayerViewController using swift

First of you need to disable AVPlayerViewController's default gestureRecognizers for that you can use

extension AVPlayerViewController {
func disableGestureRecognition() {
let contentView = view.value(forKey: "contentView") as? UIView
contentView?.gestureRecognizers = contentView?.gestureRecognizers?.filter { $0 is UITapGestureRecognizer }
}
}

And you can use it like:

playerViewController.disableGestureRecognition()

Now you need to replace

leftSwipe.direction = UISwipeGestureRecognizer.Direction.up

with

leftSwipe.direction = UISwipeGestureRecognizer.Direction.left

because you are checking for left direction.

And since your UIViewController is not longer in window hierarchy you can not present alert with self.present and to solve that you need to make playerViewController as a global variable in your class.

and then you can present alert like:

playerViewController.present(alert, animated: true)

in your handleSwipes method.

And working code will be like:

import UIKit
import AVKit

class ViewController: UIViewController {

var playerLayer: AVPlayerLayer?
var player: AVPlayer?
let playerViewController = AVPlayerViewController()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

@IBAction func tap(_ sender: Any) {

let videoUrlString = "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"
let videoURL = URL(string: videoUrlString)
self.player = AVPlayer(url: videoURL!)

playerViewController.player = player

playerViewController.disableGestureRecognition()
present(playerViewController, animated: true, completion: {
self.player?.play()
})

let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
leftSwipe.direction = UISwipeGestureRecognizer.Direction.left
playerViewController.view.addGestureRecognizer(leftSwipe)
}

@objc func handleSwipes(_ sender:UISwipeGestureRecognizer) {

if (sender.direction == .left) {

let alert = UIAlertController(title: "Swipe Action", message: "It is LEFT Swipe ", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
playerViewController.present(alert, animated: true)
}
}
}

extension AVPlayerViewController {
func disableGestureRecognition() {
let contentView = view.value(forKey: "contentView") as? UIView
contentView?.gestureRecognizers = contentView?.gestureRecognizers?.filter { $0 is UITapGestureRecognizer }
}
}

And result will be:

Sample Image

Pass tap gesture from UIView to underlying AVPlayer

Touch events stop when a view (actually a UIResponder) responds to them. So if you have a gesture recognizer attached to a view on top of your AVPlayerViewController, the gesture recognizer will catch the events and the AVPlayerViewController will not. This is by design.

Why not have your view send messages to the AVPlayerViewController in response to the user's gestures? AVPlayerViewController has a property showsPlaybackControls. Set that to YES if you want the player to show its controls.

AVPlayerViewController Swipe UISwipeGestureRecognizer.up not working Xcode 12 tvOS 14

AVPlayerViewController.CustomOverlayViewController was introduced in tvOS 13 and apparently mandatory from tvOS 14. If you want to add a channel charger (or any other view) when swiping up then:

private var playerViewController: AVPlayerViewController? {
didSet {
guard let playerViewController = playerViewController else {
return
}
playerViewController.customOverlayViewController = myChannelChangerVC
}
}

Resources

  • https://developer.apple.com/videos/play/wwdc2019/503/
  • https://developer.apple.com/documentation/avkit/adopting_custom_interactive_overlays_channel_flipping_and_parental_controls_in_tvos_video_playback


Related Topics



Leave a reply



Submit