WebView加载html播放视频的横屏问题
2016-12-22 本文已影响479人
皮乐皮儿
最近在做webview加载html播放YouTube视频,由于我们的app默认是竖屏的,在播放视频的时候点击全屏按钮调取系统的AVPlayerViewController后,显示全屏,但是不能竖屏,导致播放区域就那么大,看着有些不爽,就想怎么才能横屏看,播放区域大了,看的才够过瘾,于是在网上搜了一些资料,最终完美解决了
1.首先是在AppDelegate中去设置横屏竖屏模式
//控制是否允许横屏
@property (nonatomic,assign) BOOL allowRotation;
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (self.allowRotation) {
return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskPortrait;
}
在你调用播放器的地方去控制这个allowRotation属性,系统会执行适配屏幕的方法
2.在播放视频的控制器中需要做以下处理,由于我们不方便取得AVPlayeView,所以全屏时候的监听对象就变成了UIWindow,因为全屏时播放view是加到UIWindow上的
//注册通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(begainFullScreen) name:UIWindowDidBecomeVisibleNotification object:nil];//进入全屏
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endFullScreen) name:UIWindowDidBecomeHiddenNotification object:nil];//退出全屏
//实现方法
#pragma - mark 进入全屏
-(void)begainFullScreen
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = YES;
}
#pragma - mark 退出全屏
-(void)endFullScreen
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = NO;
//强制归正:
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val =UIInterfaceOrientationPortrait;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
参考文章: