求大神解惑 关于录音格式准换问题
用AVAudioRecorder录音录出来的wav格式,后台那里需要amr格式的。 于是去找度娘。 找到了一个VoiceConvert的三方 里面用的 libopencore-amr 按照他的方法转换
- (NSString *)getFilePath {
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *urlPath = [path stringByAppendingPathComponent:@"123.wav"];
return urlPath;
}
- (NSString *)amrFilePath {
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *urlPath = [path stringByAppendingPathComponent:@"555.amr"];
return urlPath;
}
//转为amr
- (NSString *)audioWAVtoAMR {
NSString * wavFilePath = [self getFilePath];
NSString * amrFilePath = [self amrFilePath];
if ([EMVoiceConverter wavToAmr:wavFilePath amrSavePath:amrFilePath]) {
NSLog(@"成功了");
} else {
NSLog(@"wav转amr失败");
}
}
为啥转不成功 难道姿势哪里不对。。。。
设置的应该没问题
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc]init];
//设置录音格式 AVFormatIDKey==kAudioFormatLinearPCM
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
//设置录音采样率(Hz) 如:AVSampleRateKey==8000/44100/96000(影响音频的质量)
[recordSetting setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey];
//录音通道数 1 或 2
[recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];
//线性采样位数 8、16、24、32
[recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
//录音的质量
[recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
这是三方内部的部分代码
+ (int)wavToAmr:(NSString*)_wavPath amrSavePath:(NSString*)_savePath{
if (EM_EncodeWAVEFileToAMRFile([_wavPath cStringUsingEncoding:NSASCIIStringEncoding], [_savePath cStringUsingEncoding:NSASCIIStringEncoding], 1, 16))
return 0; // success
return 1; // failed
}
int EM_EncodeWAVEFileToAMRFile(const char* pchWAVEFilename, const char* pchAMRFileName, int nChannels, int nBitsPerSample)
{
FILE* fpwave;
FILE* fpamr;
/* input speech vector */
short speech[160];
/* counters */
int byte_counter, frames = 0;
size_t bytes = 0;
/* pointer to encoder state structure */
void *enstate;
/* requested mode */
enum Mode req_mode = MR122;
int dtx = 0;
/* bitstream filetype */
unsigned char amrFrame[MAX_AMR_FRAME_SIZE];
fpwave = fopen(pchWAVEFilename, "rb");
if (fpwave == NULL)
{
return 0;
}
// Initialize the amr file
fpamr = fopen(pchAMRFileName, "wb");
if (fpamr == NULL)
{
fclose(fpwave);
return 0;
}
/* write magic number to indicate single channel AMR file storage format */
bytes = fwrite(AMR_MAGIC_NUMBER, sizeof(char), strlen(AMR_MAGIC_NUMBER), fpamr);
/* skip to pcm audio data*/
SkipToPCMAudioData(fpwave);
enstate = Encoder_Interface_init(dtx);
while(1)
{
// read one pcm frame
if (!ReadPCMFrame(speech, fpwave, nChannels, nBitsPerSample)) break;
frames++;
/* call encoder */
byte_counter = Encoder_Interface_Encode(enstate, req_mode, speech, amrFrame, 0);
bytes += byte_counter;
fwrite(amrFrame, sizeof (unsigned char), byte_counter, fpamr );
}
Encoder_Interface_exit(enstate);
fclose(fpamr);
fclose(fpwave);
return frames;
}
好气啊。 求大神解决。