AVAudioRecorder中的iphone-peakPowerForChannel函数不起作用

时间:2009-07-28 02:14:55

标签: iphone avaudiorecorder

我正在尝试使用AVAudioRecorder实例来录制声音,整个录制进度运行良好,除了当我调用[recorder peakPowerForChannel:0]试图获取音量时,返回值始终为0,这很奇怪。

我已经检查了录制的音频文件,它完全没问题。然后我使用AVAudioPlayer播放它,在播放期间,我调用[player peakPowerForChannel:0],返回值似乎是正确的并且有意义。

我怎样才能获得正确的价值?我已经设置了:recoder.meteringEnabled = YES,每次调用[recorder peakPowerForChannel:0]时都调用[recorder updateMeters]。 记录器的初始如下:

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;

4 个答案:

答案 0 :(得分:3)

您必须等待设置meteringEnabled属性,直到您发送了record消息或prepareToRecord消息。

在您的具体情况下,请添加:

[recorder prepareToRecord];

之前

recorder.meteringEnabled = YES;

它应该有用。

答案 1 :(得分:3)

试试这个。这个对我有用。我认为您遇到的问题是您没有调用updateMeters

设置一次录音机(这部分代码很好)

[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];

然后定义这个小程序以获得峰值水平:

- (void) timerTask {
   [recorder updateMeters];
   float level = [recorder peakPowerForChannel:0];
   NSLog(@"%f", level);
}

答案 2 :(得分:0)

尝试测试通道1或2的峰值而不是0

答案 3 :(得分:0)

应该返回0,表示0dB和最大功率。来自Apple的文档:

  

返回值
  记录声音的当前峰值功率(以分贝为单位)。返回值0 dB表示满量程或最大功率;返回值-160 dB表示最小功率(即接近静音)。

http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioRecorder_ClassReference/Reference/Reference.html

你想要averagePowerForChannel:改为。