October 18th, 2011

Background Audio Playback for mobile Adobe AIR apps!

If you build a mobile Adobe AIR app to play or stream audio, the user expects that the music continues to playback when the app goes in the background. When I developed ON AIR powered by Last.fm (Last.fm application using Adobe AIR and targeting tablet devices), had to solve this problem.

Dependent mobile platform, you have to do different things to enable the audio playback in the background!

BlackBerry PlayBook Tablet OS

The PlayBook normally pauses your app, when the app is going into the background and stops everything including the playback of the audio. This is the default behaviour. QNX (Part of RIM/BlackBerry) provides two ActionScript3 libraries which allows to the OS UI Components (qnx-screens.swc) in your app or use OS features or capabilities like QNXSystemPowerMode (qnx-air.swc). These libraries will be shipped with the latest version 4.5.1 of Flash Builder. To add the BlackBerry ActionScript Libraries to the project, you have to enable a property “Add platform specific libraries to library path” in the Project Properties (Flex Build Packaging -> BlackBerry Tablet OS).

After enabling the libraries, you only have to add the following lines which sets the PowerMode when the app inactive in the background to THROTTLED.

import qnx.system.QNXSystem;
import qnx.system.QNXSystemPowerMode;

qnx.system.QNXSystem.system.inactivePowerMode = QNXSystemPowerMode.THROTTLED;

In the PowerMode THROTTLED, the AIR runtime is stopping the rendering but still executes the code to playback the audio.

Here is the documentation about the PowerMode of QNX.

Google Android

On Android, the audio playback in the background is dependent of the device OS version. As multitasking on Android is available since version 3.0 (Honeycomb), only apps running on devices using Honeycomb or later will playback audio in the background.

To ensure that the app is still running when the user isn’t doing anything, you should add following line to keep the app awake.

NativeApplication.nativeApplication.systemIdleMode=SystemIdleMode.KEEP_AWAKE;

Here is the corresponding ActionScript3 documentation. To enable the AIR app to change system idle, the Android part of the AIR App Descriptor file has to contain the following uses permessions.

<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>

Apple iOS

Normally, an AIR app (or native app) on iOS is stopping the audio playback when the app is going in the background. With Adobe AIR 3.0, you can enable the Background Audio Playback for AIR application. Add the parameter UIBackgroundModes with the value audio to the iOS part of the app descriptor of your application.

<iPhone>
 <InfoAdditions>
  <![CDATA[
   <key>UIBackgroundModes</key>
   <array>
    <string>audio</string>
   </array>
  ]]>
 </InfoAdditions>
</iPhone>

Currently, AIR 3.0 has problem with the screen lock of iOS, which is disabling the background audio playback.
To prevent the screen lock on iOS, your app should use the System Idle Mode KEEP_AWAKE.

NativeApplication.nativeApplication.systemIdleMode=SystemIdleMode.KEEP_AWAKE;


9 Responses to “Background Audio Playback for mobile Adobe AIR apps!”

  1. Aaron says:

    Is any special code required for background playback on iOS? Can you post an example of how you set things up (even if only pseudocode)? Also, what can you actually execute while in the background beyond the sound.

  2. Patrick says:

    @Aaron No special code required for iOS! You only have to add the parameter UIBackgroundModes with the value audio to the App Descriptor!

  3. I added the following to the descriptor file:
    UIBackgroundModes

    audio

    The audio plays in the background but it plays over the top of other music or audio streams.

    Is there something I am missing?

    Thanks

  4. Patrick says:

    @Chris
    I tested it running my AIR app in the background and started the iPod app. Both played music at the same time.

    I checked some of the documentation of Apple and if I understand it right it is possible that multiple apps are playing audio in the backgound at the same time.

    “Because multiple applications may support audio, the system limits which applications can play audio at any given time. The foreground application always has permission to play audio. In addition, one or more background applications may also be allowed to play some audio content depending on the configuration of their audio session object.”

    Source: http://www.scribd.com/doc/51765373/85/Playing-Background-Audio

    From the perspective of the following documents, it’s a bug. There’s no way to control the audio session used by Adobe AIR on iOS.

    Apple iOS Developer Documentation – Audio Session Programming Guide

    Apple iOS Developer Documentation about Handling Audio Interruptions

  5. Silvano says:

    Hi Patrick!
    We just downloaded AIR 3.1 to see if they fixed the screen lock issue in iOS. Not successful unfortunately… Hopefully we’ll see it in 3.2 🙁

  6. Christopher says:

    Hey!
    Thanks for making the last.fm app for iPad 🙂 will you add this parameter on a coming update? Would be great to have last.fm in the background while reading a eBook.

    Thanks!

  7. 13Flo says:

    Hi guys,

    Any news about the background feature with AIR 3.4 or 3.5?

    I’m trying to play a simple playlist of sounds when the app is in background but no events are fired at all after Event.DEACTIVATE

    Any feedbackis welcome.

    Regards

  8. 13Flo says:

    Hi guys,

    After some investigations, it seems we can not do background tasks when renderMode is set to direct or gpu, on both Android and iOS.

    Does someone encountered the same issue and maybe found a solution to that issue?

    Thanks!

  9. 13Flo says:

    If you are interested in getting background tasking on iOS/Android when using Stage3D, PLEASE VOTE FOR THE FEATURE REQUEST HERE:
    https://bugbase.adobe.com/index.cfm?event=bug&id=3349232