iphone

How to make audio meter level with avaudiorecorder


I am trying to create a audio meter level while I am recording the user voice using avaudiorecorder. What can I try to achieve this?


Solution

  • Actually, the code is pretty straightforward since AVAudioPlayer and AVAudioRecorder have built in methods to get you on your way. My approach was this:

    Example:

    NSOperationQueue *queue=[[NSOperationQueue alloc] init];
    NSInvocationOperation *operation=[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(updateMeter) object:nil];
    [queue addOperation: operation];
    

    and

    -(void)updateMeter
    {
      do {
          //don't forget:
          [recorder updateMeters];
           self.averagePower   = [recorder averagePowerForChannel:0];
           self.peakPower      = [recorder peakPowerForChannel:0];
    
           // we don't want to surprise a ViewController with a method call
           // not in the main thread
          [self.delegate performSelectorOnMainThread: @selector(meterLevelsDidUpdate:) withObject:self waitUntilDone:NO];
          [NSThread sleepForTimeInterval:.05]; // 20 FPS
         }while(someCondition);
    }
    

    If your View Controller implements the meterLevelsDidUpdate: method, you can use this method to update your Level Meter.