之前有个项目涉及到自定义视频,横竖屏问题。在横屏问题上研究了很久,现在有空整理下,希望能帮助到有需要的小伙伴。效果如下:
前言: 用到的第三方框架:LLMediaPlayerView,是用AVPlayer写的一个自定义视频,在实现过程中发现横屏有问题,自己进行了改造,主要做了以下这些工作。
一 自定义UINavigationController,使有视频的controller支持横竖屏。
@interface PSJNavVC : UINavigationController
@end
#pragma mark - 支持横屏
-(NSUInteger)supportedInterfaceOrientations
{
if ([self.topViewController isKindOfClass:[PSJMoviePlayerVC class]]) {
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
}
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotate {
if ([self.topViewController isKindOfClass:[PSJMoviePlayerVC class]]) {
return YES;
}
return NO;
}
二 视频界面旋转回调处理旋转
在 PSJMoviePlayerV.m中
// 屏幕停止旋转了 _isTurn==YES(转横屏)
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
NSLog(@"收到旋转通知");
if (toInterfaceOrientation==1) {
[[UIApplication sharedApplication] setStatusBarHidden:NO];
self.isTurn = NO;
}else if (toInterfaceOrientation==4||toInterfaceOrientation==3) {
self.isTurn = YES;
}
if (_isTurn) {
[self.movieView.playerView setFullscreen:YES];
}else{
[self.movieView.playerView setFullscreen:NO];
}
}
三 旋转方法
(1)解决iOS7以上视频旋转问题
- (void)setFullscreen:(BOOL)fullscreen
{
if (fullscreen) {
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 = UIInterfaceOrientationLandscapeRight;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}else{
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];
}
}
[self setFullscreen:fullscreen animated:YES];
}
(2)把视频VC的view加在window上
if (fullscreen == NO) {
[fullscreenButton_ setImage:buttonImages_[LMMediaPlayerViewFullscreenButtonImageKey] forState:UIControlStateNormal];
[fullscreenButton_ setImage:buttonImages_[LMMediaPlayerViewFullscreenButtonSelectedImageKey] forState:UIControlStateSelected];
newRect = superView_.bounds;
self.frame = newRect;
[superView_ addSubview:self];
LM_RELEASE(superView_);
[mainWindow_ makeKeyAndVisible];
[[[UIApplication sharedApplication] delegate] setWindow:mainWindow_];
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
[[UIApplication sharedApplication] setStatusBarOrientation:[mainWindow_ rootViewController].interfaceOrientation];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
}
else {
[fullscreenButton_ setImage:buttonImages_[LMMediaPlayerViewUnfullscreenButtonImageKey] forState:UIControlStateNormal];
[fullscreenButton_ setImage:buttonImages_[LMMediaPlayerViewUnfullscreenButtonSelectedImageKey] forState:UIControlStateSelected];
superView_ = self.superview;
LM_RETAIN(superView_);
newRect = mainWindow_.frame;
UIViewController *rootViewController = [mainWindow_ rootViewController];
UIInterfaceOrientation orientation = rootViewController.interfaceOrientation;
if (orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft) {
if (floor(NSFoundationVersionNumber) >= NSFoundationVersionNumber_iOS_7_0) {
newRect = CGRectMake(0, 0, CGRectGetWidth(mainWindow_.frame), CGRectGetHeight(mainWindow_.frame));
}
else {
newRect = CGRectMake(0, 0, CGRectGetHeight(mainWindow_.frame), CGRectGetWidth(mainWindow_.frame));
}
}
[self removeFromSuperview];
[viewController.view addSubview:self];
UIWindow *newWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
newWindow.windowLevel = UIWindowLevelAlert;
newWindow.rootViewController = viewController;
[newWindow addSubview:viewController.view];
[newWindow makeKeyAndVisible];
[[[UIApplication sharedApplication] delegate] setWindow:newWindow];
LM_RELEASE(newWindow);
}
四 具体实现请参照PSJVideoPlayView DEMO,视频自定义相关
自定义视频view: LMMediaPlayerView
视频播放VC: PSJMoviePlayerVC
视频UI图片: LMMediaPlayerView.bundle(要改变UI,可以在这里替换相关的按钮文件)
五 DEMO下载地址:http://download.csdn.net/detail/jueyi1127/9420113
使用中如有任何问题,欢迎加微信shujuan2015交流.