Detect AVPlayerViewController Done Button?

I am writing a plugin for AVPlayer in ios. I need to know when the user clicks Finish in the AVPlayerViewController (I want to know when the user closes the video) and I do not have access to the AVPlayerViewController object. I checked the event and found that only the speed property in AVPlayer set to 0, but in the pause situation the speed is also set to 0. how do I find out these two situations? Thanks to everyone.

+6
source share
1 answer

I ran into a problem when I was developing the player in windowed mode. impossible now without tricks. So I used KVO to watch the contentOverlayView , which is actually a full-sized view of the AVPlayerViewController . The code is a bit complicated. In the example below, the playerView property is a xib / storyboard view on the view controller (see. Attached file).

 #import <AVKit/AVKit.h> #import <AVFoundation/AVFoundation.h> static NSString * const kBoundsProperty = @"bounds"; static void * kBoundsContext = &kBoundsContext; @interface ViewController () @property (nonatomic, strong) AVPlayerViewController *playerViewController; // View for windowed mode. @property (weak, nonatomic) IBOutlet UIView *playerView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.playerViewController = [[AVPlayerViewController alloc] init]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // Because in -viewDidLoad frame is unknown. [self loadPlayerView]; } - (void)loadPlayerView { NSURL *videoURL = [NSURL URLWithString:@"https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"]; AVPlayer *player = [[AVPlayer alloc] initWithURL:videoURL]; self.playerViewController.player = player; [player play]; [self addChildViewController:self.playerViewController]; [self.playerView addSubview:self.playerViewController.view]; // MARK: I would recommend to use constraints instead of frame. self.playerViewController.view.frame = self.playerView.bounds; [self.playerViewController.contentOverlayView addObserver:self forKeyPath:kBoundsProperty options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:kBoundsContext]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context { if (context == kBoundsContext) { CGRect oldBounds = [change[NSKeyValueChangeOldKey] CGRectValue]; CGRect newBounds = [change[NSKeyValueChangeNewKey] CGRectValue]; BOOL wasFullscreen = CGRectEqualToRect(oldBounds, [UIScreen mainScreen].bounds); BOOL isFullscreen = CGRectEqualToRect(newBounds, [UIScreen mainScreen].bounds); if (isFullscreen && !wasFullscreen) { if (CGRectEqualToRect(oldBounds, CGRectMake(0.0, 0.0, newBounds.size.height, newBounds.size.width))) { NSLog(@"Rotated fullscreen"); } else { NSLog(@"Entered fullscreen"); dispatch_async(dispatch_get_main_queue(), ^{ [[NSNotificationCenter defaultCenter] postNotificationName:@"DidEnterInFullscreen" object:nil]; }); } } else if (!isFullscreen && wasFullscreen) { NSLog(@"Exited fullscreen"); dispatch_async(dispatch_get_main_queue(), ^{ [[NSNotificationCenter defaultCenter] postNotificationName:@"DidExitFromFullscreen" object:nil]; }); } } } @end 

0
source

All Articles