微信语音连播的实现思路

作者: 久林的技术随笔 | 来源:发表于2016-04-28 22:18 被阅读2857次

最近有同学问到了关于微信语音连播的问题,在这里我将自己模仿微信语音连播的核心代码部分开源出来,供大家参考。仔细阅读,按照代码的思路就可以实现语音连播。

基本的思路就是:使用递归思想。点击语音消息,获取cell,判断(1)播放的消息是否正在播放,(2)播放的消息是否是点击的消息;然后进行播放,更新语音动画UI。播放完毕,需要在内存中的messageArray中查找下一条消息,通过消息找到cell,更新cell上的语音动画UI,播放该语音,更新数据库中的标记。然后继续下一条消息的播放。

- (void)clickCellVoice:(VMessageEntity *)model
{
    __weak VChatsViewController *weakSelf = self;
 // cell 的点击事件,
    if (model)
    {
        VChatVoiceBaseCell *voiceCell = nil;
        // 在可视化的cell上面用messageId查找与model对应的VChatVoiceBaseCell,
        for (UITableViewCell *cell in [_tableView visibleCells]) {
            if ([cell isKindOfClass:[VChatVoiceBaseCell class]]) {
              VChatVoiceBaseCell *tempVoiceCell  =(VChatVoiceBaseCell *)cell;
                if ( tempVoiceCell.message.messageId == model.messageId) {
                    voiceCell = (VChatVoiceBaseCell *)cell;
                    break;
                }
            }
        }
        if (voiceCell)
        {
            //如果点击的cell的语音文件没有播放,则开始播放,同时开启语音播放动画。
            if (![[VAudioPalyerManager sharedManager] isPlaying]) {
                [voiceCell.playIcon startAnimating];
                
                 model.voiceMessage.isPlaying = YES;
                [[VAudioPalyerManager sharedManager] playWithfile:model.voiceMessage.voicemd5 
                finishPlaying:^(NSString *fileName,BOOL isFinished) {
                    // 播放完成的回调,停止动画,开始播放下一条
                    [voiceCell.playIcon stopAnimating];
                     model.voiceMessage.isPlaying = NO;
                    if (isFinished) {
                        if (model.messageStatus == VMessageStatusNone && model.readStatus == VMessageNoRead)
                        {
                            [weakSelf playNextUnReadVoiceWithMessageEntity:model];
                        }
                    }
                }];
            }else if([[VAudioPalyerManager sharedManager]isPlaying]
             &&![model.voiceMessage.voicemd5 isEqualToString:[[VAudioPalyerManager sharedManager] currentFileName]]) 
             //如果正在播放,且与当前的文件名不同,停止播放当前的播放效果,播放另外一条。
            {
                [[VAudioPalyerManager sharedManager] stop];
                [voiceCell.playIcon startAnimating];
                 model.voiceMessage.isPlaying = YES;
                [[VAudioPalyerManager sharedManager] playWithfile:model.voiceMessage.voicemd5 
                finishPlaying:^(NSString *fileName, BOOL isFinish) {
                    [voiceCell.playIcon stopAnimating];
                    model.voiceMessage.isPlaying = NO;
                    if (isFinish) {
                        if (model.messageStatus == VMessageStatusNone && model.readStatus == VMessageNoRead)
                        {
                            [weakSelf playNextUnReadVoiceWithMessageEntity:model];
                        }
                    }
                }];
            }else if([[VAudioPalyerManager sharedManager]isPlaying] 
            && [model.voiceMessage.voicemd5 isEqualToString:[[VAudioPalyerManager sharedManager] currentFileName]])
             // 处理当前正在播放的语音,停止当前的语音播放
            {
                [[VAudioPalyerManager sharedManager] stop];
                 model.voiceMessage.isPlaying = NO;
                if([voiceCell.playIcon isAnimating])
                {
                    [voiceCell.playIcon stopAnimating];
                }
            }
        }
     }
    
}

//递归查找下一条未读语音消息
- (void)playNextUnReadVoiceWithMessageEntity:(VMessageEntity *)model
{
 //找到message在chatArray里面的位置
    model.readStatus = VMessageHaveRead;
    __weak VChatsViewController *weakSelf = self;
    if (model) {
        NSIndexPath *index = [NSIndexPath indexPathForRow:
        [self.messageArray indexOfObject:model] inSection:0];
        if (index)
        {
            for (NSInteger i = index.row+1; i < self.messageArray.count; i++) {
                id tempObj = self.messageArray[i];
                if ([tempObj isKindOfClass:[VMessageEntity class]])
                {
                    VMessageEntity *messageEntity = (VMessageEntity *)tempObj;
                    if (messageEntity.messageType == VMessageTypeVoice && messageEntity.messageStatus == VMessageStatusNone)
                    {//判断是语音消息
                        if (messageEntity.readStatus == VMessageNoRead)
                        {
                            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                //取到下一条的cell
                                VChatVoiceReceiveCell *voiceBaseCell = nil;
                                for (UITableView *cell in [_tableView visibleCells]) {
                                    if ([cell isKindOfClass:[VChatVoiceReceiveCell class]]) {
                                       VChatVoiceReceiveCell *tempBaseCell  = (VChatVoiceReceiveCell *)cell;
                                        if (tempBaseCell.message.messageId == messageEntity.messageId) {
                                            voiceBaseCell = (VChatVoiceReceiveCell *)cell;//找到cell
                                        }
                                    }
                                }
                                    //找到cell,更新UI
                                    [voiceBaseCell.playIcon startAnimating];
                                    //更新数据库消息的未读状态,去掉未读标记
                                    [voiceBaseCell receiveVoiceCellBeClick];
                                    //更新数据库
                                    
[[VMessageManager sharedManager] updateReadStatusWithClientMessageId:
messageEntity.clientMessageId readStatus:VMessageHaveRead userId:messageEntity.sendUserId];
                                    messageEntity.readStatus = VMessageHaveRead;
                                    [weakSelf.messageArray replaceObjectAtIndex:i  withObject:messageEntity];
                                    //进行播放
                                    messageEntity.voiceMessage.isPlaying  = YES;
                                    [[VAudioPalyerManager sharedManager] 
                                    playWithfile:messageEntity.voiceMessage.voicemd5 
                                    finishPlaying:^(NSString *fileName, BOOL isFinish) {
                                        [voiceBaseCell.playIcon stopAnimating];
                                        messageEntity.voiceMessage.isPlaying  = NO;
                                        if (isFinish) {
                                            if (model.messageStatus == VMessageStatusNone)
                                            {
                                                //isFinish == yes 则完整播放,再次调用这个方法
                                                [weakSelf playNextUnReadVoiceWithMessageEntity:model];
                                            }
                                        }
                                    }];
                            });
                            
                            break;
                        }
                    }
                }else{
                    NSLog(@"不是消息类型");
                }
            }
        }
    }
}

如有疑问,欢迎加群讨论:iOS--IM即时通讯交流群:551709117

或者

微博私信:http://weibo.com/490jiulin/home?wvr=5

相关文章

网友评论

  • 小鱼儿喜欢花无缺:prefect! can you open sources your code to github
  • 涨姿势长知识:建议使用KVO实现model属性改变后触发的UI刷新,不用专门通过model去找相应的cell,这样会简化很多代码,逻辑也更清晰
  • Zander:不错
  • Metaz:这里直接把从当前点击的语音开始到被其他类型截断中间所有的语音作为一个播放列表进行处理是不是更好一点?可以在播放当前语音的时候同时下载其他语音,这样播放下一条的时候就不需要等待下载了。
    笑谈红尘乱离人:@九零猴VS久林 一个 60s 的语言文件也蛮大的,如果非常多语言的话,用户的流量就白花花的流走了。目前正在想办法帮用户省流量。
    Metaz:@九零猴VS久林 我感觉微信的语音是在点击的时候才开始下载的,你可以点一条没有播放过比较长的语音,会发现在网慢一点的时候 cell 有一个禁用的状态持续一段时间然后才开始播放语音,代表在下载语音,而不是点击马上就会播放。
    久林的技术随笔:@Metaz 语音消息,一般都是下载好了,才会在前台展示。点击的过程就是一个转换与播放的过程。
  • 若雨千寻:prefect! can you open sources your code to github ?
  • 鲸鱼与巨浪:prefect! can you open sources your code to github ?

本文标题:微信语音连播的实现思路

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