App switching and graceful music handling in iOS

Posted by on Jul 6, 2012 in Dev | 4 Comments

We post few dev/technical pieces on our blog (I’d love to do more), but we ran into an issue recently that seems to plague many iOS apps on the store: Music not always playing on app resume. It’s not catastrophic when it happens – and can often be fixed by hitting the home button and trying again – but it is annoying.

It may be OpenAL related, but we’re not entirely sure to be honest. (Many more games use OpenAL than we thought at first. Apparently the list includes Angry Birds, Cut the Rope, Flight Control, Bumpy Road, Bit Pilot, Flower Garden, etc.) At first we thought we had a bug in Osmos, but on testing other apps we discovered it’s a fairly widespread issue.

To reproduce it, try switching back and forth (by double-tapping on the Home button) between the current store versions of Flight Control and Flower Garden. After a couple of swaps you’ll find that music in Flight Control often doesn’t resume. Depending on which pair of apps you choose to test you’ll end up with slightly different results — like an extra-complex game of rock-paper-scissors. (Angry Birds Space has impeccable manners though. It always recovers gracefully, and any app that comes after recovers nicely as well. I knew there was a reason behind Rovio’s success!)

So what’s going on here? Well, on startup/resume most apps will check to see if other audio is playing (eg. music or a podcast in iTunes) via a call like

   AudioSessionGetProperty(
      kAudioSessionProperty_OtherAudioIsPlaying,
      &propertySize,
      &audioIsAlreadyPlaying);

If audioIsAlreadyPlaying turns out to be true, polite games will refrain from playing their own music. Sometimes, however, audioIsAlreadyPlaying appears to be true even when it shouldn’t be. Why? It seems to be a timing issue. (Warning: speculation.) It may be that the previous app hasn’t “finished” moving to the background when the new app takes over, giving the new app a temporarily misleading view of the situation.

The solution? (Thanks to Eric Johnson of Semi Secret Software for this tip!) Keep re-polling the status of kAudioSessionProperty_OtherAudioIsPlaying every couple seconds after starting up. Trying a few times over the course of 10 seconds or so ought to do it! We did this for Osmos, and it’s recovering very reliably now.

4 Comments

  1. eddybox
    July 6, 2012

    ps. The store version will continue to suffer from these issues until we submit our next update of course. ;-)

  2. Daniel
    July 7, 2012

    It’s always interesting this kind of behind-the-scenes stuff. Thanks for posting it.

  3. Dev Links: Cleaning Up | The Indie Game Magazine - Indie Game Reviews, Previews, News & Downloads
    July 22, 2012

    […] App Switching And Graceful Music Handling In iOS (Hemisphere Games) “We post few dev/technical pieces on our blog (I’d love to do more), but we ran into an issue recently that seems to plague many iOS apps on the store: Music not always playing on app resume. It’s not catastrophic when it happens – and can often be fixed by hitting the home button and trying again – but it is annoying.” […]

  4. Dev Links: Cleaning Up | DIYGamer
    July 22, 2012

    […] App Switching And Graceful Music Handling In iOS (Hemisphere Games) “We post few dev/technical pieces on our blog (I’d love to do more), but we ran into an issue recently that seems to plague many iOS apps on the store: Music not always playing on app resume. It’s not catastrophic when it happens – and can often be fixed by hitting the home button and trying again – but it is annoying.” […]