检测AVPlayerViewController的关闭按钮的单击

时间:2018-08-09 07:11:44

标签: ios swift media-player avplayerviewcontroller

有一种视频播放功能,例如linkedIn视频播放。 我正在使用AVPlayerViewController播放video.in,因为我遇到的问题是当AVPlayerviewController将关闭时,我无法检测到“关闭”按钮事件。 有什么观察者或方法会在AVPlayerController关闭时调用?

1 个答案:

答案 0 :(得分:0)

  

Swift 3.2

import UIKit
import AVKit
import AVFoundation

class PlayResourcesVC: UIViewController {

    //MARK:- Variable Declarations
    var video_Url:String = String()
    let playerController = AVPlayerViewController()


    //MARK:- ViewDidload
    override func viewDidLoad() {
        super.viewDidLoad()

        let player = AVPlayer(url: URL(string: video_Url)!)
        playerController.player = player
        self.present(playerController, animated: false) {
            player.play()
            self.playerController.addObserver(self, forKeyPath: #keyPath(UIViewController.view.frame), options: [.old, .new], context: nil)
        }
    }

    override func viewDidDisappear(_ animated: Bool) {
        NotificationCenter.default.removeObserver(NSNotification.Name.AVPlayerItemDidPlayToEndTime)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    //MARK:- All Method
    func playerDidFinishPlaying(note: NSNotification) {
        self.navigationController?.popViewController(animated: false)
    }

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        self.playerController.removeObserver(self, forKeyPath: #keyPath(UIViewController.view.frame))
        self.navigationController?.popViewController(animated: false)
    }

}