Search code examples
iosobjective-ccocoa-touchmpmovieplayercontroller

Can be force MPMoviePlayerController to landscape when click on Fullscreen button in iOS


I create a MPMoviePlayerController in detailView(UIVIew), now i want to force MPMoviePlayerController to landscape view when user click on FullScreen button. Can i do that? Please give me any suggestion. Thanks in advance.And this is my code to create :

   NSURL *movieURL = [NSURL URLWithString:previewString];
    movieController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    [movieController.view setFrame:CGRectMake(10,130, 275 , 150)];
    movieController.view.backgroundColor = [UIColor grayColor];
    [detailview addSubview:movieController.view];
    [movieController prepareToPlay];
    movieController.shouldAutoplay = NO;

and willEnterFullscreen () function:

    - (void)willEnterFullscreen:(NSNotification*)notification {
    NSLog(@"willEnterFullscreen");
    donepress = YES;
    // nothing
}

I tried search but still do not have any good answer. Please help me. thanks so much


Solution

  • YES, you can do that with two notification observer to change the full orientation.

    First, Add two notification observer to your AppDelegate didFinishLaunchingWithOptions method:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillEnterFullscreenNotification:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillExitFullscreenNotification:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
    

    Second, Add the method and property

    - (void) moviePlayerWillEnterFullscreenNotification:(NSNotification*)notification {
        self.allowRotation = YES;
    }
    - (void) moviePlayerWillExitFullscreenNotification:(NSNotification*)notification {
        self.allowRotation = NO;
    }
    

    Third, override the supportedInterfaceOrientationsForWindow method, you can return whatever orientation you want

    -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
        if (self.allowRotation) {
            return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
        }
        return UIInterfaceOrientationMaskPortrait;
    }