I'm writing Plugin for AVPlayer
in ios. I need to know when user click on Done button in AVPlayerViewController
( I want to know when user close video) and I don't access to AVPlayerViewController
object. I checked event and found only rate property in AVPlayer
set to 0 but in pause situation also the rate set to 0 . how I figure out these two situations ?
thanks all.
Asked
Active
Viewed 3,120 times
5

MAhipal Singh
- 4,745
- 1
- 42
- 57

vahid
- 446
- 6
- 21
-
the [solution](http://stackoverflow.com/a/41281453/4593553) work for me. – Jerome Feb 17 '17 at 08:41
-
Possible duplicate of [Done button click event in AVPlayerViewController](https://stackoverflow.com/questions/38565412/done-button-click-event-in-avplayerviewcontroller) – Jonathan Cabrera Oct 16 '18 at 21:35
1 Answers
0
I met the problem when I was developing the player in windowed mode. It is currently impossible without tricks. Therefore, I used KVO to observe contentOverlayView
which is actually a full-size view of AVPlayerViewController
. Code is a bit complicated. In the example below, playerView
property is a view from xib/storyboard on the view controller (see attached).
#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

Oleg
- 488
- 4
- 14