美文网首页iOS_Skill_Collect腾讯云直播
iOS集成腾讯云直播遇到的坑

iOS集成腾讯云直播遇到的坑

作者: QuintGao | 来源:发表于2017-04-12 10:10 被阅读1305次

    最近公司项目中要集成腾讯云直播的观看端,推流端没有让集成,于是研究了一下,在这里记录一下遇到的坑点

    要求:根据推流端的直播方式自适应观看端的直播方式,也就是说推流端如果是横屏直播,观看端也只能是横屏观看,推流端如果是竖屏直播,观看端也只能竖屏观看。

    解决办法:

    1. 首先定义三个属性
    /** 标识是否是横屏 */
    @property (nonatomic, assign) BOOL isLandspace;
    /** 标识屏幕方向是否锁定 */
    @property (nonatomic, assign) BOOL hasLocked;
    /** 标识视频方向是否检测 */
    @property (nonatomic, assign) BOOL hasCheck;
    
    1. 在TXLivePlayListener中,获取到直播视频的分辨率,并做相应处理
    // 获取直播视频的分辨率
            int width  = [dict[NET_STATUS_VIDEO_WIDTH]  intValue];
            int height = [dict[NET_STATUS_VIDEO_HEIGHT] intValue];
            // 这里处理直播方向发生改变时,需要重新判断方向 
            if ((self.isLandspace && width < height) || (!self.isLandspace && width > height)) {
                
                self.hasLocked = NO;
                self.hasCheck  = NO;
            }
            
            // 去除错误数据
            if (width == 0 || height == 0) return;
            
            // 方向获取成功,不必重新获取
            if (self.hasCheck) return;
            
            // 宽 > 高,横屏
            if (width > height) {
                NSLog(@"切换到横屏");
                self.isLandspace = YES;
                // 设置屏幕强制横屏
                [self interfaceOrientation:UIInterfaceOrientationLandscapeRight];
            }else{ // 竖屏
                NSLog(@"切换到竖屏");
                self.isLandspace = NO;
                // 设置屏幕强制竖屏
                [self interfaceOrientation:UIInterfaceOrientationPortrait];
            }
            
            self.hasCheck    = YES;
    

    其中强制改变屏幕方向的方法

    /**
     *  强制屏幕转屏
     *
     *  @param orientation 屏幕方向
     */
    - (void)interfaceOrientation:(UIInterfaceOrientation)orientation
    {
        // 设置屏幕旋转
        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 = orientation;
            // 从2开始是因为0 1 两个参数已经被selector和target占用
            [invocation setArgument:&val atIndex:2];
            [invocation invoke];
        }
    }
    

    3.当前控制器需要能够自动旋转

    // 是否支持自动转屏
    - (BOOL)shouldAutorotate
    {
        return YES;
    }
    
    // 支持哪些转屏方向
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations
    {
        // 直播方向锁定
        if (self.hasCheck) {
            if (self.isLandspace) { // 横屏支持方向:left、right
                return UIInterfaceOrientationMaskLandscape;
            }else{ // 竖屏支持方向:portrait
                return UIInterfaceOrientationMaskPortrait;
            }
        }else{ // 屏幕方向未检测完毕,支持left、right、portrait
            return UIInterfaceOrientationMaskAllButUpsideDown;
        }
    }
    

    4.在控制器旋转之后做相应操作

    // 屏幕方向发生改变时调用
    - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
    {
        // 屏幕方向检测完毕
        if (self.hasCheck) {
            // 方向锁定,不必判断
            if (self.hasLocked) return;
            
            if (self.isLandspace && size.width > size.height) {
                self.hasLocked = YES;
            }
            
            if (!self.isLandspace && size.width < size.height) {
                self.hasLocked = YES;
            }
        }
    }
    

    总结:关于屏幕自动旋转只在直播控制器开启,其他控制器是不能自动旋转的,具体实现只需写导航栏和控制器的分类即可。

    相关文章

      网友评论

      本文标题:iOS集成腾讯云直播遇到的坑

      本文链接:https://www.haomeiwen.com/subject/wtnpattx.html