MPMoviewPlayerController 全屏播放旋转,底层 UIViewController 仅具有纵向模式(不允许旋转) [英] MPMoviewPlayerController fullscreen playback rotation with underlying UIViewController with portrait mode only (rotation disallowed)

查看:29
本文介绍了MPMoviewPlayerController 全屏播放旋转,底层 UIViewController 仅具有纵向模式(不允许旋转)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有一个简单的应用程序,其中包含带有两个 UIViewController 的 UITabBarController.两个 UIViewController 都是纵向的(不允许旋转).一个 UIViewController 的 UIView 确实包含 MPMoviePlayerController 的视图,以允许在此视图内播放视频,并有可能通过控件 (MPMovieControlStyleEmbedded) 使其全屏显示.代码很简单,看起来确实像......

I have a simple application, which does contain UITabBarController with two UIViewControllers. Both UIViewControllers are portrait only (no rotation allowed). One UIViewController's UIView does contain MPMoviePlayerController's view to allow video playback inside this view with possibility to make it fullscreen via controls (MPMovieControlStyleEmbedded). The code is simple and does look like ...

__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 也是).

... this does work perfectly unless user switches to fullscreen playback where I want to allow rotation to allow landscape playback too. Rotation doesn't work, because UITabBarController disallows it (and both UIViewControllers too).

所以,我尝试了两种方法,但都没有按预期工作.

So, I tried two approaches, but none of them does work as expected.

1) 子类 UITabBarController

我确实添加了属性 BOOL __allowRotation,如果它设置为 YES,我会在 UITabBarController 的 shouldAutorotateToInterfaceOrientation 方法中返回 YES.

I did add property BOOL __allowRotation and if it is set to YES, I do return YES in UITabBarController's shouldAutorotateToInterfaceOrientation method.

我正在侦听 MPMoviePlayerDidEnterFullscreenNotification 和 MPMoviePlayerWillExitFullscreenNotification 通知以将此属性设置为 YES 和 NO.

I'm listening for MPMoviePlayerDidEnterFullscreenNotification and MPMoviePlayerWillExitFullscreenNotification notifications to set this property to YES and NO.

它确实有效,但问题是,当用户以横向结束视频播放时,底层视图不会旋转回纵向.旋转回纵向的唯一方法是使用私有 API,这是不行的.

It does work, but the problem is, that when the user ends video playback in landscape, underlying view is not rotated back to portrait. The only way to rotate back to portrait is to use private API, which is no no.

2) 视图/图层转换

我也尝试过监听 MPMoviePlayerDidEnterFullscreenNotification 和 MPMoviePlayerWillExitFullscreenNotification 通知.

I also did try to listen for MPMoviePlayerDidEnterFullscreenNotification and MPMoviePlayerWillExitFullscreenNotification notifications.

当我收到 MPMoviePlayerDidEnterFullscreenNotification 时,我正在启动 UIDevice 方向通知以获取设备方向.我正在尝试根据当前设备方向转换 MPMoviePlayerController 的视图层,但它有点免疫,因为它什么都不做.我可以分配任何东西来转换属性,但它什么也不做.

When I receive MPMoviePlayerDidEnterFullscreenNotification, I'm starting UIDevice orientation notifications to get device orientation. I'm trying to transform MPMoviePlayerController's view layer based on current device orientation, but it's kinda immune, because it does nothing. I can assign whatever to transform property, but it does nothing.

它什么都不做不太正确.当我在旋转期间应用变换时,当我从全屏切换回嵌入式视频播放时,我可以看到此变换的效果.

It does nothing is not quite correct. When I apply transformation during rotation, I can see effect of this transformation when I switch back from fullscreen to embedded video playback.

3) 单独的 UIWindow

我还没有对此进行测试,但我发现 MPMoviePlayerController 为全屏播放创建了单独的 UIWindow,应该可以通过 [[UIApplication sharedApplication] windows] 访问它.这确实解释了为什么在全屏播放期间不应用转换.

I did not test this yet, but I've found somewhere that MPMoviePlayerController creates separate UIWindow for fullscreen playback, which should be accessible via [[UIApplication sharedApplication] windows]. This does explain why transformation is not applied during fullscreen playback.

但我很不喜欢这个解决方案,因为 UIWindow 无法被识别,我不想使用像 objectAtIndex:1 这样的魔法常量或对除主 UIWindows 之外的所有 UIWindows 应用转换等.

But I quite dislike this solution, because the UIWindow can't be identified and I do not want to use magic constants like objectAtIndex:1 or apply transformation to all UIWindows except the main one, etc.

除了可以修改底层实现并且它将停止工作的事实之外.

Beside the fact that the underlying implementation can be modified and it will stop working.

问题

那么,问题来了,当底层UIView(即UIView的UIViewController)禁止旋转只允许纵向时,如何让MPMoviePlayerController全屏播放只旋转?

So, the question is, how to allow MPMoviePlayerController fullscreen playback only rotation when underlying UIView (ie. UIView's UIViewController) prohibits rotation and allows portrait only?

推荐答案

您可以尝试以模态方式呈现新的 UIViewController (with shouldAutorotate YES),并在发送 MPMoviePlayerWillEnterFullscreenNotification 时将 __moviePlayer.view 添加到此控制器中.当moviePlayer 退出全屏时,执行相反的操作.

You can try to present new UIViewController (with shouldAutorotate YES) modally and add __moviePlayer.view into this controller when it sends MPMoviePlayerWillEnterFullscreenNotification. Do the opposite when moviePlayer exits fullscreen.

这篇关于MPMoviewPlayerController 全屏播放旋转,底层 UIViewController 仅具有纵向模式(不允许旋转)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆