title: Monitor current dB level in Objective-C
body: <p>I'm trying to display the decibel level of incoming sound via the iPhone microphone, everything works in the simulator, but does not work on an actual iOS device. It's like the iPhone is not detecting sound, here is my code:</p>
  
  <pre><code>- (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 &gt; Privacy &gt; 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:&amp;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]];
  }
  </code></pre>