最近在做webview加載html播放YouTube視頻,由于我們的app默認是豎屏的航闺,在播放視頻的時候點擊全屏按鈕調(diào)取系統(tǒng)的AVPlayerViewController后褪测,顯示全屏,但是不能豎屏潦刃,導(dǎo)致播放區(qū)域就那么大侮措,看著有些不爽,就想怎么才能橫屏看乖杠,播放區(qū)域大了分扎,看的才夠過癮,于是在網(wǎng)上搜了一些資料胧洒,最終完美解決了
1.首先是在AppDelegate中去設(shè)置橫屏豎屏模式
//控制是否允許橫屏
@property (nonatomic,assign) BOOL allowRotation;
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (self.allowRotation) {
return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskPortrait;
}
在你調(diào)用播放器的地方去控制這個allowRotation屬性畏吓,系統(tǒng)會執(zhí)行適配屏幕的方法
2.在播放視頻的控制器中需要做以下處理,由于我們不方便取得AVPlayeView卫漫,所以全屏?xí)r候的監(jiān)聽對象就變成了UIWindow菲饼,因為全屏?xí)r播放view是加到UIWindow上的
//注冊通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(begainFullScreen) name:UIWindowDidBecomeVisibleNotification object:nil];//進入全屏
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endFullScreen) name:UIWindowDidBecomeHiddenNotification object:nil];//退出全屏
//實現(xiàn)方法
#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];
}
}
參考文章: