我用的是1.1.0 的, 尽管当时已经有了1.1.4版本,不知道为什么总是更新失败就放弃了!pod 'EZAudio', '~> 1.1.0'
(在IOS4.3以后的系统不再支持amr格式播放了)
在使用EZAudio库录制amr格式的音频时,总是出现闪退的问题,发现是其中一个库文件的问题,以下是原文链接:
http://www.jianshu.com/p/601145d1583c
使用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[0] length:bufferSize];
//vol就是音量 *150是因为放大后控制控件宽度
self.audioV.vol = (int)(vol * 150);
}
});
}
以上这种写法会闪退.原因是这个库录wav格式配置不对.
修改库中EZAudioUtilities.m文件第287行函数为:
+ (AudioStreamBasicDescription)stereoFloatInterleavedFormatWithSampleRate:(float)sampleRate
{
#if 0 // 这个库本身的代码 录wav格式直接出错 闪退
AudioStreamBasicDescription asbd;
UInt32 floatByteSize = sizeof(float);
asbd.mChannelsPerFrame = 2;
asbd.mBitsPerChannel = 8 * floatByteSize;
asbd.mBytesPerFrame = asbd.mChannelsPerFrame * floatByteSize;
asbd.mFramesPerPacket = 1;
asbd.mBytesPerPacket = asbd.mFramesPerPacket * asbd.mBytesPerFrame;
asbd.mFormatFlags = kAudioFormatFlagIsFloat;
asbd.mFormatID = kAudioFormatLinearPCM;
asbd.mSampleRate = sampleRate;
asbd.mReserved = 0;
return asbd;
#endif
// 改用这种配置录出来就是wav格式
AudioStreamBasicDescription asbd;
memset(&asbd, 0, sizeof(asbd));
asbd.mSampleRate = 8000; //采样率
asbd.mFormatID = kAudioFormatLinearPCM;
asbd.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
asbd.mChannelsPerFrame = 1; //单声道
asbd.mFramesPerPacket = 1; //每一个packet一侦数据
asbd.mBitsPerChannel = 16; //每个采样点16bit量化
asbd.mBytesPerFrame = (asbd.mBitsPerChannel / 8) * asbd.mChannelsPerFrame;
asbd.mBytesPerPacket = asbd.mBytesPerFrame ;
return asbd;
}