Search code examples
iosaudiounit

AudioUnitRender got error kAudioUnitErr_CannotDoInCurrentContext (-10863)


I want to play the recorded audio directly to speaker when headset is plugged in an iOS device.

What I did is calling AudioUnitRender in AURenderCallback func so that the audio data is writed to AudioBuffer structure.

It works well if the "IO buffer duration" is not set or set to 0.020seconds. If the "IO buffer duration" is set to a small value (0.005 etc.) by calling setPreferredIOBufferDuration, AudioUnitRender() will return an error:

kAudioUnitErr_CannotDoInCurrentContext (-10863).

Any one can help to figure out why and how to resolve it please? Thanks


Solution

  • The buffer is full so wait until a subsequent render pass or use a larger buffer.

    This same error code is used by AudioToolbox, AudioUnit and AUGraph but only documented for AUGraph.

    To avoid spinning or waiting in the render thread (a bad idea!), many of the calls to AUGraph can return: kAUGraphErr_CannotDoInCurrentContext. This result is only generated when you call an AUGraph API from its render callback. It means that the lock that it required was held at that time, by another thread. If you see this result code, you can generally attempt the action again - typically the NEXT render cycle (so in the mean time the lock can be cleared), or you can delegate that call to another thread in your app. You should not spin or put-to-sleep the render thread.

    https://developer.apple.com/reference/audiotoolbox/kaugrapherr_cannotdoincurrentcontext