How can I detect when the user press the expand icon of the AVPlayerViewController? I want to know when the movie playing is entering the fullscreen mode.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
It is also possible to observe bounds
of playerViewController.contentOverlayView
and compare that to [UIScreen mainScreen].bounds
, e.g.:
self.playerViewController = [AVPlayerViewController new];
// do this after adding player VC as a child VC or in completion block of -presentViewController:animated:completion:
[self.playerViewController.contentOverlayView addObserver:self forKeyPath:@"bounds" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];
...
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *, id> *)change context:(void *)context {
if (object == self.playerViewController.contentOverlayView) {
if ([keyPath isEqualToString:@"bounds"]) {
CGRect oldBounds = [change[NSKeyValueChangeOldKey] CGRectValue], newBounds = [change[NSKeyValueChangeNewKey] CGRectValue];
BOOL wasFullscreen = CGRectEqualToRect(oldBounds, [UIScreen mainScreen].bounds), isFullscreen = CGRectEqualToRect(newBounds, [UIScreen mainScreen].bounds);
if (isFullscreen && !wasFullscreen) {
if (CGRectEqualToRect(oldBounds, CGRectMake(0, 0, newBounds.size.height, newBounds.size.width))) {
NSLog(@"rotated fullscreen");
}
else {
NSLog(@"entered fullscreen");
}
}
else if (!isFullscreen && wasFullscreen) {
NSLog(@"exited fullscreen");
}
}
}
}
回答2:
You can use KVO to observe the videoBounds
property of your AVPlayerViewController
instance.
Edit The most basic example being
[_myPlayerViewController addObserver:self forKeyPath:@"videoBounds" options:0 context:nil];
回答3:
In swift, both for audio and video:
Add this observer in a initialization function such as viewDidLoad, or didMoveToSuperview:
//Observe if player changed bounds and maybe went to fullscreen
playerController.contentOverlayView!.addObserver(self, forKeyPath: "bounds", options: NSKeyValueObservingOptions.New, context: nil)
and this function on the class:
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if keyPath == "bounds" {
let rect = change!["new"] as! NSValue
if let playerRect: CGRect = rect.CGRectValue() as CGRect {
if playerRect.size == UIScreen.mainScreen().bounds.size {
print("Player in full screen")
isVideoInFullScreen = true
} else {
print("Player not in full screen")
}
}
}
}
回答4:
Swift 2.2 Create and use subclass of AVPlayerViewController.
class YouVideoPlayer: AVPlayerViewController {
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if view.bounds == contentOverlayView?.bounds {
//code
}
}
回答5:
self.frameChecker = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(checkContetnOverlay) userInfo:nil repeats:YES];
-(void)checkContetnOverlay{
BOOL currentPlayerIsFullscreen = (self.avVideoPlayer.contentOverlayView.frame.size.width>1000 || self.avVideoPlayer.contentOverlayView.frame.size.height>1000);
//works with these values only for iPad
if (((PlayerViewController*)self.parentViewController).playerIsInfullScreen != currentPlayerIsFullscreen) {
if (currentPlayerIsFullscreen) {
NSLog(@"CUSTOM PLAYER (av) : changed to fullscreen");
self.avVideoPlayer.showsPlaybackControls = YES;
}else{
NSLog(@"CUSTOM PLAYER (av) : changed to minimised");
self.avVideoPlayer.showsPlaybackControls = YES;
}
}
}