开发者

MPMoviewPlayerController fullscreen playback rotation with underlying UIViewController with portrait mode only (rotation disallowed)

Hallo,

I have a simple application, which does contain UITabBarController with two UIViewControllers. Both UIViewControllers are portrait only (no rotation allowed). One UIViewController's UIView does contain MPMoviePlayerController's view to allow video playback inside this view with possibility to make it fullscreen via controls (MPMovieControlStyleEmbedded). The code is simple and does look like ...

__moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@"MOVIE_URL"]];
__moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
__moviePlayer.view.frame = CGRectMake( 10, 10, 300, 200 );
__moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
__moviePlayer.shouldAutoplay = NO;
[__moviePlayer prepareToPlay];  
[self.view addSubview:__moviePlayer.view];

... this does work perfectly unless user switches to fullscreen playback where I want to allow rotation to allow landscape playback too. Rotation doesn't work, because UITabBarController disallows it (and both UIViewControllers too).

So, I tried two approaches, but none of them does work as expected.

1) Subclassed UITabBarController

I did add property BOOL __allowRotation and if it is set to YES, I do return YES in UITabBarController's shouldAutorotateToInterfaceOrientation method.

I'm listening for MPMoviePlayerDidEnterFullscreenNotification and MPMoviePlayerWillExitFullscreenNotification notifications to set this property to YES and NO.

It does work, but the problem is, that when the user ends video playback in landscape, underlying view is not rotated back to portrait. The only way to rotate back to portrait is to use private API, which is no no.

2) View/layer transformation

I also did try to listen for MPMoviePlayerDidEnterFullscreenNotification and MPMoviePlayerWillExitFullscreenNotification notifications.

When I receive MPMoviePlayerDidEnterFullscreenNotification, I'm starting UIDevice orientation notifications to get device orientation. I'm trying to transform MPMoviePlayerController's view layer based on current device orientation, but it's kinda immune, because it does nothing. I can assign whatever to transform property, but it does nothing.

It does nothing is not quite correct. When I apply transformation during rotation, I can see effect of this transformation when I switch back from fullscreen to embedded video p开发者_开发知识库layback.

3) Separate UIWindow

I did not test this yet, but I've found somewhere that MPMoviePlayerController creates separate UIWindow for fullscreen playback, which should be accessible via [[UIApplication sharedApplication] windows]. This does explain why transformation is not applied during fullscreen playback.

But I quite dislike this solution, because the UIWindow can't be identified and I do not want to use magic constants like objectAtIndex:1 or apply transformation to all UIWindows except the main one, etc.

Beside the fact that the underlying implementation can be modified and it will stop working.

Question

So, the question is, how to allow MPMoviePlayerController fullscreen playback only rotation when underlying UIView (ie. UIView's UIViewController) prohibits rotation and allows portrait only?


I have a very similar situation. My app is portrait-only. But I need to show full-screen videos in any orientation, and then get back to the portrait orientation, after the user quits full-screen mode.

Split's method doesn't work for me, because I would like to let user watch the video in fullscreen and embedded, and switch between modes, not loosing the play position, and without any pauses.

I found this workaround:

First, I have a root UINavigationController subclass, that receives all messages regarding rotation.

I forbid the rotation in this controller with:

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

I am overriding the

- (id) initWithRootViewController:(UIViewController *)rootViewController; method. 

Adding the support for device orientation modifications:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(receivedRotate:) name: UIDeviceOrientationDidChangeNotification object: nil];

Now I have a handler receivedRotate: - that catches all the device rotations in spite of not auto-rotating to any orientations except portrait:

- (void) receivedRotate:(NSNotification*) notify {
    if(isVideoFullscreen) {
        UIDeviceOrientation toInterfaceOrientation = [[UIDevice currentDevice] orientation];
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.4];
        [UIView setAnimationCurve:2];

        if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
            self.view.transform = CGAffineTransformMakeRotation(-M_PI_2);
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];
            self.view.bounds = CGRectMake(0, 0, 1024, 768);
        } else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
            self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight]; 
            self.view.bounds = CGRectMake(0, 0, 1024, 768);            
        } else if(toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
            self.view.transform = CGAffineTransformMakeRotation(M_PI);
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortraitUpsideDown];
            self.view.bounds = CGRectMake(0, 0, 768, 1024);
        } else if(toInterfaceOrientation == UIInterfaceOrientationPortrait) {
            self.view.transform = CGAffineTransformMakeRotation(0);
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
            self.view.bounds = CGRectMake(0, 0, 768, 1024);
        }

        [UIView commitAnimations];
    }

}

I just check the rotations of the device, and rotate my view accordingly.

Then - how do the root controller knows, when the video is fullscreen? Just add two other message handlers to the init:

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

And the handlers themselves:

- (void) willEnterFullscreen: (NSNotification *) notify {
    isVideoFullscreen = YES;
}

- (void) willExitFullscreen: (NSNotification *) notify {
    self.view.transform = CGAffineTransformMakeRotation(0);
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
    self.view.bounds = CGRectMake(0, 0, 768, 1024);    
    isVideoFullscreen = NO;
}

When exiting fullscreen - we restore the portrait orientation. So, this works for me, hope it will help someone.


You can try to present new UIViewController (with shouldAutorotate YES) modally and add __moviePlayer.view into this controller when it sends MPMoviePlayerWillEnterFullscreenNotification. Do the opposite when moviePlayer exits fullscreen.


Register for MPMoviePlayerWillExitFullscreenNotification and MPMoviePlayerWillEnterFullscreenNotification in app delegate and handle the orientation using an instance variable.


-(void)moviePlayerFullScreen:(NSNotification *)notification
{
    if ([notification.name isEqualToString:@"MPMoviePlayerWillEnterFullscreenNotification"]) {

        self.supportedOrientation=UIInterfaceOrientationMaskAll;

    }
    else if ([notification.name isEqualToString:@"MPMoviePlayerWillExitFullscreenNotification"])
    {
        self.supportedOrientation=UIInterfaceOrientationMaskPortrait;

    }
 }
 - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window  {
    return self.supportedOrientation;
}


The MPMoviePlayerViewController has its own function to present videos modally:

NSURL *videoURL = [NSURL fileURLWithPath:video.path];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];

//Calls for movie playback once video is finished
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moviePlayBackDidFinish:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:moviePlayer];
playerView = [[MPMoviePlayerViewController alloc]init];
[moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
[playerView setView:moviePlayer.view];

[moviePlayer.view setFrame: self.view.bounds];  
[self presentMoviePlayerViewControllerAnimated:playerView];

[moviePlayer play];
NSLog(@"playing video view");


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.....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜