关于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");
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值