Done button click event in AVPlayerViewController

前端 未结 9 853
广开言路
广开言路 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:58

    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
        } 
    } 
    
    0 讨论(0)
  • 2020-12-03 17:59

    Objective c version: (Based on vmchar answer)

    - (void)viewDidLoad
    {
        [super viewDidLoad];
            NSURL *url = [NSURL fileURLWithPath:path];
            AVPlayer *player = [AVPlayer playerWithURL:url];
            AVPlayerViewController *AVPlayerVc = [[AVPlayerViewController alloc] init];
            if (AVPlayerVc) {
                [self presentViewController:AVPlayerVc animated:YES completion:^{
                    [player play];
                    AVPlayerVc.player = player;
                    [AVPlayerVc addObserver:self forKeyPath:@"view.frame" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial) context:nil];
                }];
    
            }
    }
    
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
        if ([keyPath isEqualToString:@"view.frame"]) {
            CGRect newValue = [change[NSKeyValueChangeNewKey]CGRectValue];
            CGFloat y = newValue.origin.y;
            if (y != 0) {
                  NSLog(@"Video Closed");
            }
         }
    }
    
    0 讨论(0)
  • 2020-12-03 18:02

    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

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