监控Objective-C中的当前dB级别

时间:2015-02-11 07:18:49

标签: objective-c microphone avaudiorecorder avaudiosession

我试图通过iPhone麦克风显示传入声音的分贝级别,一切都在模拟器中工作,但不适用于实际的iOS设备。它就像iPhone没有检测到声音,这是我的代码:

- (void)viewDidLoad {
    [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
        if (granted) {
            [self setup];

            NSLog(@"granted");
        } else {
            NSLog(@"denied");

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Microphone Access Denied"
                                                            message:@"You must allow microphone access in Settings > Privacy > Microphone"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
        }
    }];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)setup {

    // record audio to /dev/null
    NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];

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

    // create a AVAudioRecorder
    NSError *error;
    self.recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
    [self.recorder setMeteringEnabled:YES];

    if (self.recorder) {
        [self.recorder prepareToRecord];
        self.recorder.meteringEnabled = YES;
        [self.recorder record];
        self.levelTimer = [NSTimer scheduledTimerWithTimeInterval:0.01f target:self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];
        [self.recorder updateMeters];

    }
}

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

    // here is the DB!
    float peakDecebels =  [self.recorder peakPowerForChannel:1];
    NSLog(@"peak: %f", peakDecebels);
    float avaeragePower = [self.recorder averagePowerForChannel:1];
    NSLog(@"averagePower: %f", avaeragePower);

    [self.dbLabel setText:[NSString stringWithFormat:@"db %f",peakDecebels]];
}

1 个答案:

答案 0 :(得分:1)

您需要正确设置音频会话:

NSError* error = nil;
   BOOL success = [[AVAudioSession sharedInstance]     
                            setCategory:AVAudioSessionCategoryRecord 
                                  error:&error];
    if (!success && error) {
        NSLog(@"error %@",error);
    }

这应该在开始录制之前完成。