使用系统的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属性即可。
MPMoviePlayerController全屏横竖屏切换解决方案
本文介绍了如何解决使用MPMoviePlayerController播放视频时,全屏模式下仍保持竖屏的问题。通过监听全屏进入和退出通知,结合应用状态判断,控制屏幕旋转方向,确保全屏模式充分利用屏幕空间。
2028

被折叠的 条评论
为什么被折叠?



