最近有同学问到了关于微信语音连播的问题,在这里我将自己模仿微信语音连播的核心代码部分开源出来,供大家参考。仔细阅读,按照代码的思路就可以实现语音连播。
基本的思路就是:使用递归思想。点击语音消息,获取cell,判断(1)播放的消息是否正在播放,(2)播放的消息是否是点击的消息;然后进行播放,更新语音动画UI。播放完毕,需要在内存中的messageArray中查找下一条消息,通过消息找到cell,更新cell上的语音动画UI,播放该语音,更新数据库中的标记。然后继续下一条消息的播放。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
- (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 ==pan>{ if (model.messageStatus ==ͣ在播放,(2)播放的消息是否是点击的消息;然后进行播放,更新语音动画UI。播放完毕,需要在内存中的messageArray中查找下一条消息,通过消息找到cell,更新cell上的语音动画UI,播放该语音,更新数据库中的标记。然后继续下一条消息的播放。
|