在iOS 4.3之后,AVAudioPlayer是不支持播放amr文件格式的音频,AudioServicesPlaySystemSound虽支持少于三十秒的amr文件播放,但是只能用做铃声播放,不能当音频来播放。
对此需要将amr格式转码成wav格式,这里面需要引入VoiceConvert第三方库,需要确认其支持arm64,否则会报错
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
//把data写入文件中,取名AudioTempFile
[data writeToFile:[NSTemporaryDirectory() stringByAppendingPathComponent:@"AudioTempFile"] atomically:YES];
//将amr格式的数据转成wav
[EMVoiceConverter amrToWav:[NSTemporaryDirectory() stringByAppendingPathComponent:@"AudioTempFile"] wavSavePath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"AudioTempConvertFile"]];
//读取新的wav格式音频文件
NSData* convertData = [NSData dataWithContentsOfFile:[NSTemporaryDirectory() stringByAppendingPathComponent:@"AudioTempConvertFile"]];
//初始化播放器,播放converData
self.player = [[AVAudioPlayer alloc] initWithData:convertData error:nil];
self.player.delegate = self;
[self.player prepareToPlay];
[self.player play];
最后附上第三方库VoiceConvert