【问题标题】:iPhone app : MPMoviePlayer: PlayVideo in Landscape mode onlyiPhone 应用程序:电影播放器​​:仅在横向模式下播放视频
【发布时间】:2012-03-20 13:52:32
【问题描述】:

我有一个仅在纵向模式下运行的 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 中进行任何设置,而是管理视图方向。

标签: iphone objective-c ios4 mpmovieplayercontroller


【解决方案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,如果 isLandscapeOK==YES,则其他选项卡 VC 将回答纵向或横向。

【讨论】:

  • 参考您最后的说明:您也可以简单地以模态方式显示该视图控制器,因此没有任何所描述的方向副作用(标签栏、导航栏)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多