MPMoviePlayerController全屏模式下横屏与竖屏切换

来源:互联网 发布:上证指数如何计算 知乎 编辑:程序博客网 时间:2024/05/23 18:06

使用系统的MPMoviePlayerController处理多媒体视频文件的时候,会遇到在进行全屏切换时,进入全屏后播放画面会发现还是竖屏模式,这样的话就完全失去了全屏模式的有优势了,浪费了很多屏幕空间,查阅了一些资料,发现可以通过注册监听通知中心关于MPMoviePlayerController控件的进入全屏(MPMoviePlayerWillEnterFullscreenNotification)和退出全屏(MPMoviePlayerWillExitFullscreenNotification)消息,判断当前应用所处状态,之后根据状态来设置屏幕的方向,从而对屏幕旋转方向进行控制。

AppDelegate.h文件中引入MediaPlayer头文件

#import <MediaPlayer/MediaPlayer.h>

声明记录当前应用状态变量

@implementation AppDelegate{        BOOL _isFullScreen;}...@end

注册进入全屏和退出全屏消息事件

[[NSNotificationCenter defaultCenter] addObserver:self                                             selector:@selector(willEnterFullScreen:)                                                 name:MPMoviePlayerWillEnterFullscreenNotification                                               object:nil];[[NSNotificationCenter defaultCenter] addObserver:self                                             selector:@selector(willExitFullScreen:)                                                 name:MPMoviePlayerWillExitFullscreenNotification                                               object:nil];

实现上面消息对应的事件响应函数

- (void)willEnterFullScreen:(NSNotification *)notification{    _isFullScreen = YES;}- (void)willExitFullScreen:(NSNotification *)notification{    _isFullScreen = NO;}

实现application:supportedInterfaceOrientationsForWindow:方法

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{    if (_isFullScreen) {        return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;    } else {        return UIInterfaceOrientationMaskPortrait;    }}

如果需要跨界面拿到_isFullScreen,只需要Appdelegate这个单例,然后设置_isFullScreen属性即可。

0 0
原创粉丝点击