如何在使用 MPMoviePlayerController 时识别点击手势

How to recognize a tap gesture while using an MPMoviePlayerController

我正在尝试在播放视频时识别点击手势,以便我可以像 snapchat 那样将其关闭,但是,它说 MPMoviePlayerControllers 没有成员可以添加触摸手势,这是真的还是我我用错方法了吗?

var MP4 : NSData?
var MarkerLong : CLLocationDegrees?
var MarkerLat : CLLocationDegrees?
var Url : String?
var videoPlayer : MPMoviePlayerController!

private var firstAppear = true

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    if firstAppear {
        do {
            try playVideo()
            firstAppear = false
        } catch AppError.InvalidResource(let name, let type) {
            debugPrint("Could not find resource \(name).\(type)")
        } catch {
            debugPrint("Generic error")
        }

    }
}

private func playVideo() throws {

    self.videoPlayer = MPMoviePlayerController()
    self.videoPlayer.repeatMode = MPMovieRepeatMode.None
    self.videoPlayer.contentURL = NSURL(string: Url!)
    self.videoPlayer.controlStyle = MPMovieControlStyle.None
    self.view.addSubview(self.videoPlayer.view)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(PlayVideoViewController.videoPlayBackDidFinish(_:)), name: MPMoviePlayerPlaybackDidFinishNotification, object: self.videoPlayer)
    self.videoPlayer.view.frame.size = CGSizeMake(640, 1136)
    self.videoPlayer.view.center = self.view.center
    self.videoPlayer.play()
    let gesture = UITapGestureRecognizer(target: self, action: "someAction:")
    self.videoPlayer.addGestureRecognizer(gesture)

}

MPMoviePlayerController 是一个视图控制器。手势识别器已添加到 视图 。您需要将此手势添加到 MPMoviePlayerController 的 view.

(更好的是,停止使用 MPMoviePlayerController;它已被弃用。)

我建议使用 AVPlayerViewController,但请确保不要将其子类化,因为 Apple 声明不这样做。

1) MPMoviePlayer 已弃用(不要再使用此代码)

2) AVPlayerViewController 有一组更复杂的代码来允许更多的自定义。

如果你真的想自定义一些东西,你可以子类化 AVPlayer 并制作你自己的自定义视图来播放视频,但是你必须添加你自己的 pause/start,等等...