快速介绍如何在AVPlayerViewController中播放视频时检测触摸 [英] In swift, how to detect touch while playing video in AVPlayerViewController
问题描述
我已通过编程方式将AVPlayerViewController
添加到了UIViewController
.播放器完成播放后,我可以收到通知(playerDidFinishPlaying).我还想知道在播放视频时用户是否触摸了屏幕,并且没有找到任何相关的通知.
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 touchesBegan(_:with:) method:
迅速2:
自定义基础类:
class CustomAVPlayerViewController: AVPlayerViewController {
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("touchesBegan")
}
}
ViewController:
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()
}
快捷键3:
自定义基础类:
class CustomAVPlayerViewController: AVPlayerViewController {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touchesBegan")
}
}
View Controller:
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".
Each time you tap on the playerViewController
, "touchesBegan" will be printed.
这篇关于快速介绍如何在AVPlayerViewController中播放视频时检测触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!