简体   繁体   中英

In swift, how to detect touch while playing video in AVPlayerViewController

I have programmatically added an AVPlayerViewController to a UIViewController . I am able to receive the notification when the player is finished playing (playerDidFinishPlaying). I would also like to know if a user has touched the screen while the video is playing and I have not found any related notifications.

The solution is to create a Base class of AVPlayerViewController and override touches​Began(_:​with:​) method:

Swift 2:

Custom Base Class:

class CustomAVPlayerViewController: AVPlayerViewController {
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        print("touchesBegan")
    }
}

ViewController:

let videoURL = NSURL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(URL: videoURL!)
let playerViewController = CustomAVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true) {
    playerViewController.player!.play()
}

Swift 3:

Custom Base Class:

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

View Controller:

let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(url: videoURL!)
let playerViewController = CustomAVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
    playerViewController.player!.play()
}

Don't forget to import AVKit and import AVFoundation .

Each time you tap on the playerViewController , "touchesBegan" will be printed.

I had this same problem. The contentOverlayView is only available in tvos, so that was not an option.

I ended up adding a UIView over the UIImageView that I added the AVPlayer to. I set the background color to clear on the UIView, so it's not visible, but can receive gestures. This provides a target for the tap gesture recognizer.

I resolve the same. Subclassing AVPlayerViewController will work on iOS 11.4.1 but not on iOS 12 an above. So the solution for this is add subview on playerviewcontroller contentoverlayview and then on that subview you can add any gesture or button for detecting the touch. Here is the code snippet for the same::

// This notification is added for continuous playing of the video you can remove this in case you need video is played only once.

private func playVideo() {

    guard let path = Bundle.main.path(forResource: "BG01", ofType:"mp4") else {
        debugPrint("video.m4v not found")
        return
    }
    self.player = AVPlayer(url: URL(fileURLWithPath: path))

    NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem, queue: .main) { [weak self] _ in
        self?.player?.seek(to: kCMTimeZero)
        self?.player?.play()
    }

  let  playerController : AVPlayerViewController? = AVPlayerViewController()
    let btn : UIButton = UIButton()
    btn.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
    btn.addTarget(self, action: #selector(touchDetect), for: .touchUpInside)
    btn.backgroundColor  = UIColor.clear
    playerController?.contentOverlayView?.addSubview(btn)
   // playerController?.homeVCProtocolDelegate = self as HomeVCProtocol
    playerController?.player = player
    playerController?.showsPlaybackControls = false
    self.player?.play()
    present(playerController!, animated: false) {
    self.player?.play()
    }

}

@objc func touchDetect()
{
    // Here you will get the call
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM