我試圖使用AVAudioRecorder的averagePowerForChannel方法來監視iPad/iPhone應用程序的麥克風輸入級別。我有一個回調輪詢平均水平 - 在iPhone上它工作正常,並返回合理的水平,但由於某種原因在iPad上它總是返回-120.0。AVAudioRecorder averagePowerForChannel總是返回-120.0
下面是我的一些設置代碼:
- (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];
}
以及我開始記錄方法:
- (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];
}
和位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]);
...
}
凡iPhone,NSLog語句將返回明智的值,並且iPad將始終返回-120.0。
注意:我在一個cocos2d應用程序中使用了這個。出於某種原因,如果我在iPad上重新啓動當前場景,話筒電平將返回正確的值。
任何人有任何建議嗎?我在這裏非常遺憾。 謝謝!
也許它會幫助你 http://stackoverflow.com/questions/24991908/why-does-averagepowerforchannel-always-return-160 – 2014-08-04 03:39:43