AVAudioRecorder 不在 OS X Mavericks 上录制

AVAudioRecorder doesn't record on OS X Mavericks

在我的 cocoa 应用程序中,我尝试从内置麦克风录制语音。为此,我正在使用 AVAudioRecorder class。在下面的代码中,我设置了 AVAudioRecorder 实例。

- (void)setupAudioRecorder {

    NSMutableDictionary *recordSettings = [NSMutableDictionary dictionary];

    recordSettings[AVFormatIDKey] = @(kAudioFormatLinearPCM);
    recordSettings[AVSampleRateKey] = @(16000.0);
    recordSettings[AVNumberOfChannelsKey] = @(1);
    recordSettings[AVLinearPCMBitDepthKey] = @(16);
    recordSettings[AVLinearPCMIsBigEndianKey] = @(NO);
    recordSettings[AVLinearPCMIsFloatKey] = @(NO);

    recordSettings[AVEncoderBitRateStrategyKey] = AVAudioBitRateStrategy_Variable;
    recordSettings[AVEncoderAudioQualityForVBRKey] = @(AVAudioQualityMedium);
    recordSettings[AVSampleRateConverterAlgorithmKey] = AVSampleRateConverterAlgorithm_Mastering;

    recordSettings[AVSampleRateConverterAudioQualityKey] = @(AVAudioQualityMedium);

    NSString *file = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)
                   objectAtIndex:0]
                  stringByAppendingPathComponent:@".recorder_voice.caf"];

    NSFileManager *fm = [NSFileManager defaultManager];

    if ([fm fileExistsAtPath:file])
        [fm removeItemAtPath:file error:nil];

    NSURL *url = [NSURL fileURLWithPath:file];
    NSError *err = nil;

    self.recorder.delegate = nil;
        self.recorder = [[AVAudioRecorder alloc] initWithURL:url
                                            settings:recordSettings
                                               error:&err];
    if(!self.recorder){
    // show error
    }

    [self.recorder setDelegate:self];
    [self.recorder prepareToRecord];

    self.recorder.meteringEnabled = YES;
}

在 Yosemite 上一切正常,但在 Mavericks 上我却保持沉默。执行 [self.recorder prepareToRecord]; 时系统日志显示错误:

AQMEIO.cpp:377 _FindIOUnit: 错误 -66680。

这是什么意思,我该如何解决?

我终于找到了答案。原因在于沙盒项目设置。我没有在 Target -> Capabilities -> App Sanbox 中选中麦克风框。 Yosemite忽略这个问题,但小牛不会。