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 find next button , previous button and done button click event.
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 find next button , previous button and done button click event.
Officially there is no Done Button click event, a engineer from Apple said about it here.
As for my research, I found out that there is one but really indirect way to get an event if Done Button was clicked.
I found out that the only variable of AVPlayerViewController, which is changing when done button is clicked is AVPlayerViewController.view.frame
.
First of all view.frame is appearing in the center of the viewController.
If you present it with animated : true
it goes to the bottom of viewController and the back to the center. When done is clicked it goes back to the bottom.
If you present it with animated : false
there will be only 2 changes: frame will be at the center of viewController when you start to play video, and at the bottom, when Done is clicked.
So if you add observer to the AVPlayerViewController.view.frame
in the callback to present(PlayerViewController, animated : true)
you'll get only one call of the observer, right when done button is clicked and video view will be out of the screen.
In my case AVPlayerViewController was presented modally with animation. Code below worked for me:
Swift 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
}
Also, I found out when you click Done, AVPlayerViewController is not dismissed and you can see it in ParentViewController.presentedViewController
, so you can't add observer to this property
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...
This is a small improvement from the answer by @vmchar:
We can use the .isBeingDismissed method to ensure that the AVPlayerViewController is being closed instead of analysing the frame.
...
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)
...
Then to observe the value
override func observeValue(forKeyPath keyPath: String?,
of object: Any?,
change: [NSKeyValueChangeKey : Any]?,
context: UnsafeMutableRawPointer?)
{
if (playerViewController!.isBeingDismissed) {
// Video was dismissed -> apply logic here
}
}
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.
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