Swift, how to get event click done button during play the video on AVPlayer?

时间:2023-01-23 23:02:10

I have one TableViewController and I play video on that Table by tapping button inside cell to play by AVPlayer by code

我有一个TableViewController,我在该表上播放视频,方法是点击单元格内的按钮,按代码播放AVPlayer

@IBAction func btnFullScreen(sender: AnyObject) {    
    let playController = AVPlayerViewController()
    self.presentViewController(playController, animated: true, completion: nil)
    playController.player?.play()
}

So, It will play the video in full screen, and also exist with done button on the top left. I want to get event done button, after click the done button it will get the current time of player to do more something else. but how to get the event click done button?

因此,它将全屏播放视频,并且左上角也有完成按钮。我想获得事件完成按钮,点击完成按钮后,它将获得玩家当前时间做更多其他事情。但是如何让事件点击完成按钮?

2 个解决方案

#1


1  

Currently, it is not possible that you can intercept the Done button of AVPlayerController, a Bug Report is submitted for this with radar: 27047358

目前,您无法拦截AVPlayerController的完成按钮,使用雷达为此提交了错误报告:27047358

You can only add an observer for AVPlayerItemDidPlayToEndTimeNotification which will be fired when your item finished playing.

您只能为AVPlayerItemDidPlayToEndTimeNotification添加一个观察者,当您的项目完成播放时将触发该观察者。

Also according to the documentation:

另外根据文件:

Do not subclass AVPlayerViewController. Overriding this class’s methods is unsupported and results in undefined behaviour.

不要子类化AVPlayerViewController。不支持覆盖此类的方法,并导致未定义的行为。

Reference - here

参考 - 这里

#2


0  

I'm using this trick , it works on my scenario but it may give you a clue :

我正在使用这个技巧,它适用于我的场景,但它可能会给你一个线索:

Step one:

add observers on a view controller you want to present your from there player view controller to get notified when its frame changes:

在视图控制器上添加观察者,您希望从其中显示播放器视图控制器,以便在其帧更改时收到通知:

 self.playerViewController.addObserver(self, forKeyPath: #keyPath(MyViewController.view.frame), options: .new, context: nil)
 NotificationCenter.default.addObserver(self, selector: #selector(self.playerDidFinishPlaying),
                                               name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.playerViewController.player!.currentItem)

Step Two:

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if (keyPath ==  #keyPath(MyViewController.view.frame)){

        if playerViewController.player?.rate == 0{
            doneClicked()
        }
    }
}

Step Three:

    func doneClicked(){

    self.playerViewController.player!.pause()
    self.playerViewController.player!.rate =  0.0
    self.playerViewController.player = nil
    self.playerViewController.removeObserver(self, forKeyPath: #keyPath(HomeTabViewController.view.frame))
    NotificationCenter.default.removeObserver(self)
    self.playerViewController.dismiss(animated: false, completion: nil)
}

#1


1  

Currently, it is not possible that you can intercept the Done button of AVPlayerController, a Bug Report is submitted for this with radar: 27047358

目前,您无法拦截AVPlayerController的完成按钮,使用雷达为此提交了错误报告:27047358

You can only add an observer for AVPlayerItemDidPlayToEndTimeNotification which will be fired when your item finished playing.

您只能为AVPlayerItemDidPlayToEndTimeNotification添加一个观察者,当您的项目完成播放时将触发该观察者。

Also according to the documentation:

另外根据文件:

Do not subclass AVPlayerViewController. Overriding this class’s methods is unsupported and results in undefined behaviour.

不要子类化AVPlayerViewController。不支持覆盖此类的方法,并导致未定义的行为。

Reference - here

参考 - 这里

#2


0  

I'm using this trick , it works on my scenario but it may give you a clue :

我正在使用这个技巧,它适用于我的场景,但它可能会给你一个线索:

Step one:

add observers on a view controller you want to present your from there player view controller to get notified when its frame changes:

在视图控制器上添加观察者,您希望从其中显示播放器视图控制器,以便在其帧更改时收到通知:

 self.playerViewController.addObserver(self, forKeyPath: #keyPath(MyViewController.view.frame), options: .new, context: nil)
 NotificationCenter.default.addObserver(self, selector: #selector(self.playerDidFinishPlaying),
                                               name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.playerViewController.player!.currentItem)

Step Two:

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if (keyPath ==  #keyPath(MyViewController.view.frame)){

        if playerViewController.player?.rate == 0{
            doneClicked()
        }
    }
}

Step Three:

    func doneClicked(){

    self.playerViewController.player!.pause()
    self.playerViewController.player!.rate =  0.0
    self.playerViewController.player = nil
    self.playerViewController.removeObserver(self, forKeyPath: #keyPath(HomeTabViewController.view.frame))
    NotificationCenter.default.removeObserver(self)
    self.playerViewController.dismiss(animated: false, completion: nil)
}