iPhone应用程序:MPMoviePlayer:仅在横向模式下播放视频

时间:2012-03-20 13:52:33

标签: iphone objective-c ios4 mpmovieplayercontroller

嗨我有一个只在纵向模式下运行的iPhone应用程序。但我想让mpmovieplayer只在横向模式下播放视频

我怎样才能做到这一点?

这是代码。

    NSString *path = [[NSBundle mainBundle] pathForResource:lblVideoName.text ofType:@"mp4" inDirectory:nil];
    NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
    NSLog(@"URL== %@",url);
    moviePlayer =  [[MPMoviePlayerController alloc]
                    initWithContentURL:url];


    moviePlayer.controlStyle = MPMovieControlStyleDefault;
    moviePlayer.shouldAutoplay = YES;
    [self.view addSubview:moviePlayer.view];
    [moviePlayer setFullscreen:YES animated:YES];

请帮助和建议。

由于

1 个答案:

答案 0 :(得分:1)

您可以在自己的视图控制器中显示影片,该控制器是为横向设置的。

// in the VC where the user indicates he wants to see a movie...
- (void)startTheMovie {
    // run a storyboard segue with a modal transition, or ...
    MyMovieVC *movieVC = [[MyMovieVC alloc] initWithNibName:@"MyMovieVC" bundle:nil];
    [self presentModalViewController:movieVC animated:YES];
}

// in MyMovieVC

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
        (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

// and the code that you wrote on view will appear

您可以在此界面中包含一个关闭按钮,或者,youtube方式是让它自行解散。你可以通过订阅完成的通知来做到这一点:

[[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(moviePlayerFinished:)
name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

然后,在完成的消息上

- (void)moviePlayerFinished:(NSNotification*)notification {
    [self dismissModalViewControllerAnimated:YES];
}

请注意,如果您在选项卡式界面中执行此操作,则所有选项卡(即使是不可见的选项卡)都需要同意让界面变为横向。这有点道理,但过去让我心疼。我没有一个漂亮的解决方案。我的方法是在我的AppDelegate上公开访问BOOL isLandscapeOK。如果isLandscapeOK == YES,此MovieVC会将其设置为YES,其他选项卡VC将回答纵向或横向。