iOS自定义视频LLMediaPlayerView支持横竖屏改良版(附DEMO)

之前有个项目涉及到自定义视频,横竖屏问题。在横屏问题上研究了很久,现在有空整理下,希望能帮助到有需要的小伙伴。效果如下:



前言: 用到的第三方框架: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交流.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值