2

I'm using AUAudioUnit to play audio that the app is streaming from the server. My code works fine in the foreground. But when I background the app, it won't play the audio. I got the following error.

[aurioc] AURemoteIO.cpp:1590:Start: AUIOClient_StartIO failed (561145187)

The error code 561145187 means AVAudioSessionErrorCodeCannotStartRecording

This error type usually occurs when an app starts a mixable recording from the background and it isn’t configured as an Inter-App Audio app.

This is how I set up the AVAudioSession in Swift:

try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .voiceChat, options: [.allowBluetooth])

This is how I set up the AUAudioUnit in Objective-C:

AudioComponentDescription description;
description.componentType = kAudioUnitType_Output;
description.componentSubType = kAudioUnitSubType_VoiceProcessingIO;
description.componentManufacturer = kAudioUnitManufacturer_Apple;
description.componentFlags = 0;
description.componentFlagsMask = 0;

self.format = [[AVAudioFormat alloc] initWithSettings: @{
                                                       AVFormatIDKey: @(kAudioFormatLinearPCM),
                                                       AVSampleRateKey: @16000,
                                                       AVNumberOfChannelsKey: @1,
                                                       AVLinearPCMIsFloatKey: @NO,
                                                       AVLinearPCMBitDepthKey: @16,
                                                       AVLinearPCMIsBigEndianKey: @NO
                                                       }];

self.audioUnit = [[AUAudioUnit alloc] initWithComponentDescription:description options:0 error:nil];

When I call the startHardwareAndReturnError method, that's when I get that error message.

[self.unit startHardwareAndReturnError:outError];

I already set the Background Modes - Audio capability. And I'm pretty sure I set up the AVAudioSession so it's non-mixable. I'm not even recording. I'm just trying to play the audio. What else am I missing?

tsoang
  • 75
  • 1
  • 8
  • I am curious if you were able to resolve it. I am in a similar situation trying to use Record on a Custom Keyboard. – xoail Jun 18 '20 at 14:06
  • Yes, i solved it by using a different framework. I use Audio Queue framework which allows me to play audio when the app is in the background – tsoang Jun 18 '20 at 20:09
  • Nice! care to share the sample? I am trying to find more info on Audio Queues but seems sparingly documented for swift. – xoail Jun 18 '20 at 21:39
  • Here's the Apple programming guide: https://developer.apple.com/library/archive/documentation/MusicAudio/Conceptual/AudioQueueProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40005343-CH1-SW1 – tsoang Jun 19 '20 at 01:48
  • @tsoang Can you elaborate on exactly what you did for it to work? Following the programming guide as well as the SpeakHere sample project, I still get the same error code 561145187 if I start recording from the background. This answer: https://stackoverflow.com/a/42020251/5593080 mentions some kind of multitasking flag, as does this answer: https://stackoverflow.com/a/16898661/5593080 I'm wondering if you did something similar and if you could shed some light. – arius Jun 10 '21 at 02:28

2 Answers2

1

To record audio in the background, you have to start the audio unit in the foreground. Then the audio unit will continue to run in the background. If you don't have any data to play, you can play silence.

If you are not recording audio, then don't use the playAndRecord session type. Use one of the play-only session types instead.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
  • I'm writing a push-to-talk app. When the app is backgrounded, it may or may not get a push audio message from the server. Playing a silent sound doesn't seem acceptable. Do you think I shouldn't use AUAudioUnit for streaming audio in the background? What other classes would you suggest I use? Thx. – tsoang Apr 21 '20 at 19:21
  • @hotpaw2 it is possible to start audio recording when my application is in background ? i want to start audio recording when my BLE device is trigger even my application running in background. every thing working fine but audio recording not starting. Can you please help me !!! Thanks. – Jay Bhalani Aug 09 '22 at 12:48
0

Some case, you'd better set AVAudioSessionCategoryOptionMixWithOthers in you iOS default AVAudioSessionCategoryOptions.

Note that, some case ...

Beckon
  • 128
  • 2
  • 15