AVAudioRecorder averagePowerForChannel sempre retorna -120,0
Estou tentando usar o método averagePowerForChannel do AVAudioRecorder para monitorar os níveis de entrada no microfone para um aplicativo para iPad / iPhone. Eu tenho um retorno de chamada que pesquisa o nível médio em um loop - no iPhone ele funciona bem e retorna níveis sensíveis, mas por alguma razão no iPad ele sempre retorna -120,0.
Aqui está um pouco do meu código de instalação:
- (void) setupMic {
if (micInput) {
[micInput release];
micInput = nil;
}
NSURL *newURL = [[NSURL alloc] initFileURLWithPath:@"/dev/null"];
NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] init];
[recordSettings setObject:[NSNumber numberWithInt:kAudioFormatAppleLossless] forKey: AVFormatIDKey];
[recordSettings setObject:[NSNumber numberWithFloat:22050.0] forKey: AVSampleRateKey];
// [recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
[recordSettings setObject:[NSNumber numberWithInt:12800] forKey:AVEncoderBitRateKey];
[recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSettings setObject:[NSNumber numberWithInt: AVAudioQualityLow] forKey: AVEncoderAudioQualityKey];
micInput = [[AVAudioRecorder alloc] initWithURL:newURL settings:recordSettings error:nil];
// [micInput setMeteringEnabled:YES];
[newURL release];
[recordSettings removeAllObjects];
[recordSettings release];
}
Bem como o meu método de gravação inicial:
- (void) startRecording {
NSLog(@"startRecording!");
[micInput pause];
[micInput prepareToRecord];
micInput.meteringEnabled = YES;
[micInput record];
[micInput updateMeters];
levelTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:0.0] interval:0.03 target:self selector:@selector(levelTimerCallback:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:levelTimer forMode:NSDefaultRunLoopMode];
}
e um pouco do retorno de chamada levelTimer:
- (void)levelTimerCallback:(NSTimer *)timer {
[micInput updateMeters];
double avgPowerForChannel = pow(10, (0.05 * [micInput averagePowerForChannel:0]));
[micSprite receiveInput:avgPowerForChannel];
NSLog(@"Avg. Power: %f", [micInput averagePowerForChannel:0]);
...
}
Onde no iPhone, a instrução NSLog retornará valores sensíveis e o iPad sempre retornará -120,0.
Nota: Estou usando isso dentro de um aplicativo cocos2d. Por alguma razão, se eu reiniciar a cena atual no iPad, os níveis do microfone retornarão os valores corretos.
Alguém tem alguma sugestão? Estou seriamente perdido aqui. Obrigado!