Mohammed Abul-huda Mohammed Abul-huda - 2 months ago 17
Objective-C Question

AVPlayerviewcontroller ios objective c

I have an application for both ipad iphones, ipad users always landscape mode is enabled and portrait for iphone users, now what I am trying to achieve is in the iphone application I play a video using AVPlayerViewController but since the application is locked in portrait inside the appdelegate, when I press the full screen button on the player it will just stay opn portrait mode I want to make it landscape I tried all the answers I found in stackoverflow but no luck any idea on how to make it work ?

Answer

I solved this problem with an NSTimer I made a Method that will be called every second I know not good practice but this is what I did:

    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(yourMethod) userInfo:nil repeats:YES];

-(void)yourMethod{
if(!_fullscreen){
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}else{
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
}   }

the _fullscreen is a flag that will change when the AVplayer goes to fullscreen view or normal view.

and to track the AVplayer states I used an Observer @"bounds" to check what size the AVPlayer screen is.

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

                if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){

                }else{
                    _fullscreen = YES;
                    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
                }
            }
        }
        else if (!isFullscreen && wasFullscreen) {
            NSLog(@"exited fullscreen");
            if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){

            }else{
                _fullscreen = NO;
                [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];

            }
        }
    }
}}
Comments