ios录音wav格式 并转成amr格式

使用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;
}

使用EncodeAudio库转为amr格式

提取密码: prdq
    //转成amr格式后发送
    NSData * data = [[NSData alloc] initWithContentsOfFile:RECORD_PATH];
    data = [EncodeAudio convertWavToAmrFile:data];
    [[ELChatManager sharedELChatManager] sendAudioFile:self.msgList fileData:data];

参考资料:
https://github.com/zx1262111739/Chat

你可能感兴趣的:(ios录音wav格式 并转成amr格式)