Done button click event in AVPlayerViewController

前端 未结 9 852
广开言路
广开言路 2020-12-03 17:40

I want to play local video in AVPlayerViewController but did not find click event of Done button.

My video is able to play in AVPlayerViewController but I did not fi

相关标签:
9条回答
  • 2020-12-03 17:40

    I had the same problem, and I found another trick (works with YoutubePlayer swift framework which plays videos in a webview, but you might be able to adapt it to other uses).

    In my case, pressing the Done button and pressing the Pause button on the fullscreen video player both results in a pause event on the YoutubePlayer. However, as the video plays in a different window, I subclassed my application's main window and overrode the becomeKey and resignKey functions to store whether my window is the key window or not, like that:

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

    Once I have that, I can check whether my window is key when the video state goes to pause - when the Done button was pressed, my window is the key window and I can dismiss my video view controller, and in the Pause case my window is not the key window, so I do nothing.

    0 讨论(0)
  • 2020-12-03 17:48

    I found a workaround without having to subclass the AVPlayerViewController, which the documentation clearly states you should not (https://developer.apple.com/documentation/avkit/avplayerviewcontroller). It is not pretty, but it got the job done for me by giving me somewhere to run code when the AVPlayerViewController is dismissed.

    AnyViewController: UIViewController, AVPlayerViewControllerDelegate {
        private let playerVC = AVPlayerViewController()
    
        override func viewDidLoad() {
            playerVC.delegate = self        
        }
    
        func playerViewController(_ playerViewController: AVPlayerViewController, willEndFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator) {
            if playerViewController.isBeingDismissed {
                playerViewController.dismiss(animated: false) { [weak self] in
                    self?.someDismissFunc()
                }
            }
        }
    }
    

    It is really messed up that there is no clean way of doing this as far as I can tell :(

    0 讨论(0)
  • 2020-12-03 17:51


    I've done this to get Done button click event from AVPlayerViewController.

    First of all, Create an extension of Notification.Name like bellow

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

    Now, Create an extension of AVPlayerViewController and override viewWillDisappear like bellow

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

    THIS IS FOR ONLY BASIC FUNCTIONALITY THAT HANDLES DONE BUTTON'S EVENT.

    happy coding...

    0 讨论(0)
  • 2020-12-03 17:52

    The easiest solution for me was to subclass AVPlayerViewController and add simple completion block to it.

    class MYVideoController: AVPlayerViewController {
    
      typealias DissmissBlock = () -> Void
      var onDismiss: DissmissBlock?
    
      override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        if isBeingDismissed {
          onDismiss?()
        }
      }
    }
    

    Usage

    ...
    let avPlayerViewController = MYVideoController()
    avPlayerViewController.onDismiss = { [weak self] in 
        print("dismiss")
    }
    
    0 讨论(0)
  • 2020-12-03 17:54

    I guess there are lots of ways to skin a cat. I needed to handle the 'Done' click event. In my case, I wanted to make sure I was able to hook into the event after the "X" was clicked and the AVPlayerViewController was COMPLETELY closed (dismissed).

    Swift 4.0

    protocol TableViewCellVideoPlayerViewControllerDelegate: class {
        func viewControllerDismissed()
    }
    
    class MyPlayerViewController: AVPlayerViewController {
    
        weak var navDelegate: TableViewCellVideoPlayerViewControllerDelegate?
    
        open override func viewDidDisappear(_ animated: Bool) {
            super.viewWillDisappear(animated)
            if self.isBeingDismissed {
                self.navDelegate?.viewControllerDismissed()
            }
        }
    }
    
    class TodayCell: UITableViewCell, SFSafariViewControllerDelegate,  TableViewCellVideoPlayerViewControllerDelegate {
        func viewControllerDismissed() {
            self.continueJourney()
        }
     }
    
    0 讨论(0)
  • 2020-12-03 17:56

    You can look into this Stackoverflow post and here is a github's project for reference. You will have to use this :

     self.showsPlaybackControls = true
    

    Please also have a look into Apple's documentation

    0 讨论(0)
提交回复
热议问题