关于iOS只在视频全屏播放下允许横屏的解决方法

今天在利用MPMoviePlayController写一个小Demo时发现了一点点小的问题,因为整个项目都是只支持竖屏浏览,所以就导致了在播放视频的时候全屏进入时旋转屏幕视频却不能横屏显示,无疑,在视频体验这方面这种体验是极差的。点进去查看MPMovieController里面的属性,并没有发现有关于横竖屏显示的,倒是发现了一些关于视频全屏以及退出全屏的通知,后来百度了一下,找到了其解决方法。思路其实并不困难,只是在设置横竖屏的时候要将方法设置在appdelegate中,然后监听视频是否进入全屏,赋值给appdelegate自己定义的一个布尔值判断显示哪一种方案。
代码如下:

//.h
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, assign) BOOL allowRotation;
@end
//.m
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window

{
    if (self.allowRotation) {
        return UIInterfaceOrientationMaskPortrait |UIInterfaceOrientationMaskLandscapeLeft |UIInterfaceOrientationMaskLandscapeRight;
    }
    return UIInterfaceOrientationMaskPortrait;
}


- (NSUInteger)supportedInterfaceOrientations

{
    return UIInterfaceOrientationMaskPortrait |UIInterfaceOrientationMaskLandscapeLeft |UIInterfaceOrientationMaskLandscapeRight;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

{
    return UIInterfaceOrientationPortrait;
}

//在MPMovieController所在的类里添加
/**
 *  添加通知监控媒体播放控制器状态
 */
-(void)addNotification{
    NSNotificationCenter *noti = [NSNotificationCenter defaultCenter];
    [noti addObserver:self    selector:@selector(moviePlayerWillEnterFullscreenNotification:)
                 name:MPMoviePlayerWillEnterFullscreenNotification object:_moviePlayer];
    [noti addObserver:self     selector:@selector(moviePlayerWillExitFullscreenNotification:)             name:MPMoviePlayerWillExitFullscreenNotification object:_moviePlayer];

}

- (void)moviePlayerWillEnterFullscreenNotification:(NSNotification*)notify

{
    AppDelegate  *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    delegate.allowRotation = YES;
    NSLog(@"moviePlayerWillEnterFullscreenNotification");
}

- (void)moviePlayerWillExitFullscreenNotification:(NSNotification*)notify

{
    AppDelegate  *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    delegate.allowRotation = NO;
    [self.moviePlayer play];
    NSLog(@"moviePlayerWillExitFullscreenNotification");
}

你可能感兴趣的:(iOS)