Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Haibane

macrumors newbie
Original poster
Jun 28, 2009
5
0
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
 
Last edited by a moderator:
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
 
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];
 
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:
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.