我想问一下简单的麦克风音量检测问题.我的代码适用于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]);
}
Run Code Online (Sandbox Code Playgroud)
它在iOS 6中运行得非常好,但它在iOS 7中没有采样任何东西.结果总是-120.
如果您已激活应用程序的权限,请检入"设置".这就像推送通知一样.
一旦警报响应,它就不会再次弹出.你必须去:
Settings -> Privacy -> Microphone
然后检查你的应用程序的状态.
如果您在那里看不到您的应用,则表示您没有正确请求访问麦克风.试试这个.
if([[AVAudioSession sharedInstance] respondsToSelector:@selector(requestRecordPermission:)]) {
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
if (!granted) {
NSLog(@"User will not be able to use the microphone!");
}
}];
}
Run Code Online (Sandbox Code Playgroud)
希望它有助于提供线索.
干杯!
在 iOS 7 中,用户必须同意访问麦克风的应用程序。
在使用之前,AVAudioRecorder
您必须先请求此同意。这是使用requestAccessForMediaType:completionHandler:
上的方法完成的AVCaptureDevice
。
如果您没有征得同意 - 或者您没有提出请求 - 当您尝试在 iOS 7 上录制任何内容时,您会得到沉默。
归档时间: |
|
查看次数: |
5765 次 |
最近记录: |