我想询问一个关于简单麦克风音量检测的问题。我的代码在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。