AudioUnitInitialize returns -10851 (kAudioUnitErr_

2019-07-20 08:02发布

Suppose the code is:

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

The error manifests in AudioUnitInitialize returning kAudioUnitErr_InvalidPropertyValue and the following message being printed in the debugger console:

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

If you've set a stream format for kAudioUnitScope_Input as well, then a variation of this message will be:

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

2条回答
聊天终结者
2楼-- · 2019-07-20 08:46

Error code -10851 corresponds to kAudioUnitErr_InvalidPropertyValue.

Apparently, the error is solved by performing this initialization before doing AudioUnitInitialize:

AVAudioSession *mySession = [AVAudioSession sharedInstance];
[mySession setCategory: AVAudioSessionCategoryPlayAndRecord error:nil];

Additionally, Apple recommends setting the sampling rate that you intend to use throughout the app:

[mySession setPreferredSampleRate:audio_sample_rate error:nil];
// make sure we got what we wanted
audio_sample_rate = [mySession sampleRate];
查看更多
何必那么认真
3楼-- · 2019-07-20 08:55

This problem mostly occurs when you didn't set category and mode for AudioSession. Try to update like below:

- (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");

}

Don't use ActivateSession line, below line responsible to generate error.

  //[session setActive:YES error:&error];
查看更多
登录 后发表回答