如何在ios 7中获得麦克风音量?

前端之家收集整理的这篇文章主要介绍了如何在ios 7中获得麦克风音量?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有一个观点来获得ios 7中的麦克风音量?
NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];

NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithFloat: 44100.0],AVSampleRateKey,[NSNumber numberWithInt: kAudioFormatAppleLossless],AVFormatIDKey,[NSNumber numberWithInt: 0],AVNumberOfChannelsKey,[NSNumber numberWithInt: AVAudioQualityMax],AVEncoderAudioQualityKey,nil];

NSError *error;

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

if (recorder) {
    //recorder.delegate = self;
    [recorder prepareToRecord];
    recorder.meteringEnabled = TRUE;
    [recorder record];
    levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.1 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];
} else {
    NSLog(@"");// mic error message
}

- (void)levelTimerCallback:(NSTimer *)timer {
    [recorder updateMeters];

    const double ALPHA = 0.05;
    double peakPowerForChannel = pow(10,(ALPHA * [recorder averagePowerForChannel:0]));
    db = 20* log10(peakPowerForChannel);
    db += taraturaDb;
    db = db < 0 ? 0 : db;
    lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;

    if (lowPassResults < 0.95) {
        NSLog(@"%f",db);
}

这在iOS6中有效,在iOS 7中不起作用

谢谢

解决方法

在初始化AVAudioRecorder之前放置这些行:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:YES error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];

猜你在找的iOS相关文章