我正在尝试在MacOSX 10.14.x上使用AVAudioRecorder
来获取输入源(在本例中为麦克风)的音频电平读数。由于在iOS上不存在,因此不适用AVAudioSession
。我确实在SandBox设置中启用了麦克风,并在Audio Midi Setup应用程序中仔细检查了所有设置,并将麦克风设置为2ch 24-bit Integer 44.1kHz。我已经删除了所有错误检查,但是所有 都应该没问题(例如,对recorder
对象的所有调用都看似成功)。另外,我在另一台10.14.x机器上尝试了同样的结果。
运行此命令时,即使有音频,我在回调中获得的平均功率和峰值功率的值都为-120.0。
知道我在做什么错吗?
注意:当我启动[recorder prepareToRecord]
时,会收到以下两条错误消息:
no saved enable hardware sample rate converter preference found
ca_debug_string: inPropertyData == NULL
AVAudioRecorder *recorder; // instance variable
NSTimer *levelTimer; // instance variable
NSURL *url = [NSURL fileURLWithPath: @"/dev/null"];
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.00], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityHigh], AVEncoderAudioQualityKey,
nil];
NSError *error = nil;
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
[recorder record];
levelTimer = [NSTimer scheduledTimerWithTimeInterval:0.03
target:self
selector:@selector(levelTimerCallback:)
userInfo:nil
repeats:YES];
- (void)levelTimerCallback:(NSTimer *)timer {
[recorder updateMeters];
float peakPower = [recorder peakPowerForChannel:0];
float avgPower = [recorder averagePowerForChannel:0];
}