回想起項(xiàng)目中關(guān)于音頻處理交互這塊真是想罵人的趕腳,對(duì)于這些坑希望程序猿們不要碰到了頭大,耐心尋找解決辦法。下面是我遇到的一些坑跟大家分享一下,如有不足請(qǐng)聯(lián)系指正,1241545546@qq.com
用到的框架lame.h libmp3lame.a 拖到工程導(dǎo)入lame.h就可以了。如有問(wèn)題請(qǐng)聯(lián)系。
下載地址:鏈接: https://pan.baidu.com/s/1hsukG7q 密碼: ew7p
1.首先關(guān)于網(wǎng)上其他說(shuō)的轉(zhuǎn)碼的將AMR 與WAV互轉(zhuǎn)的這個(gè)我多次嘗試失敗,報(bào)轉(zhuǎn)碼錯(cuò)誤,至今不明所以,有明白的童鞋歡迎告知。我所采用的是轉(zhuǎn)碼MP3格式,這個(gè)只發(fā)現(xiàn)樂(lè)視和魅族安卓端發(fā)送的語(yǔ)音IOS端無(wú)法播放,不知道是不是手機(jī)問(wèn)題。代碼如下:
@1.錄制部分:
//創(chuàng)建錄制并開(kāi)始錄制
NSDictionary *setting=[self getAudioSetting];
[self setAudioSession];
NSError *error=nil;
_audioRecorder=[[AVAudioRecorder alloc]initWithURL:[self getSavePath] settings:setting error:&error];
if (![self.audioRecorder isRecording]) {
[self.audioRecorder record];//首次使用應(yīng)用時(shí)如果調(diào)用record方法會(huì)詢問(wèn)用戶是否允許使用麥克風(fēng)
}
//設(shè)置
*? 設(shè)置音頻會(huì)話
-(void)setAudioSession{
AVAudioSession *audioSession=[AVAudioSession sharedInstance];
#error 注意此處有坑:如果發(fā)現(xiàn)錄制完成播放錄制的語(yǔ)音之后再次錄制(不退出銷毀)時(shí)沒(méi)有響應(yīng)或者根本就不錄制保存,那么問(wèn)題出現(xiàn)在這里,你可能播放的時(shí)候在此設(shè)置了AVAudioSession的環(huán)境為AVAudioSessionCategoryPlayback。播放狀態(tài)不支持錄制。
//設(shè)置為播放和錄音狀態(tài),以便可以在錄制完之后播放錄音
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[audioSession setActive:YES error:nil];
}
-(NSDictionary *)getAudioSetting{
NSMutableDictionary *dicM=[NSMutableDictionary dictionary];
//設(shè)置錄音格式
[dicM setObject:@(kAudioFormatLinearPCM) forKey:AVFormatIDKey];
//設(shè)置錄音采樣率,8000是電話采樣率,對(duì)于一般錄音已經(jīng)夠了,但是這里是錄制mp3格式。
[dicM setObject:@(11025) forKey:AVSampleRateKey];
//設(shè)置通道,這里采用單聲道
[dicM setObject:@(2) forKey:AVNumberOfChannelsKey];
//每個(gè)采樣點(diǎn)位數(shù),分為8、16、24、32
[dicM setObject:@(16) forKey:AVLinearPCMBitDepthKey];
//是否使用浮點(diǎn)數(shù)采樣
//? ? [dicM setObject:@(YES) forKey:AVLinearPCMIsFloatKey];
//....其他設(shè)置等
return dicM;
}
//錄制完成即可轉(zhuǎn)碼
-(void)transForm{
mp3FilePath = [NSString stringWithFormat:@"%@/Voicerecoard/mp3Audio.mp3",[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]];
//? ? NSURL* mp3FileUrl = [NSURL fileURLWithPath:mp3FilePath];
@try {
int read, write;
FILE *pcm = fopen([_currentFile cStringUsingEncoding:1], "rb");? //source 被轉(zhuǎn)換的音頻文件位置
fseek(pcm, 4*1024, SEEK_CUR);? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //skip file header
FILE *mp3 = fopen([mp3FilePath cStringUsingEncoding:1], "wb");? //output 輸出生成的Mp3文件位置
const int PCM_SIZE = 8192;
const int MP3_SIZE = 8192;
short int pcm_buffer[PCM_SIZE*2];
unsigned char mp3_buffer[MP3_SIZE];
lame_t lame = lame_init();
lame_set_in_samplerate(lame, 11025.0);
lame_set_VBR(lame, vbr_default);
lame_init_params(lame);
do {
read = fread(pcm_buffer, 2*sizeof(short int), PCM_SIZE, pcm);
if (read == 0)
write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE);
else
write = lame_encode_buffer_interleaved(lame, pcm_buffer, read, mp3_buffer, MP3_SIZE);
fwrite(mp3_buffer, write, 1, mp3);
} while (read != 0);
lame_close(lame);
fclose(mp3);
fclose(pcm);
}
@catch (NSException *exception) {
NSLog(@"%@",[exception description]);
}
@finally
{
}
}
-(NSURL *)getSavePath{
NSString *urlStr=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString* file=[urlStr stringByAppendingString:@"/Voicerecoard"];
if (![[NSFileManager defaultManager]fileExistsAtPath:file]) {
[[NSFileManager defaultManager]createDirectoryAtPath:file withIntermediateDirectories:YES attributes:nil error:nil];
}
NSDateFormatter *foreme=[[NSDateFormatter alloc] init];//創(chuàng)建日期格式化的對(duì)象
[foreme setDateFormat:@"yyyy-MM-ddhh:mm:ss"];
NSDate *data=[NSDate date];
NSString* string=[foreme stringFromDate:data];
NSString* path=[string stringByAppendingString:@".caf"];
urlStr=[file stringByAppendingPathComponent:path];
NSLog(@"file path:%@",urlStr);
NSURL *url=[NSURL fileURLWithPath:urlStr];
_currentFile=urlStr;
return url;
}