最近在做播放视频的功能,选择在webview中加载视频,视频可以全屏播放,但是当设备横放时,播放内容不能随着设备的旋转而转成横屏的全屏播放模式。需要的效果是:应用其他页面只能是竖屏模式,当全屏播放视频时,要适应横屏。
由于使用webview加载视频,不能直接控制播放视频的控件,没有办法知道视频是否进入全屏模式。
经过网上一番搜索,找到以下几个方法,使用通知获取进入全屏状态:
一:
<span style="font-family:KaiTi_GB2312;font-size:14px;"> [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(videoStarted:) name:MPMoviePlayerDidEnterFullscreenNotification object:nil];// 播放器即将播放通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(videoFinished:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];// 播放器即将退出通知</span>
二:<span style="font-family:KaiTi_GB2312;font-size:14px;"> [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoStarted:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoStarted:) name:@"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];
</span>
以上两种方法在iOS8中都接收不到通知,用下面这种方法可以
<span style="font-family:KaiTi_GB2312;font-size:14px;">[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoStarted:) name:UIWindowDidBecomeVisibleNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoFinished:) name:UIWindowDidBecomeHiddenNotification object:nil];</span>
-(void)youTubeStarted:(NSNotification *)notification
{
NSLog(@"全屏开始");
AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
app.isFull = YES;
}
-(void)youTubeFinished:(NSNotification *)notification{
NSLog(@"全屏结束");
AppDelegate *appDelegate =(AppDelegate *) [[UIApplication sharedApplication] delegate];
appDelegate.isFull =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];
}
}
- @interface AppDelegate :UIResponder
- {
- BOOL _isFull; // 是否全屏
- }
- @property (nonatomic)BOOL isFull
- - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
- if(_isFull)
- returnUIInterfaceOrientationMaskAll;
- returnUIInterfaceOrientationMaskPortrait;
- }