麦克风音量检测 及录音DEMO

麦克风音量检测

- (void)audio

{

    //录音设置

    NSMutableDictionary *recordSetting = [[[NSMutableDictionary alloc]init] autorelease];

    //设置录音格式  AVFormatIDKey==kAudioFormatLinearPCM

    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];

    //设置录音采样率(Hz) 如:AVSampleRateKey==8000/44100/96000(影响音频的质量)

    [recordSetting setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey];

    //录音通道数  1 2

    [recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];

    //线性采样位数  8162432

    [recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];

    //录音的质量

    [recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];

    

    NSString *strUrl = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/lll.aac", strUrl]];

    urlPlay = url;

    

    NSError *error;

    //初始化

    recorder = [[AVAudioRecorder alloc]initWithURL:url settings:recordSetting error:&error];

    //开启音量检测

    recorder.meteringEnabled = YES;

    recorder.delegate = self;

}


- (void)detectionVoice

{

    [recorder updateMeters];//刷新音量数据

    //获取音量的平均值  [recorder averagePowerForChannel:0];

    //音量的最大值  [recorder peakPowerForChannel:0];

    

    double lowPassResults = pow(10, (0.05 * [recorder peakPowerForChannel:0]));

    NSLog(@"%lf",lowPassResults);

    //最大50  0

    //图片 -》大

    if (00.06) {

        [self.imageView setImage:[UIImage imageNamed:@"record_animate_01.png"]];

    }else if (0.060.13) {

        [self.imageView setImage:[UIImage imageNamed:@"record_animate_02.png"]];

    }else if (0.130.20) {

        [self.imageView setImage:[UIImage imageNamed:@"record_animate_03.png"]];

    }else if (0.200.27) {

        [self.imageView setImage:[UIImage imageNamed:@"record_animate_04.png"]];

    }else if (0.270.34) {

        [self.imageView setImage:[UIImage imageNamed:@"record_animate_05.png"]];

    }else if (0.340.41) {

        [self.imageView setImage:[UIImage imageNamed:@"record_animate_06.png"]];

    }else if (0.410.48) {

        [self.imageView setImage:[UIImage imageNamed:@"record_animate_07.png"]];

    }else if (0.480.55) {

        [self.imageView setImage:[UIImage imageNamed:@"record_animate_08.png"]];

    }else if (0.550.62) {

        [self.imageView setImage:[UIImage imageNamed:@"record_animate_09.png"]];

    }else if (0.620.69) {

        [self.imageView setImage:[UIImage imageNamed:@"record_animate_10.png"]];

    }else if (0.690.76) {

        [self.imageView setImage:[UIImage imageNamed:@"record_animate_11.png"]];

    }else if (0.760.83) {

        [self.imageView setImage:[UIImage imageNamed:@"record_animate_12.png"]];

    }else if (0.830.9) {

        [self.imageView setImage:[UIImage imageNamed:@"record_animate_13.png"]];

    }else {

        [self.imageView setImage:[UIImage imageNamed:@"record_animate_14.png"]];

    }

}


- (void) updateImage

{

    [self.imageView setImage:[UIImage imageNamed:@"record_animate_01.png"]];

}


你可能感兴趣的:(object-c,动画)