【发布时间】:2011-06-28 04:51:45
【问题描述】:
你好,
我有一个简单的应用程序,其中包含带有两个 UIViewController 的 UITabBarController。两个 UIViewController 都是纵向的(不允许旋转)。一个 UIViewController 的 UIView 确实包含 MPMoviePlayerController 的视图,以允许在此视图内播放视频,并有可能通过控件 (MPMovieControlStyleEmbedded) 使其全屏显示。代码很简单,看起来确实像......
__moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@"MOVIE_URL"]];
__moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
__moviePlayer.view.frame = CGRectMake( 10, 10, 300, 200 );
__moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
__moviePlayer.shouldAutoplay = NO;
[__moviePlayer prepareToPlay];
[self.view addSubview:__moviePlayer.view];
...除非用户切换到我想允许旋转以允许横向播放的全屏播放,否则这确实工作得很好。旋转不起作用,因为 UITabBarController 不允许它(以及 UIViewControllers 也是)。
所以,我尝试了两种方法,但都没有按预期工作。
1) 子类 UITabBarController
我确实添加了属性 BOOL __allowRotation,如果它设置为 YES,我会在 UITabBarController 的 shouldAutorotateToInterfaceOrientation 方法中返回 YES。
我正在侦听 MPMoviePlayerDidEnterFullscreenNotification 和 MPMoviePlayerWillExitFullscreenNotification 通知以将此属性设置为 YES 和 NO。
它确实有效,但问题是,当用户以横向结束视频播放时,底层视图不会旋转回纵向。旋转回纵向的唯一方法是使用私有 API,这是不行的。
2) 视图/图层转换
我也尝试过监听 MPMoviePlayerDidEnterFullscreenNotification 和 MPMoviePlayerWillExitFullscreenNotification 通知。
当我收到 MPMoviePlayerDidEnterFullscreenNotification 时,我正在启动 UIDevice 方向通知以获取设备方向。我正在尝试根据当前设备方向转换 MPMoviePlayerController 的视图层,但它有点免疫,因为它什么都不做。我可以分配任何东西来转换属性,但它什么也不做。
它什么都不做不太正确。当我在旋转期间应用变换时,当我从全屏切换回嵌入式视频播放时,我可以看到此变换的效果。
3) 单独的 UIWindow
我还没有对此进行测试,但我发现 MPMoviePlayerController 为全屏播放创建了单独的 UIWindow,应该可以通过 [[UIApplication sharedApplication] windows] 访问它。这确实解释了为什么在全屏播放期间不应用转换。
但我很不喜欢这个解决方案,因为 UIWindow 无法被识别,我不想使用像 objectAtIndex:1 这样的魔术常量或对除主 UIWindows 之外的所有 UIWindows 应用转换等。
除了可以修改底层实现并且它将停止工作的事实。
问题
那么,问题来了,当底层UIView(即UIView的UIViewController)禁止旋转只允许纵向时,如何让MPMoviePlayerController全屏播放只旋转?
【问题讨论】:
标签: iphone cocoa-touch uitabbarcontroller rotation mpmovieplayercontroller