简体   繁体   English

在swift中,如何在AVPlayerViewController中播放视频时检测触摸

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

I have programmatically added an AVPlayerViewController to a UIViewController . 我已经以编程方式将一个AVPlayerViewController添加到UIViewController I am able to receive the notification when the player is finished playing (playerDidFinishPlaying). 我可以在玩家完成游戏时收到通知(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: 解决方案是创建一个AVPlayerViewControllerBase类并覆盖触摸Began(_:with AVPlayerViewController方法:

Swift 2: 斯威夫特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: 斯威夫特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 . 不要忘记import AVKitimport AVFoundation

Each time you tap on the playerViewController , "touchesBegan" will be printed. 每次点击playerViewController ,都会打印“touchesBegan”。

I had this same problem. 我有同样的问题。 The contentOverlayView is only available in tvos, so that was not an option. contentOverlayView仅在tvos中可用,因此不是一个选项。

I ended up adding a UIView over the UIImageView that I added the AVPlayer to. 我最后在UIImageView上添加了一个UIView,我添加了AVPlayer。 I set the background color to clear on the UIView, so it's not visible, but can receive gestures. 我将背景颜色设置为在UIView上清除,因此它不可见,但可以接收手势。 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. Subclassing AVPlayerViewController可以在iOS 11.4.1上运行,但不能在iOS 12上运行。 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. 因此,解决方法是在playerviewcontroller contentoverlayview上添加子视图,然后在该子视图上添加任何手势或按钮以检测触摸。 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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