直播模块开发总结

作者: Jeavil_Tang | 来源:发表于2016-07-20 22:12 被阅读797次

    这次的版本开发无疑是对我的一次挑战,所有的新功能都需要独立开发,并且时间很紧,之前连视频播放器都很少接触,但新需求一来就是直播功能,真的还是蛮有压力的。不过完后工回想一下收获颇多,无论是技术上的,还是心理上的。吼吼,来总结一下此次开发中踩过的坑吧!

    第一大坑: 局部界面转屏

    iOS开发开发中,转屏包含设备的转动方向和屏幕转动方向,如果要开启设备的转屏方向,可以在Target中勾选多支持的方向,但是我们的项目比较特殊,只有在播放视频的界面才可以转屏其他界面只能竖屏,这个需求困扰了我好久,终于最后找到了一个解决方案:

    • 首先在Target中勾选所需要支持的转屏方向

    • 在 AppDelegate.h 中声明一个属性用来标示是否可以转屏

    /***  是否允许横屏的标记 */
    @property (nonatomic,assign)BOOL allowRotation;
    
    • 在 AppDelegate.m 中 实现方法:
    -(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
        if (self.allowRotation) {
            return UIInterfaceOrientationMaskPortrait| UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
    
        }else {
            return UIInterfaceOrientationMaskPortrait;
        }
    }
    

    与之相关的还有两个方法如下,但是我这里不需要就没有实现

    //是否自动旋转,返回YES可以自动旋转  
    - (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;  
    //这个是返回优先方向  
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;  
    
    
    • 然后在你需要转屏的那个 ViewController 里面设置转屏与否
    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        appDelegate.allowRotation = YES;
    }
    
    

    需要注意的是如果在这个视图控制器消失或销毁后,保持其他视图控制器不支持转屏,那你需要在viewWillDisAppear的时候将AppDelegate中的属性设置为NO

    - (void)viewWillDisappear:(BOOL)animated {
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        appDelegate.allowRotation = NO;
        [self.playerView shutDownPlayer];
        [UIApplication sharedApplication].statusBarHidden = NO;
    }
    
    • 在需要转屏的ViewController中添加通知监测设备是否旋转,并针对不同方向设置做不同的操作
    - (void)listeningRotating {
        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        [[NSNotificationCenter defaultCenter]
         addObserver:self
         selector:@selector(onDeviceOrientationChange)
         name:UIDeviceOrientationDidChangeNotification
         object:nil];
    }
    
    - (void)onDeviceOrientationChange {
        
        UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
        UIInterfaceOrientation interfaceOrientation =
        (UIInterfaceOrientation)orientation;
        
        [self.landscapeControlOverlay removeFromSuperview];
        
        switch (interfaceOrientation) {
            case UIInterfaceOrientationPortrait:
                [self setOrientationPortrait];
                break;
                
            case UIInterfaceOrientationLandscapeRight:
                [self setOrientationLandscape];
                
            case UIInterfaceOrientationLandscapeLeft:
                [self setOrientationLandscape];
                
            default:
                break;
        }
    }
    
    • 强制屏幕旋转
    - (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;
            [invocation setArgument:&val atIndex:2];
            [invocation invoke];
        }
            
        if (orientation == UIInterfaceOrientationLandscapeRight ||
            orientation == UIInterfaceOrientationLandscapeLeft) {
            [self setOrientationLandscape];
        } else if (orientation == UIInterfaceOrientationPortrait) {
            [self setOrientationPortrait];
        }
    }
    

    第二大坑:转屏后遗症

    通过才第一个坑终于把视频播放界面转屏其他界面只能竖屏的功能实现了,接着迎接的就是其他界面无缘无故的 crash,说具体一点我们不仅只有一个观看直播的界面,还可以在其他界面的tableViewCell中点击观看,但是我将转屏通知,转屏后的界面更新,通通都封装在视频播放器这个控件中,但是其他界面tableViewCell中的播放器却又不支持转屏功能。所以,当我在设置中取消屏幕锁定后,然后在cell中播放视频时,只要一晃动手机就会崩溃在MAsonry中提示: couldn't find a common superview for <UIButton: 0x14d172340; frame = (0 0; 0 0); opaque = NO; layer = <CALayer: 0x1510b0690>> and <LivePlayer: 0x14cf889a0; frame = (0 0; 0 0); layer = <CALayer: 0x14f60e740>> 这个问题可是让我足足找了两天,真的是整的我和测试妹子都要崩溃了。其原因就是在tableview所在的这个ViewController中是不支持转屏的,但是我在初始化播放器控件的时候并没有判断当前界面是否支持转屏,不分青红皂白的给人家通通设置了设备转屏通知,就是上面提到的 - (void)listeningRotating 方法,所以我只要一晃动手机就会出发改通知方法,给一些不存在的对象设置约束,当然要崩溃了。所以提醒大家也给自己敲个警钟,以后千万要注意了。

    第三大坑:Cell中的播放器最好使用单例

    如果需要在TableViewCell中播放视频,建议将视频播放器写成一个单例,不然真的很麻烦,我是把所有功能都开发完了才发现如果用单例代码会更简洁。

    现在我的做法是当你在某个cell中播放视频,然后再点击其他cell中的视频时,将上一条关闭;如果在cell播放视频的时候切换到其他界面时,将当前cell中的播放器关闭;程序进入后台时关闭当前cell中的直播;从直播播放切换到音频播放时,关闭直播;这些情况都需要记录当前cell,然后在对cell中的播放器做操作,这样做其实也没什么问题,但是不够优雅,并且消耗资源,每次都需要记录当前的cell所在的indexpath,如果使用单里的话无论在什么地方都可以对播放器进行操作,不仅方便还提高了代码的易读性。

    - (void)shutDownSelectCellPlayer {
        if (_currentIndex != 0) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_currentIndex-1000 inSection:0];
            LivePlayerCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
            [cell stopLivePlayer];
        }
    }
    

    我这是一发反面教材,以后还是要改成单例模式

    第四大坑: ARC下的僵尸指针问题

    这个坑我不知道要怎么描述,因为我封装的播放器逻辑不够严谨,因此有时会给一个已经释放了的对象做操作造成致命的崩溃

    第五大坑: 逻辑严谨性

    这点我不知该从何说起或者怎么说,或许还是因为自己对于有些基础掌握的不牢固,因此在开发过程中总会犯些低级错误,比如先将某对象释放了,然后又对其发送了消息,或者是在某对象可能还没创建出来的时候就以该对象为参照视图使用masonry建立约束,然后花时间去找崩溃原因,找到问题根源后恍然大吾然后自己都服了自己了。所以在写代码的时候一定要先将逻辑里清楚,这点很重要,慢慢修炼吧😂

    相关文章

      网友评论

      • 蠡园里面有李源:楼主,我在使用网易云视频demo的时候,将摄像头采集方向改为横屏,设备方向也改为横竖屏,但是在实际过程中,进去到直播界面,横屏之后,画面右侧有一半是黑的?这是什么原因?
      • cdfd8e7a3fbe:分享精神成就你我!
      • Charles___:强制转屏是私有api 会被拒绝吧
        Jeavil_Tang:@一只孤独的iOS 的确,因为调用的苹果私有API 有被拒的风险
      • davisjy:感谢楼主
      • Ryan___:博主 请教问题,用手机横屏直播,输出到pc端,视频画面方向不对,我应该在哪里入手改代码啊
        Jeavil_Tang:@cactus666 你需要重新设置一次view的约束,在监听设备转向的方法里面。你试一下呢
      • 214742ee46cf:直播只遇到这些问题吗,你用的自己的服务器还是其他平台的
        Jeavil_Tang:用的网易云信不是自己的服务器
      • 子慕大诗人:一定要先思考再写代码,要不永远都有逻辑漏洞的习惯
        Jeavil_Tang:@子慕大诗人 是的:blush:

      本文标题:直播模块开发总结

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