Hi,
I
am trying to write an audio app that needs to continue when the app is
put in the background. I have set the appropriate background modes (both
audio and voip), in the plist file and I have a long running thread
that wakes up the app every now and then while it is in the background.
Most of the time it works. However, there is one case with which I am
having a problem.
My
app receives the interrupt notification when a phone call come in, and
then again when the phone call has ended. On the interrupt begin
notification, my app's audio operation is automatically suspended and I
also call AudioSessionSetActive(false) although I suspect that this may
not be necessary. On the interrupt end notification, I attempt to
resume the audio operation by calling AudioSessionSetActive(true) and
then AudioOutputUnitStart().
I have set up my audio interruption handler using AudioSessionInitialize().
If
the phone call comes in when my app is in the foreground, everything
works fine. My app's audio operation gets suspended when the phone call
comes in and then resumes when the phone call ends.
However,
if my app is in the background when the phone call comes in, then when
the phone call ends, the call to AudioSessionSetActive(true) fails. If I
ignore that failure and try to call AudioOutputUnitStart(), it fails as
you would expect, with this message "17:59:46.943
<com.apple.main-thread> AUIOClient_StartIO failed (-12985)" I have
tried using a timer and retrying the operation after 20 seconds, but it
still fails. Is there any way I can find out what that error number means?
If I bring the app back in the foreground and try AudioSessionSetActive(true), then everything resumes working normally.
I
also tried setting a periodic timer and putting the app in the
background. Every time the timer fires, I start an audio operation for a
short time and then stop it. In that case, I use
AudioOutputUnitStart() and AudioOutputUnitStop() on every timer event.
This works fine and no call to AudioSessionSetActive(true) is
necessary. As as long as no other audio app runs, my app is able to
restart it's audio operation on every timer event.
Am
I doing something wrong? Is there no way that a background app can
resume audio operation after being interrupted by another audio app? Or
do I need to do something more than AudioSessionSetActive(true).
I was curious to see if other apps do something similar. I looked at one app: Waze. I setup a route and pushed the app into the background and started driving. I confirmed that it does in fact resume after an incoming phone call and it keeps giving voice instructions while still in the background. However, Waze only does playback and my app does recording and I don't know if that distinction matters.
Thanks,
Richard