Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing landscape orientation on fullscreen MPMoviePlayerController prevents correct rotation when exiting fullscreen

I have an iPhone application (iOS6+) that supports all interface orientations. However, only landscape orientation should be supported when an MPMoviePlayerController is playing a video fullscreen.

I found the following solution on Stack Overflow and it works.

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

...

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

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

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

However, an annoying problem persists, namely that if the video exits fullscreen in portrait orientation (after playing in forced landscape), the underlying view doesn't rotate back. I have to manually rotate the device to landscape and back to portrait to trigger updating of the orientation. Is there some way I can trigger this kind of update programatically?

The following set of screenshots should illustrate what I mean:

enter image description hereenter image description hereenter image description here

NB: For various reasons, using MPMoviePlayerViewController is not possible.

like image 999
svth Avatar asked Feb 20 '14 14:02

svth


3 Answers

Hi all I had same problem I resolved it -

Here is my complete code....

You need to first change in appdelegate:

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([[[NowPlaying sharedManager] playerViewController] allowRotation])//Place your condition here
{
    return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskPortrait;
}

Register Notifications for the full screen control:

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

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

Then add line of code in the player controller:

- (void)moviePlayerWillEnterFullscreenNotification:(NSNotification *)notification
{
dispatch_async(dispatch_get_main_queue(), ^
               {
                   self.allowRotation = YES;
               });
}



- (void)moviePlayerWillExitFullscreenNotification:(NSNotification *)notification
{
self.allowRotation = NO;
[self.moviePlayerController setControlStyle:MPMovieControlStyleNone];

dispatch_async(dispatch_get_main_queue(), ^
               {

                   //Managing GUI in pause condition
                       if (self.currentContent.contentType == TypeVideo && self.moviePlayerController.playbackState == MPMoviePlaybackStatePaused)
                   {
                       [self.moviePlayerController pause];
                       if (self.playButton.selected)
                           self.playButton.selected = NO;
                   }
                   self.view.transform = CGAffineTransformMakeRotation(0);
                   [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
                   self.view.bounds = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
               });
}

This code is tested in iOS6 and iOS7 working fine. Thanks :)

Please let me know if there is any question.....

like image 98
Vijay Sharma Avatar answered Nov 10 '22 00:11

Vijay Sharma


You need to subclass and provide landscape as supported interface orientation.

@interface MyMovieViewController : MPMoviePlayerViewController
@end

@implementation MyMovieViewController

- (BOOL)shouldAutorotate 
{
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations 
{
    return UIInterfaceOrientationMaskLandscape;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}

@end
like image 41
Srikanth Avatar answered Nov 09 '22 22:11

Srikanth


You could try to "force" an orientation refresh to let the system handle the correct orientation for you:

- (void)forceOrientationRefresh
{
    // Force orientation refresh
    [self presentModalViewController:[UIViewController new]
                            animated:NO];
    [self dismissModalViewControllerAnimated:NO];
}

It's hack-ish but it works.

like image 2
Rivera Avatar answered Nov 09 '22 23:11

Rivera