New Audio Capabilities in AIR 3

AIR 3 has two important new audio capabilities:

  1. Device speaker selection. Play audio through a phone’s speaker, or through its earpiece.
  2. Background audio playback on iOS. Keep audio playing in the background when a user switches away from your application, and even when the screen is switched off.

Device Speaker Selection

Toggling between a phone’s speaker and earpiece for playing audio is as easy as setting the audioPlaybackMode property on SoundMixer like this:

// Play audio through the speaker:
SoundMixer.audioPlaybackMode = AudioPlaybackMode.MEDIA;

// Play audio through the earpiece:
SoundMixer.audioPlaybackMode = AudioPlaybackMode.VOICE;

Background Audio

Background audio playback has always worked on Android because of the nature of the Android’s multi-tasking implementation. Starting with AIR 3, it works on iOS, as well. To allow audio to continue playing when your application is closed — and even when the device’s screen is turned off — add the following to your application descriptor:

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

I wrote a sample application that demonstrates both these concepts called DeviceSpeakerExample. The code is available on github, and/or you can download the Flash Builder project.

10 Responses to New Audio Capabilities in AIR 3

  1. Roche says:

    Hello,

    I try to reproduce this but when I click on Earpiece button I’ve an error:

    Variable flash.media::AudioPlaybackMode is not defined.
    at DeviceSpeakerExample/toggleSpeaker()

    I’ve copy the air 3.0 sdk in a copy of 4.5.1 sdk.

    When I compile I’ve no problem, but impossible to find the class AudioPlaybackMode

  2. julio says:

    Hello,

    When I click on Earpiece button I’ve an error regarding to AudioPlaybackMode.
    It’s not found whereas when I compil i haven’t no error.

    I dont’ understand why

  3. Christian Cantrell says:

    @julio: What device are you using? If it’s an Android device, make sure you have the AIR 3 runtime installed.

  4. Mitch says:

    It is really strange. I use “AIR for Android” so it should be included, but the compiler tells me that it is not defined!

    Szene 1, Ebene ‘Ebene 1’, Bild 1, Zeile 32 1120: Zugriff auf eine nicht definierte Eigenschaft AudioPlaybackMode.

  5. Ferro says:

    Can this work with a looping sound while the screen is off? or a playlist? I cannot play a sound on an interval (timer) every second while the screen is off. Any help?

  6. Pingback: Slides, Links, and Questions From my MAX 2011 Presentation « Christian Cantrell

  7. Pingback: Adding the AIR 3.0 SDK to Flash Pro CS5.5 « Jay Armstrong – Flashes of Interest

  8. Silvano says:

    Hi Christian! Thanks for all the hard work…
    I ran this for a new app that we are building and it works like a charm. Here is a question..
    I get the music from a service in my server, if I have the app running i have no problem. But when the iphone goes to sleep, I do hear that song till it finishes. But it obviously doesn’t now what to do after that. Any suggestions ?

  9. Subodh says:

    Hi,
    i was able to play it on Android devices, but in iPad2 it didn’t worked (Device Speaker Selection)
    Is there anything or setting i have to implement to get it work on iPad.
    I have implemented your DeveiceSpeakerExample provided.

    Thanks

  10. Subodh says:

    Hi,
    Can we Detect Whether A Headset Is Plugged In Or Not in our application.