iPhone应用:MPMoviePlayer:仅以横向模式播放视频

3

我有一个只能在竖屏模式下运行的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];

你不需要在MPMovieController中进行任何设置,而是要管理视图方向。 - Parth Bhatt
1个回答

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,这个MovieVC会将其设置为YES,其他选项卡VC会在isLandscapeOK == YES时回答纵向或横向。

关于您的最后一条注释:您也可以将该视图控制器模态显示,因此不会出现任何描述的方向副作用(选项卡栏、导航栏)。 - Till

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接