AVAudioSessionErrorCodeCannotStartRecording when recording in the background with iOS 13

Hello.


I am working on adding support for iOS 13. Previously the app has been able to record audio for transmition while in the background when the user presses an external button, such as one from a wired headset. This is important as many users such as first responders are currently able to talk by pressing a button, rather than pulling the phone out of a pocket, unlocking it, launching the app etc.


I can't find any sort of recording in the background permission to put in the info plist. Are there any special categories or options needed in the Audio Session to permit this on iOS 13? Failing that, is there a way to launch the app when the user presses the button ( wired or bluetooth le) so the recording can start?


How does the Messages App allow recording on the notification? Inter-App Audio?


Thanks.

We use audio units for recording. It's a lot of code and I cannot share it. Project has audio background mode capability. I have used iPadOS 13.1 beta (I hope that iOS 13.1 beta is the same). If the application (on iOS 12.4) is in the background and something interrupts the app during recording (alarm, phone call) then it cannot resume recording (AudioOutputUnitStart failed with error AVAudioSessionErrorCodeCannotStartRecording). When I tested it on iPadOS 13.1 beta then it worked. I didn't try with AVAudioRecorder.

DTS response on Aug 27, 2019,due to overriding privacy considerations,apps will not in general be allowed to start recording while in the background,Engineering has made some decisions about what will happen in the short term.😟

I don’t get this error anymore on iOS 13.1 beta 1 - seems to be resolved.

Could you tell me which API do you use? AVFoundation or AudioToolBox? Which device do you use? Iphone or iPad?

I'm using AVAudioEngine on an iPhone.

Actually, I spoke too soon. This issue seems to be resolved only after interruptions, but it still exists when changing audio route. So for example, if you connect bluetooth headphones while the app is backgrounded, the audio recording will stop and will fail to recover.

Please file a bug report about this behavior.

Hi, 3ZS


Here is files for you, 3ZS

BackgroundStartRecording.mov : video for using this project

AugRecorder.zip : Xcode Project


https://www.dropbox.com/sh/5rrrs9jeqzgo9tu/AABCqe31KqJhhsS4EVuMLnBla?dl=0


# Run This Project on Any iPhone with iOS 12.4 or iOS 13 beta for iOS 13.1 beta


## Execute Summary

when .inactive : AVAudioRecorder.record() got true

when .background : AVAudioRecorder.record() got false

on SettingsViewController.swift L26~30


## prepare

1. Play Music For create Player and begin Background Mode Audio

- play button : Recorder Tab - bottom left

2. Record(PH) for create recorder

- record(PH) button : Recorder Tab - middle right

3. pause player and recorder

- recorder pause button : middle left

- player pause button : bottom left

4. Setup RemoteCommand for recording

- turn on using RemoteCommand switch

- Setting Tab - using RemoteCommand switch


## try Recording when .inactive

1. show Control Center - swipe TopRight Edge down on iPhone X

2. touch 'prev track' button

- got: true - recorder.record() result


## try Recording when .background

1. exit Application - swipe Bottom Edge up on iPhone X

- now home screen

2. show Control Center - swipe TopRight Edge down on iPhone X

- now show control center 'AugRecorder' with active prevTrack button

3. touch 'prev track' button

- got: false - recorder.record() result


---------- Debug Console Logs ----------

## prepare

2019-09-02 13:50:55 +0000 set audio session: soloAmbient ( , , ) ==> playback ( , , ) in 0.0005

2019-09-02 13:51:02 +0000 set audio session: playback ( , , ) ==> playback ( , , ) in 0.0003

2019-09-02 13:51:04 +0000 set audio session: playback ( , , ) ==> playback ( , , ) in 0.0006

2019-09-02 13:51:07 +0000 set audio session: playback ( , , ) ==> playAndRecord ( HFP, SPKR, A2DP) in 0.2391

recorder.record() true

## try Recording when .inactive

recorder() ==> true

## try Recording when .background

recorder() ==> false

nevos, thank you. your answer was help.

my app was convert from audio unit to AVAudioEngine and no error yet.

AVAudioSessionErrorCodeCannotStartRecording - iOS 12.4 - 13.1~


Is not possibile to start Recording when device is in Background.



If I start a Recording in foreground no problem appears, if I switch in background during the recording no problem yet.



But if the recording time is terminated and new recording is schedule immediately after (~ 1 /2 Seconds ) and device is in background the Recording don’t start.



With this in log:

-CMSUtilities- CMSUtility_IsAllowedToStartRecording: CMSession: Client <private> with PID *** is in the background and doesn't have the entitlement to start recording in the background.

I resolved this problem with CallKit.

When CallKit is actived, try open mic.

How????


Can u explain well how it's work?

I use AudioQueue for recording. Recording restart in background after interrupt does not work on iOS 13.0 beta, but works fine on lastest iOS 13.1 beta, looks like issue is fixed by Apple.

does it work for you after "route changes" (e.g. when you connect disconnect headphones / airpods)?

Yes, it also works fine.

I launch app -> plug headphones -> talk to Siri (audio session interrupt began and ended) -> app continues to record -> unplug headphones -> talk to Siri -> app continues to record

Tested on iOS 13.1 beta 2

AVAudioSessionErrorCodeCannotStartRecording when recording in the background with iOS 13
 
 
Q