视频播放后 iPhone 强制定向

Posted

技术标签:

【中文标题】视频播放后 iPhone 强制定向【英文标题】:iPhone forcing orientation after video playback 【发布时间】:2011-07-15 14:36:37 【问题描述】:

我遇到了一个问题,应该很容易解决。这是我的代码:

-(IBAction)playMovie:(id)sender

    NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"myMovie" ofType:@"MOV"];  
    NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];  
    moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];  

    [[NSNotificationCenter defaultCenter] addObserver:self  
                                             selector:@selector(moviePlaybackComplete:)  
                                                 name:MPMoviePlayerPlaybackDidFinishNotification  
                                               object:moviePlayerController];  

    [self.view addSubview:moviePlayerController.view];  
    //[moviePlayerController setOrientation:UIInterfaceOrientationLandscapeLeft];
    moviePlayerController.fullscreen = YES;  
    moviePlayerController.scalingMode = MPMovieScalingModeFill;    

    [moviePlayerController play]; 



- (void)moviePlaybackComplete:(NSNotification *)notification  
  

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];

    moviePlayerController = [notification object];  
    [[NSNotificationCenter defaultCenter] removeObserver:self  
                                                    name:MPMoviePlayerPlaybackDidFinishNotification  
                                                  object:moviePlayerController];  

    [moviePlayerController.view removeFromSuperview];  
    [moviePlayerController release];  
  

还有方向:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

    if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) 
        return(YES);
    

    if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) 
        return([moviePlayerController isFullscreen]);
    




    return(NO);


这首先可以按预期工作。方向最初被强制为纵向,然后电影播放和横向允许电影旋转并以横向观看。然后单击完成并完成电影后,界面将保持横向。我需要将其强制恢复为纵向模式...我见过类似的解决方案

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];

现在似乎从 ios 4.0 开始被弃用

理想情况下,一旦电影完成方法触发,它应该自动旋转到纵向!

有什么解决办法吗?

【问题讨论】:

【参考方案1】:

试试这个:

[[NSNotificationCenter defaultCenter] addObserver:self  
                                         selector:@selector(moviePlaybackComplete:)  
                                         name:MPMoviePlayerWillExitFullscreenNotification  
                                         object:moviePlayerController];

【讨论】:

以上是关于视频播放后 iPhone 强制定向的主要内容,如果未能解决你的问题,请参考以下文章