快速介绍如何在AVPlayerViewController中播放视频时检测触摸 [英] In swift, how to detect touch while playing video in AVPlayerViewController

查看:93
本文介绍了快速介绍如何在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.

推荐答案

解决方案是创建 Base 类,并覆盖

The solution is to create a Base class of AVPlayerViewController and override touches​Began(_:​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 AVKitimport AVFoundation.

每次点击playerViewController时,将打印"touchesBegan".

Each time you tap on the playerViewController, "touchesBegan" will be printed.

这篇关于快速介绍如何在AVPlayerViewController中播放视频时检测触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆