1、设置plist,具体操作见我所写的后台播放音乐的博客(需要真机测试)

2、设置支持后台

  AVAudioSession *session = [AVAudioSession sharedInstance];

    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

    [session setActive:YES error:nil];

3、初始化

if (_recorder == nil) {

            NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"recorder"];

            NSLog(@"path:%@", path);//路径文件的后缀不能是.mp3/.mp4之类的,要不然AVAudioRecorder会创建失败,如果不存储录音,path可随便设但是不能为nil

            NSDictionary *settings = @{AVSampleRateKey: [NSNumber numberWithFloat:44100.0f],

                                       AVEncoderAudioQualityKey: [NSNumber numberWithInt:AVAudioQualityMax],

                                       AVNumberOfChannelsKey: [NSNumber numberWithInt:1],

                                       AVFormatIDKey: [NSNumber numberWithInt:kAudioFormatAppleLossless]};

            NSError *error = nil;

            _recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:path] settings:settings error:&error];

            if (error) {

                NSLog(@"error:%@ info:%@", [error localizedDescription], [error userInfo]);

            }

            [_recorder record];

            _recorder.delegate = self;//不需要监控开始结束与失败事件的可以不设置delegate

            [_recorder prepareToRecord];//准备录音

            _recorder.meteringEnabled = YES;//开启测量

            [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(levelTimerCallback:) userInfo:nil repeats:YES];

        }

//定时方法

- (void)levelTimerCallback:(NSTimer *)timer {

    [_recorder updateMeters];                //跟新测量

    CGFloat avg = [_recorder averagePowerForChannel:0];  //获取通道0的平均音量,声音不大的情况下avg是个负数,没有启动MIC是-160

    CGFloat max = [_recorder peakPowerForChannel:0];    ///获取通道0的峰值音量,声音不大的情况下avg是个负数,没有启动MIC是-160

    NSLog(@"avg:%f---max:%f", avg, max);

}