如何检测AVPlayerViewController的全屏模式

时间:2014-10-12 21:41:42

标签: ios8 avfoundation

如何检测用户何时按下AVPlayerViewController的展开图标? 我想知道电影播放何时进入全屏模式。

5 个答案:

答案 0 :(得分:5)

还可以观察bounds playerViewController.contentOverlayView并将其与[UIScreen mainScreen].bounds进行比较,例如:

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");
            }
        }
    }
}

答案 1 :(得分:4)

您可以使用KVO观察videoBounds实例的AVPlayerViewController属性。

修改

最基本的例子
[_myPlayerViewController addObserver:self forKeyPath:@"videoBounds" options:0 context:nil];

答案 2 :(得分:4)

在swift中,音频和视频:

将此观察者添加到初始化函数中,例如viewDidLoad或didMoveToSuperview:

//Observe if player changed bounds and maybe went to fullscreen
playerController.contentOverlayView!.addObserver(self, forKeyPath: "bounds", options: NSKeyValueObservingOptions.New, context: nil)

这个函数在类上:

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")
            }
        }
    }
}

答案 3 :(得分:1)

Swift 2.2 创建并使用AVPlayerViewController的子类。

ImageButton Logo; // Logo??
Drawabable drawableLogo = Logo.getDrawable();

ImageButton logo;  
logo = (ImageButton ) findViewById(R.id.mylogo):
Drawabable drawableLogo = logo.getDrawable();

答案 4 :(得分:-1)

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;
            }
        }
    }