#pragma mark - 播放原wav
- (IBAction)playWav:(id)sender {
UIButton *btn = (UIButton *)sender;
playIndex = btn.tag;
ChatMessageInfo *info = [self.dataArray objectAtIndex:btn.tag];
NSString *fileName = info.localPath;
self.soundBtn = btn;
// 当本地没有的时候,去下载
if (fileName.length == 0) {
[self downloadVoice:info];
}
if (fileName.length > 0)
{
// 对于同一个音频文件的重复点击,不再进行生成新的player
if (![fileName isEqualToString:tempFileName])
{
// 暂停播放的player,开一个新的player
if (self.player) {
if ([self.player isPlaying]) {
[self.player stop];
[self.player setCurrentTime:0.0];
[selfupdateVoicePlayStateSwitch:kStophasBeforeIndex:YES];
}
}
self.player = [[AVAudioPlayeralloc] initWithContentsOfURL:[NSURLURLWithString:fileName] error:Nil];
self.player.delegate = self;
}
tempFileName = fileName;
if ([self.player isPlaying])
{
[self.player stop];
[self.player setCurrentTime:0.0];
[selfupdateVoicePlayStateSwitch:kStophasBeforeIndex:NO];
}else
{
[self.player play];
[selfupdateVoicePlayStateSwitch:kStarthasBeforeIndex:NO];
}
}
beforeIndex = btn.tag;
}
-(void)updateVoicePlayStateSwitch:(int)_type hasBeforeIndex:(BOOL)_hasIndex
{
NSIndexPath *mIndexPath;
if (_hasIndex) {
ChatMessageInfo *info = [self.dataArrayobjectAtIndex:beforeIndex];
info.isPlay = _type;
_type == 1?[self startTimer]:[self stopTimer];
mIndexPath = [NSIndexPath indexPathForRow:beforeIndex inSection:0];
}else{
ChatMessageInfo *info = [self.dataArrayobjectAtIndex:playIndex];
info.isPlay = _type;
_type == 1?[self startTimer]:[self stopTimer];
mIndexPath = [NSIndexPath indexPathForRow:playIndex inSection:0];
}
NSArray *indexArray = [NSArray arrayWithObject:mIndexPath];
[self.tableViewreloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationNone];
}