检测AVPlayerViewController的单击关闭按钮[复制]

问题描述 投票:0回答:1

这个问题在这里已有答案:

视频播放有一个功能就像LinkedIn视频播放一样。我正在使用AVPlayerViewController播放视频。我面临的问题是,当AVPlayerviewController关闭时,我无法检测到关闭按钮事件。当AVPlayerController解散时,是否有任何观察者或方法会调用?

ios swift media-player avplayerviewcontroller
1个回答
1
投票

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)
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.