作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我以编程方式将 AVPlayerViewController
添加到 UIViewController
。当玩家完成播放 (playerDidFinishPlaying) 时,我能够收到通知。我还想知道是否有用户在播放视频时触摸了屏幕,我没有发现任何相关通知。
最佳答案
解决方案是创建一个 AVPlayerViewController
的 Base 类并覆盖 touchesBegan(_:with:)方法:
swift 2:
自定义基类:
class CustomAVPlayerViewController: AVPlayerViewController {
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("touchesBegan")
}
}
View Controller :
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 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()
}
不要忘记import AVKit
和import AVFoundation
。
每次点击 playerViewController
时,都会打印“touchesBegan”。
关于ios - 如何在 AVPlayerViewController 中播放视频时快速检测触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39885018/
我是一名优秀的程序员,十分优秀!