本算法实现了微信的语音连播功能:即自动读取本条之后的未读语音消息;连播一般都是一条播放完成,在接着播放下一条。
- (void)clickCellVoice:(VMessageEntity *)model
{
__weak
VChatsViewController *weakSelf =
self;
if ([self.keyBoardView
isFirstResponder])
{
[self
tapAnywhereToDismissKeyboard:nil];
}
if (model)
{
model.readStatus =
VMessageHaveRead;
VChatVoiceBaseCell *voiceCell =
nil;
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)
{
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)
{
[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)
{
[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里面的位置
__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(@"不是消息类型");
}
}
}
}
}