在swift中,如何在AVPlayerViewController中播放视频时检测触摸
In swift, how to detect touch while playing video in AVPlayerViewController
我以编程方式将 AVPlayerViewController
添加到 UIViewController
。当玩家完成播放 (playerDidFinishPlaying) 时,我能够收到通知。我也想知道在播放视频的过程中有没有用户触屏,我还没有找到相关的通知。
解决方法是创建AVPlayerViewController
的Base class并覆盖touchesBegan(_:with:)方法:
Swift 2:
自定义基础 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:
自定义基础 Class:
class CustomAVPlayerViewController: AVPlayerViewController {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touchesBegan")
}
}
视图控制器:
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()
}
别忘了import AVKit
和import AVFoundation
。
每次点击 playerViewController
,都会打印 "touchesBegan"。
我遇到了同样的问题。 contentOverlayView 仅在 tvos 中可用,因此这不是一个选项。
我最终在我添加了 AVPlayer 的 UIImageView 上添加了一个 UIView。我在 UIView 上将背景颜色设置为清除,因此它不可见,但可以接收手势。这为点击手势识别器提供了一个目标。
我也是这么想的。子类化 AVPlayerViewController 将在 iOS 11.4.1 上工作,但不能在 iOS 12 及以上版本上工作。因此,解决方案是在 playerviewcontroller contentoverlayview 上添加子视图,然后在该子视图上您可以添加任何手势或按钮来检测触摸。这是相同的代码片段::
// 添加此通知是为了连续播放视频,您可以删除此通知,以防您只需要播放一次视频。
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
}
我以编程方式将 AVPlayerViewController
添加到 UIViewController
。当玩家完成播放 (playerDidFinishPlaying) 时,我能够收到通知。我也想知道在播放视频的过程中有没有用户触屏,我还没有找到相关的通知。
解决方法是创建AVPlayerViewController
的Base class并覆盖touchesBegan(_:with:)方法:
Swift 2:
自定义基础 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:
自定义基础 Class:
class CustomAVPlayerViewController: AVPlayerViewController {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touchesBegan")
}
}
视图控制器:
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()
}
别忘了import AVKit
和import AVFoundation
。
每次点击 playerViewController
,都会打印 "touchesBegan"。
我遇到了同样的问题。 contentOverlayView 仅在 tvos 中可用,因此这不是一个选项。
我最终在我添加了 AVPlayer 的 UIImageView 上添加了一个 UIView。我在 UIView 上将背景颜色设置为清除,因此它不可见,但可以接收手势。这为点击手势识别器提供了一个目标。
我也是这么想的。子类化 AVPlayerViewController 将在 iOS 11.4.1 上工作,但不能在 iOS 12 及以上版本上工作。因此,解决方案是在 playerviewcontroller contentoverlayview 上添加子视图,然后在该子视图上您可以添加任何手势或按钮来检测触摸。这是相同的代码片段::
// 添加此通知是为了连续播放视频,您可以删除此通知,以防您只需要播放一次视频。
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
}