2016-07-25 23 views
5

AVPlayerViewControllerでローカルビデオを再生したいが、完了ボタンのクリックイベントが見つかりませんでした。AVPlayerViewControllerの完了ボタンイベント

私のビデオはAVPlayerViewControllerで再生できますが、次のボタン、前のボタン、完了したボタンのクリックイベントは見つかりませんでした。

答えて

7

に表情が正式に何の完了ボタンのクリックイベントはありません持ってください、アップルからのエンジニアはそれhereについて語りました。

私の研究では、完了ボタンがクリックされた場合にイベントを取得することが実際に間接的な方法があることが分かりました。

終了ボタンをクリックしたときに変更されるAVPlayerViewControllerの唯一の変数はAVPlayerViewController.view.frameです。 まずview.frameがviewControllerの中央に表示されます。

animated : trueと表示すると、viewControllerの下部に戻り、中央に戻ります。完了すると、それは一番下に戻ります。

animated : falseと表示すると、ビデオの再生を開始するときにフレームがviewControllerの中央に、完了したときに下部にフレームが2つだけ変更されます。

present(PlayerViewController, animated : true)へのコールバックでAVPlayerViewController.view.frameにオブザーバーを追加すると、オブザーバーが1回だけ呼び出されます。終了ボタンをクリックすると、ビデオビューが画面外に表示されます。

私の場合、AVPlayerViewControllerはモーダルでアニメーションで表示されました。以下のコードは、私の仕事:あなたは、[完了]をクリックしたときに

スウィフト3.0

override func viewDidLoad() 
{ 
    super.viewDidLoad() 

    let videoURL = NSURL(fileURLWithPath:Bundle.main.path(forResource: "MyVideo", ofType:"mp4")!) 
    let player = AVPlayer(url: videoURL as URL) 

    present(playerViewController, animated: false) {() -> Void in 
     player.play() 

     self.playerViewController.player = player 
     self.playerViewController.addObserver(self, forKeyPath: #keyPath(UIViewController.view.frame), options: [.old, .new], context: nil) 
    }} 
    override func observeValue(forKeyPath keyPath: String?, 
          of object: Any?, 
          change: [NSKeyValueChangeKey : Any]?, 
          context: UnsafeMutableRawPointer?) 
{ 

    print(self.playerViewController.view.frame) 
    //Player view is out of the screen and 
    //You can do your custom actions 
} 

はまた、私が見つけた、AVPlayerViewControllerが却下されていない、あなたはオブザーバを追加することはできませんので、あなたは、ParentViewController.presentedViewControllerでそれを見ることができますこのプロパティ

+0

はありがとうございました! @vmchar、私の一日を節約!あなた以外のどこかで正解を見つけることができません! – Jerome

+0

haha​​ Perfect –

+0

これはかなり甘い、感謝しました! – Jason

0

にあなたは私が

override func viewWillAppear(_ animated: Bool) { print("Done Button Click Event") }

を使用することができますあなたのクラス。 AVPlayerViewControllerが消えるとviewWillAppear funcが呼び出され、ここで作業を完了できます。

AVPlayerViewControllerクラスを使用している場合は、このfuncを使用できます。

override func viewWillDisappear(_ animated: Bool) { <#code#> }

0

私は同じ問題を抱えていた、と私は別のトリック(WebViewの中でビデオを果たしているYoutubePlayer迅速なフレームワークと連携していますが、他の用途に適合させることができるかもしれない)を発見しました。

私の場合、Doneボタンを押してフルスクリーンビデオプレーヤーのPauseボタンを押すと、YoutubePlayerのpauseイベントになります。私がすることをしたら

class MyWindow:UIWindow { 
    override func becomeKey() { 
     Services.appData.myWindowIsKey = true 
    } 

    override func resignKey() { 
     Services.appData.myWindowIsKey = false 
    } 
} 

:ビデオは別のウィンドウで再生しかし、私は私の窓は、そのようなキーのウィンドウであるか否かを格納するbecomeKeyresignKey機能を自分のアプリケーションのメインウィンドウをサブクラス化し、オーバーライドDoneボタンが押されたとき、私のウィンドウはキーウィンドウでであり、ビデオビューコントローラを閉じることができ、Pauseのケースでは私のウィンドウは私のウィンドウがキーであるかどうかを確認できますいいえキーウィンドウなので、私は何もしません。

2


これは、AVPlayerViewControllerからDoneボタンのクリックイベントを取得するために実行しました。

まず第一に、これを行うボタンのを扱う基本的な機能のみのためにある怒鳴る

extension Notification.Name { 
static let kAVPlayerViewControllerDismissingNotification = Notification.Name.init("dismissing") 
} 

ようNotification.Nameの拡張を作成し、AVPlayerViewControllerの拡張を作成し、viewWillDisappear

怒鳴るような
// create an extension of AVPlayerViewController 
extension AVPlayerViewController { 
    // override 'viewWillDisappear' 
    open override func viewWillDisappear(_ animated: Bool) { 
     super.viewWillDisappear(animated) 
     // now, check that this ViewController is dismissing 
     if self.isBeingDismissed == false { 
      return 
     } 

     // and then , post a simple notification and observe & handle it, where & when you need to..... 
     NotificationCenter.default.post(name: .kAVPlayerViewControllerDismissingNotification, object: nil) 
    } 
} 

を上書きEVENT。

コーディング幸せ...

+0

一時停止/進む/戻るボタンがタップされたときの処理方法は? – pkc456

+0

@ pkc456私の意見では、簡単にそれらのイベントを処理できるように、UIViewControllerのAVPlayerLayerを使用してカスタムプレーヤーを作成する必要があります。 – Vats

0

これは@vmcharによって答えから小さな改善である:

我々はAVPlayerViewControllerではなく、フレームを分析するのに閉じられていることを確認するために.isBeingDismissedメソッドを使用することができます。

... 

let videoURL = NSURL(fileURLWithPath:"video.mp4") 
let player = AVPlayer(url: videoURL as URL) 

present(playerViewController!, animated: false) {() -> Void in 
    player.play() 

    self.playerViewController!.player = player 
    self.playerViewController!.addObserver(self, forKeyPath:#keyPath(UIViewController.view.frame), options: [.old, .new], context: nil) 
... 

は、その後値を観察する

override func observeValue(forKeyPath keyPath: String?, 
          of object: Any?, 
          change: [NSKeyValueChangeKey : Any]?, 
          context: UnsafeMutableRawPointer?) 
{ 

    if (playerViewController!.isBeingDismissed) { 
    // Video was dismissed -> apply logic here 
    } 
}