iOS Developer

UIWebview,MPMoviePlayerControlle

2017-08-02  本文已影响0人  涛_3c6a

问题说明:
项目工程仅支持竖屏,其中某个页面有UIWebview,在Webview中播放视频,要求视频全局播放时,可以横屏,并且视频结束全局播放时,程序依然只能竖屏。
一. Device Orientation
Target --> General --> Deployment info 中配置Device Orientation 如下:


1.png

二. 重写APPDelegate的supportedInterfaceOrientationsForWindow方法。

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window {
    id presentedViewController = [window.rootViewController presentedViewController];
    NSString *className = presentedViewController ? NSStringFromClass([presentedViewController class]) : nil;
    if (window && ([className isEqualToString:@"MPInlineVideoFullscreenViewController"] || [className isEqualToString:@"AVFullScreenViewController"])) {
    // 使用MPMoviePlayerController 播放时为MPInlineVideoFullscreenViewController,使用UIWebview播放视频时为AVFullScreenViewController 
        return UIInterfaceOrientationMaskAll;
    } else {
        return UIInterfaceOrientationMaskPortrait;
    }
}

说明:

  1. 网上有通过UIMoviePlayerControllerDidEnterFullscreenNotification监听视频播放器进入全屏模式,但是此方案在iOS8以后就失效了。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPlayEnterFullscreen) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
  1. 还有通过UIWindowDidBecomeVisibleNotification监听视频播放器进入全屏。 个人认为此方法并不准确。其原理在于,当视频播放器进入全屏时,会创建新的window,此处监听新window的可见状态。但是屏幕上除了此新window外,还有可能出现其他的window,这种方法无法区分。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(begainFullScreen) name:UIWindowDidBecomeVisibleNotification object:nil];//进入全屏
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endFullScreen) name:UIWindowDidBecomeHiddenNotification object:nil];//退出全屏

三. 当使用MPMoviePlayerController播放视频时,也可以通过下面的方案实现。
在APPDelegate中实现如下方法。

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPlayEnterFullscreen) name:@"" object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPlayExitFullscreen) name:MPMoviePlayerDidExitFullscreenNotification object:nil];

- (void)videoPlayEnterFullscreen{
    self.isFull = YES;
}
- (void)videoPlayExitFullscreen{
    self.isFull = NO;
}

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window {
    
    if (self.isFull) {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    return UIInterfaceOrientationMaskPortrait;
}

参考:
https://stackoverflow.com/questions/28465612/how-can-i-set-landscape-orientation-when-playing-a-video-from-webview
http://blog.csdn.net/oiken/article/details/48770547

文章内容部分参考自网络,如有版权问题请联系linjitaoyt@gmail.com

上一篇下一篇

猜你喜欢

热点阅读