AVAudioRecording metering problem

Discussion in 'iOS Programming' started by Haibane, Jun 28, 2009.

  1. Haibane, Jun 28, 2009
    Last edited by a moderator: Feb 22, 2016

    Haibane macrumors newbie

    Joined:
    Jun 28, 2009
    #1
    Hi Everyone,

    I am totally desperate now with this new class. I managed to make it work and record audio without problems but for some reason the metering does not work.
    Could you please give me some help with this, what am I doing wrong:
    In this very simple example I just want to display the current peak or average level, but I always get 0 or -160.

    some code:
    Code:
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
           
    NSDictionary *recordSettings =
            [[NSDictionary alloc] initWithObjectsAndKeys:
             [NSNumber numberWithFloat: 44100.0],   AVSampleRateKey,
             [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey, [NSNumber numberWithInt: 1],                         AVNumberOfChannelsKey, [NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey, nil];
           
    soundRecorder = [[AVAudioRecorder alloc] initWithURL: recURL
                                                                       settings: recordSettings
                                                                          error: nil];
    
    soundRecorder.meteringEnabled = TRUE;
           
            soundRecorder.delegate = self;
            [soundRecorder prepareToRecord];
            [soundRecorder record];
    
    and I have a timer for checking:
    
    for (int k=0; k < 2; k++) {
                float peak = [soundRecorder peakPowerForChannel:k];
                float average = [soundRecorder averagePowerForChannel:k];
                NSLog(@"Peak power for channel %i: %4.2f",k,peak);
                NSLog(@"Average power for channel %i:%4.2f",k,average);
                NSString *aString = [[NSString alloc] initWithFormat:mad:"%4.2f",peak];
                peakLabel.text = aString ;
    }
    I am not sure with the channels, 0 and 1 gives always 0, all the other gives -160.

    What am I doing wrong? Please help
     
  2. Kib macrumors newbie

    Joined:
    Jul 15, 2009
    #2
    Same problem

    Hi,

    I am having the same problem. I can record and playback audio, but cannot get record-metering to work.

    The functions for controlling metering for recording and playback are symmetrical, so I don't understand why metering for playback works, while metering for recording does not.

    Is this an Apple bug?

    Has anyone found a solution - or a work-around?

    Any info would be much appreciated.

    -K
     
  3. Haibane thread starter macrumors newbie

    Joined:
    Jun 28, 2009
    #3
    Well, the solution is pretty simple. First you have to create the recorder object (init) and THEN you have to call the meteringenabled (or what?) selector then it works. At least for me. Hope works also for you ;)

    so instead of this:
    soundRecorder.meteringEnabled = TRUE;

    soundRecorder.delegate = self;
    [soundRecorder prepareToRecord];
    [soundRecorder record];

    try this:
    soundRecorder.delegate = self;
    [soundRecorder prepareToRecord];
    soundRecorder.meteringEnabled = TRUE;
    [soundRecorder record];
     
  4. BenBenson macrumors newbie

    BenBenson

    Joined:
    Jun 11, 2015
    Location:
    Berlin
    #4
    you need to call

    [recorder updateMeters];

    before reading the values.

    And you have to do this in a loop during recording to get updates all the time...

    Maybe 6 years too late :confused:
     

Share This Page