我想问一个简单的麦克风音量检测问题。我的代码适用于 iOS 6 或更低版本但不适用于 iOS 7,我的代码如下所示:
-(void)viewDidLoad{
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
NSError *error;
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
if (recorder) {
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
[recorder record];
} else{
NSLog([error description]);
}
}
// then call periodically with the following method, volumeLevelSampling
-(void)volumeLevelSampling{
[recorder updateMeters];
NSLog(@"Average input: %f Peak input: %f", [recorder averagePowerForChannel:0], [recorder peakPowerForChannel:0]);
}
它在 iOS 6 中运行良好,但在 iOS 7 中没有采样。结果始终为 -120。