使用AVAudioRecorder录制音频

SYAudio

导入录音头文件(注意添加framework:AVFoundation.frameworkAudioToolbox.framework

#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
@property (nonatomic, strong) AVAudioRecorder *recorder; // 录音

@property (nonatomic, strong) NSTimer *voiceTimer; // 录音音量计时器

开始录音

- (void)recorderStart:(NSString *)filePath
{
    if (!filePath || filePath.length <= 0) {
        NSLog(@"文件路径无效");
        return;
    }

    // 强转音频格式为xx.caf
    BOOL isCaf = [filePath hasSuffix:@".caf"];
    if (isCaf) {
        self.recorderFilePath = filePath;
    } else {
        NSRange range = [filePath rangeOfString:@"." options:NSBackwardsSearch];
        NSString *filePathTmp = [filePath substringToIndex:(range.location + range.length)];
        self.recorderFilePath = [NSString stringWithFormat:@"%@caf", filePathTmp];
    }

       // 参数设置 格式、采样率、录音通道、线性采样位数、录音质量
       NSMutableDict  *recorderDict = [NSMutableDictionary dictionary];
        // kAudioFormatMPEG4AAC :xxx.acc;kAudioFormatLinearPCM :xxx.caf
        [_recorderDict setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
        [_recorderDict setValue:[NSNumber numberWithInt:16000] forKey:AVSampleRateKey];
        [_recorderDict setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
        [_recorderDict setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
        [_recorderDict setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
    // 生成录音文件
    NSURL *urlAudioRecorder = [NSURL fileURLWithPath:filePath];
    self.recorder = [[AVAudioRecorder alloc] initWithURL:urlAudioRecorder settings:recorderDict error:nil];

    // 开启音量检测
    self.recorder.meteringEnabled = YES;
    self.recorder.delegate = self;

    if (self.recorder)
    {
        // 录音时设置audioSession属性,否则不兼容Ios7
        AVAudioSession *recordSession = [AVAudioSession sharedInstance];
        [recordSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
        [recordSession setActive:YES error:nil];

        if ([self.recorder prepareToRecord])
        {
            [self.recorder record];            
            [self startVoiceTimer];
        }
    }
}

停止录音

- (void)recorderStop
{
    if (self.recorder)
    {
        if ([self.recorder isRecording])
        {
            [self.recorder stop];

            // 停止录音后释放掉
            self.recorder.delegate = nil;
            self.recorder = nil;
        }
    }

    [self stopVoiceTimer];
}

录音音量开启监测

- (void)startVoiceTimer
{
    if (self.monitorVoice) {
        self.voiceTimer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(detectionVoice) userInfo:nil repeats: YES];
        [[NSRunLoop currentRunLoop] addTimer:self.voiceTimer forMode:NSRunLoopCommonModes];        
        [self.voiceTimer setFireDate:[NSDate distantPast]];
        NSLog(@"开始检测音量");
    }
}

录音音量停止监测

- (void)stopVoiceTimer
{
    if (self.voiceTimer)
    {
        [self.voiceTimer setFireDate:[NSDate distantFuture]];
        if ([self.voiceTimer isValid])
       {
           [self.voiceTimer invalidate];
       }
       self.voiceTimer = nil;
        NSLog(@"停止检测音量");
    }
}

录音音量显示

- (void)detectionVoice
{
    // 刷新音量数据
    [self.recorder updateMeters];

//    // 获取音量的平均值
//    [self.audioRecorder averagePowerForChannel:0];
//    // 音量的最大值
//    [self.audioRecorder peakPowerForChannel:0];

    double voice = pow(10, (0.05 * [self.recorder peakPowerForChannel:0]));    
    NSLog(@"voice: %f", voice);
}

猜你喜欢

转载自blog.csdn.net/potato512/article/details/81463576