问题
I am trying to use an AVAudioRecorder instance to record sound, the whole recording progress runs well, except that when I call [recorder peakPowerForChannel:0] trying to get the volume, the return value is always 0, that's pretty odd..
I have checked the recorded audio file, it's totally fine. Then I use AVAudioPlayer to play it, and during playing, i call [player peakPowerForChannel:0], the return value seems to be right and make sense.
How can I get the right value ? I have set that: recoder.meteringEnabled = YES, and called [recorder updateMeters] every time I call [recorder peakPowerForChannel:0]. The initial of recorder is as follows:
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
//General Audio Format Settings
[recordSetting setValue:[NSNumber numberWithInt: 'ima4'] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
//Linear PCM Format Settings
[recordSetting setValue:[NSNumber numberWithInt: 32] forKey:AVLinearPCMBitDepthKey];
[recordSetting setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[recordSetting setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
//Encoder Settings
[recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityMedium] forKey:AVEncoderAudioQualityKey];
[recordSetting setValue:[NSNumber numberWithInt:96] forKey:AVEncoderBitRateKey];
[recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVEncoderBitDepthHintKey];
//Sample Rate Conversion Settings
[recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVSampleRateConverterAudioQualityKey];
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:nil];
recorder.meteringEnabled = YES;
回答1:
Try this. It works for me. I think the problem you are experiencing is that you don't call updateMeters
Set up the recorder once (this part of your code is fine)
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
//Start the actual Recording
[recorder record];
NSTimer *TimerUpdate = [NSTimer scheduledTimerWithTimeInterval:.1
target:self selector:@selector(timerTask) userInfo:nil repeats:YES];
Then define this small routine to get the peak level:
- (void) timerTask {
[recorder updateMeters];
float level = [recorder peakPowerForChannel:0];
NSLog(@"%f", level);
}
回答2:
You have to wait to set the meteringEnabled property until either you've either sent the record message or the prepareToRecord message.
In your specific case, add:
[recorder prepareToRecord];
before
recorder.meteringEnabled = YES;
and it should work.
回答3:
try to test the peak of channels 1 or 2 and not 0
回答4:
It is supposed to return 0, indicating 0dB and maximum power. From Apple's documentation:
Return Value
The current peak power, in decibels, for the sound being recorded. A return value of 0 dB indicates full scale, or maximum power; a return value of -160 dB indicates minimum power (that is, near silence).
http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioRecorder_ClassReference/Reference/Reference.html
You want averagePowerForChannel: instead.
来源:https://stackoverflow.com/questions/1191617/iphone-peakpowerforchannel-function-in-avaudiorecorder-doesnt-work