iOS 7上麦克风无法工作

7

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


1
我也遇到了同样的问题,当我在“设置”应用程序中检查时,我的应用程序已经被授权使用麦克风。 - Shizam
@EricPoon...你能修复一下吗?因为我有一个应用程序,无法从麦克风录制声音,但只能在iPad Air和iOS 7上使用。 - Beto
3个回答

6

请在设置中查看您是否已激活应用程序的权限。这类似于推送通知。

一旦警报得到回答,它就不会再弹出。您需要前往: 设置 -> 隐私 -> 麦克风然后检查您的应用程序状态。

如果您无法在那里看到您的应用程序,则表示您未正确请求访问麦克风。请尝试此操作。

if([[AVAudioSession sharedInstance] respondsToSelector:@selector(requestRecordPermission:)]) {
    [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
        if (!granted) {
            NSLog(@"User will not be able to use the microphone!");
        }
    }];
}

希望这能给你提供一些线索。祝好!

3

iOS 7中,用户必须同意应用程序访问麦克风。

在使用AVAudioRecorder之前,您必须首先请求此同意。这可以使用AVCaptureDevice上的requestAccessForMediaType:completionHandler:方法来完成。

如果您没有同意或者还没有请求,那么在iOS 7上尝试录制任何内容时会得到静默。


1
提供给阅读此文和Manuel的回复的任何人的信息是,文档还指出:“使用AVMediaTypeAudio调用此方法等同于调用AVAudioSession方法requestRecordPermission:。” - qix

0

我曾经遇到过同样的问题,以下是我解决问题的方法:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[audioSession setActive:YES error:nil];

if([audioSession respondsToSelector:@selector(requestRecordPermission:)]){
    //ios7
    [audioSession requestRecordPermission:^(BOOL granted) {
        if(granted){
            [self recordNow];
        }else{
            NSLog(@"RECORD NOT AUTHORIZED");
        }
    }];
}else{
    //before ios7
    [self recordNow];
}

你在iPad Air上遇到了这个程序的问题吗?因为我有一个应用程序,无法从麦克风录制声音,但只能在iPad Air上使用。 - Beto

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接