使用EZAudio

录M4A格式可以参考该库例子中的代码.

录wav格式得改下源码.看下面的代码

  AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *error = nil;
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
if (error) NSLog(@"audio session category error: %@", error);
[session setActive:YES error:&error];
if (error) NSLog(@"audio session active error: %@", error); self.microphone = [EZMicrophone microphoneWithDelegate:self];
self.recorder = [EZRecorder recorderWithURL:[NSURL fileURLWithPath:RECORD_PATH]
clientFormat:[self.microphone audioStreamBasicDescription]
fileType:EZRecorderFileTypeWAV // 指定为WAV格式
delegate:self]; [self.microphone startFetchingAudio];
self.recording = YES;
self.audioTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(audioTimer:)
userInfo:nil repeats:YES]; // 定时器计算录音时间
- (void)audioTimer:(NSTimer *)timer
{
self.audioV.audioTime = (int)self.recorder.currentTime;
} // 这是个代理函数 可以用来计算音量
- (void)microphone:(EZMicrophone *)microphone
hasAudioReceived:(float **)buffer
withBufferSize:(UInt32)bufferSize
withNumberOfChannels:(UInt32)numberOfChannels
{
dispatch_async(dispatch_get_main_queue(), ^{
if (self.isRecording) {
CGFloat vol = [EZAudioUtilities RMS:buffer[] length:bufferSize];
//vol就是音量 *150是因为放大后控制控件宽度
self.audioV.vol = (int)(vol * );
}
});
}

以上这种写法会闪退.原因是这个库录wav格式配置不对.

修改库中EZAudioUtilities.m文件第287行函数为

 + (AudioStreamBasicDescription)stereoFloatInterleavedFormatWithSampleRate:(float)sampleRate
{
#if 0 // 这个库本身的代码 录wav格式直接出错 闪退
AudioStreamBasicDescription asbd;
UInt32 floatByteSize = sizeof(float);
asbd.mChannelsPerFrame = ;
asbd.mBitsPerChannel = * floatByteSize;
asbd.mBytesPerFrame = asbd.mChannelsPerFrame * floatByteSize;
asbd.mFramesPerPacket = ;
asbd.mBytesPerPacket = asbd.mFramesPerPacket * asbd.mBytesPerFrame;
asbd.mFormatFlags = kAudioFormatFlagIsFloat;
asbd.mFormatID = kAudioFormatLinearPCM;
asbd.mSampleRate = sampleRate;
asbd.mReserved = ;
return asbd;
#endif // 改用这种配置录出来就是wav格式
AudioStreamBasicDescription asbd;
memset(&asbd, , sizeof(asbd));
asbd.mSampleRate = ; //采样率
asbd.mFormatID = kAudioFormatLinearPCM;
asbd.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
asbd.mChannelsPerFrame = ; //单声道
asbd.mFramesPerPacket = ; //每一个packet一侦数据
asbd.mBitsPerChannel = ; //每个采样点16bit量化
asbd.mBytesPerFrame = (asbd.mBitsPerChannel / ) * asbd.mChannelsPerFrame;
asbd.mBytesPerPacket = asbd.mBytesPerFrame ; return asbd;
}

使用EncodeAudio库转为amr格式

提取密码: prdq

//转成amr格式后发送 NSData * data = [[NSData alloc] initWithContentsOfFile:RECORD_PATH]; data = [EncodeAudio convertWavToAmrFile:data];

05-08 08:44