AudioUnitInitialize返回-10851(kAudioUnitErr_InvalidPropertyValue)

时间:2015-01-26 03:34:34

标签: objective-c macos core-audio

假设代码是:

...
status = AudioUnitSetProperty(
    unit,
    kAudioUnitProperty_StreamFormat,
    kAudioUnitScope_Input, element,
    &format,
    sizeof(AudioStreamBasicDescription));
...
status = AudioUnitInitialize(unit);

错误在AudioUnitInitialize返回kAudioUnitErr_InvalidPropertyValue中显示,并在调试器控制台中打印以下消息:

[pool] <aurioc> 806: failed: -10851 (enable 2, outf< 2 ch,  48000 Hz, Int16, inter> inf< 2 ch,      0 Hz, Float32, non-inter>)

如果您也为kAudioUnitScope_Input设置了流格式,则此消息的变体将为:

[pool] <aurioc> 806: failed: -10851 (enable 2, outf< 2 ch,  48000 Hz, Int16, inter> inf< 2 ch,  48000 Hz, Int16, inter>)

2 个答案:

答案 0 :(得分:1)

错误代码-10851对应kAudioUnitErr_InvalidPropertyValue

显然,错误是通过在执行AudioUnitInitialize:

之前执行此初始化来解决的
AVAudioSession *mySession = [AVAudioSession sharedInstance];
[mySession setCategory: AVAudioSessionCategoryPlayAndRecord error:nil];

此外,Apple建议设置您打算在整个应用中使用的采样率:

[mySession setPreferredSampleRate:audio_sample_rate error:nil];
// make sure we got what we wanted
audio_sample_rate = [mySession sampleRate];

答案 1 :(得分:0)

当您未为AudioSession设置类别和模式时,通常会发生此问题。 尝试更新如下:

- (void)configureAudioSession {
   // Configure the audio session
   AVAudioSession *session = [AVAudioSession sharedInstance];
   // we are going to play and record so we pick that category
   NSError *error = nil;
   [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];

   // set the mode to voice chat
   [session setMode:AVAudioSessionModeVoiceChat error:&error];

/*      TODO:
  +        // add interruption handler
  +        [[NSNotificationCenter defaultCenter] addObserver:self
  +                                                 selector:@selector(handleInterruption:)
  +                                                     name:AVAudioSessionInterruptionNotification
  +                                                   object:sessionInstance];
  +
  +        // we don't do anything special in the route change notification
  +        [[NSNotificationCenter defaultCenter] addObserver:self
  +                                                 selector:@selector(handleRouteChange:)
  +                                                     name:AVAudioSessionRouteChangeNotification
  +                                                   object:sessionInstance];
  +
  +        // if media services are reset, we need to rebuild our audio chain
  +        [[NSNotificationCenter defaultCenter]    addObserver:    self
  +                                                 selector:   @selector(handleMediaServerReset:)
  +                                                     name:   AVAudioSessionMediaServicesWereResetNotification
  +                                                   object:   sessionInstance];
  +*/
   NSLog(@"setupAudioSession");

}

不要使用ActivateSession行,负责生成错误的行。

  //[session setActive:YES error:&error];