AVAudioSession setActive throws unknown error

Here is what I am trying to do play a short short sound (tone) when an event is triggered and the app is in the background. This needs to work even when the ringer is off (as the user will be using this feature to find their phone). So the AVAudioPlayer with category playback seems like my only viable option, and it was working fine until recently I encountered the following issue:


When the app is in the background, and I call the setActive method on the instance of the AVAudioSession, I get the following error (in the NSError passed to the aforementioned method):


NSOSStatusErrorDomain code=561015905, the description is null


The app has audio background permission in the Info plist, and the same code used to work fine a week ago. Now I get the error on simulator as well as actual hardware device. Can you please help me understand what exactly is the problem, and/or any other mechanism of achieving my objective stated above.

Replies

Not an unknown error, you can find it in AVAudioSession.h it's the FourCC '!pla'


AVAudioSessionErrorCodeCannotStartPlaying

You can't arbitrarily play sounds from the background. So given only the information provided, here are some some questions to ask.


How is the app not being suspended because of being backgrounded? Generally, audio needs to be initiated from the foreground then transitioned to the background (which will keep you playing using the audio plist key). What is being done to stay running?


Is the app receiving remote control events? Am I expecting to be the "Now Playing" app?


Was the audio session previously set to be Mixable in the past? Mixable will remove some restrictions.


What has changed in the last week? Code changes, installed a new OS version on the device, tested on another device, installed a new Xcode, tested using a different version of the simulator, installed new apps on the device and so on.

My bad, should've checked the source for the error code! To answer your questions:


The app is indeed suspended when in background, however, it 'wakes up' upon receiving bluetooth events from a BLE peripheral. One of the specific cases is when someone wants to find their phone using the BLE peripheral, in which case I need the phone to play a sound irrespective of it being muted (which is what the user would expect) - so I'm not doing anything to keep the app running but instead depending on the delegate calls from CBCentralManager


Right now I'm achieving this through AudioToolbox, but it doesn't work when the hardware mute button is on, and the only change I made was install a new version of Xcode. There is a hack which I think may work - playing a sound file with no sound on loop, however it seems really wasteful (seems, because I've not checked energy impact, but given that I'll most likely be playing a real sound only a few times a week, I highly doubt it is a good solution)


Also, I'm doubting my initial assertion that it worked earlier (sound started playing when app was IN background) - so please ignore that.

Thanks for the details. My suggestion at this point would be that you should at the least be "Mixable" i.e. use AVAudioSessionCategoryOptionMixWithOthers along with the Playback Category.


The trick to this however is that even when the session is mixable there are some internal checks as to the "why" an application is awake before it is allowed to play audio. In certain situation it is not allowed to play, geolocation being an example.

So, try mixable and if that doesn't work, file a bug and attach a sample project that illustrates what you're attempting.

Hi one.more.kiran,

I'm experiencing the same issue, have you been able to resolve it on your side. I've tried setting the category to mixable with no difference.

Thanks