开发者

Reverting to portrait after Done pressed on movie player?

I've got a MPMoviePlayer working. It is designed to show a postage-stamp size movie as a subview in the view. When the phone is rotated into landscape, it goes into full screen mode. And when the phone is in portrait it goes into postage-stamp portrait mode.

The only problem is when I press Done when in landscape mode, it stays in landscape, with the postage stamp sized movie, rather than kick back into portrait..

Here's some of my code:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 


    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation ==  UIInterfaceOrientationLandscapeLeft) {
        [moviePlayerController setFullscreen:YES animated:YES];
    } else
    {
        [moviePlayerController setFullscreen:NO animated:YES];

    }


}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{

        r开发者_开发问答eturn interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft;

}

How would I get it to kick into portrait mode after pressing Done?


@cannyboy...you just need to use below method in your APPDelegate.m if your application only works with portrait mode

- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if ([[window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]])
{
    //NSLog(@"in if part");
    return UIInterfaceOrientationMaskAllButUpsideDown;
}
else
{
    //NSLog(@"in else part");
    return UIInterfaceOrientationMaskPortrait;
}}


I was the same problem and now I am telling you how I solved it. I don't know the below method is right or wrong but it work fine.

  • Instead of opening MPMoviePlayer in view open it in a new viewController. I mean create a new UIViewController to show the movie and push it some how so that user will not understood that they are redirecting into a new screen.
  • Disable the parent screen for landscape mode and allow MovieViewController to landscape.
  • When user press the done button or close button simply pop the viewController and as the previous screen don't support landscape mode so the screen will automatically shows in portrait mode.


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


- (void)_moviePlayerWillExitFullscreen:(NSNotification *)notification 
{
    CGFloat ios = [[[UIDevice currentDevice] systemVersion] floatValue];
    CGFloat min = 5.0;
    if (ios >= min)
    {
        if (self.interfaceOrientation != UIInterfaceOrientationPortrait)
        {  
            if([self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationPortrait])
            {
                [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
                [self willRotateToInterfaceOrientation:self.interfaceOrientation duration:0];
                [UIViewController attemptRotationToDeviceOrientation];
            } 
        }
    }   
}

Note that this only works in ios 5.0 and later, and you will get a warning that setOrientation is not supported, but it works pretty well


One way you could try is to force your device to think it is in portrait mode. To do this, try using:

[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait];

If you do not want your device to show landscape while not playing a movie, you will also have to add some logic to your code that you have shown above to only change to landscape if the movie is playing.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜