gpt4 book ai didi

ios - 如何在 AVPlayerViewController 中播放视频时快速检测触摸

转载 作者:搜寻专家 更新时间:2023-10-30 22:11:23 25 4
gpt4 key购买 nike

我以编程方式将 AVPlayerViewController 添加到 UIViewController。当玩家完成播放 (playerDidFinishPlaying) 时,我能够收到通知。我还想知道是否有用户在播放视频时触摸了屏幕,我没有发现任何相关通知。

最佳答案

解决方案是创建一个 AVPlayerViewControllerBase 类并覆盖 touches​Began(_:​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 AVKitimport AVFoundation

每次点击 playerViewController 时,都会打印“touchesBegan”。

关于ios - 如何在 AVPlayerViewController 中播放视频时快速检测触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39885018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com