by Christian Cantrell

 Comments (6)

Created

September 26, 2011

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.

COMMENTS

  • By Roche - 12:42 PM on September 27, 2011   Reply

    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

  • By julio - 9:19 AM on September 28, 2011   Reply

    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

  • By Christian Cantrell - 4:48 PM on September 28, 2011   Reply

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

  • By Mitch - 4:59 PM on September 30, 2011   Reply

    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.

  • By Ferro - 4:25 AM on October 6, 2011   Reply

    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?

  • By Silvano - 11:26 PM on October 19, 2011   Reply

    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 ?

ADD A COMMENT