In the following automotive audio examples, the vehicle head unit runs
Android 9 and includes a radio application and a
navigation application. In addition, the vehicle tuner is externally routed
and plays over the speakers. In real-world use cases, it may be advantageous
to handle the tuner as an input to Android and have the Radio app read from
the tuner and write to an AudioTrack
object.
User starts radio
In this interaction sequence, no media is playing in the vehicle when the user presses Play for a preset frequency in the radio application. The radio application must gain focus for the tuner to play sound over the speakers.
- Radio: "Tune to FM 96.5."
- Radio: Request focus GAIN.
- AudioManager: GAIN granted.
- Radio:
createAudioPatch()
- Radio: "Play tuner output."
- Externally-routed tuner: Mixer enables Tuner audio route to amplifier.
Radio ducks navigation prompt
In this interaction sequence, the radio is playing when the navigation
application generates a navigation prompt for a next turn announcement. The
navigation application must obtain transient focus from the
AudioManager
to play the navigation prompt.
- Radio: "Play tuner output."
- Externally-routed tuner: Mixer enables Tuner audio route to amplifier.
- Navigation: Request focus GAIN TRANSIENT from
AudioManager
. - AudioManager: GAIN TRANSIENT to Navigation.
- Navigation: Open stream, send packets.
- Navigation: Context GUIDANCE routed on bus1.
- Mixer: Ducks Tuner to play bus1 GUIDANCE on Speakers.
- Navigation: Announcement over, close stream.
- Navigation: Abandon focus.
The AudioManager
recognizes the radio playback can duck and would
normally apply a ducking factor to the music stream without notifying the
radio application. However, the framework ducking is bypassed by overlaying
framework/base/core/res/res/values/config.xml
and setting
config_applyInternalDucking
to false
, so the
external tuner continues to provide sound and the radio application remains
unaware of any changes. The mixer (below the HAL) is responsible for combining
the two inputs and can choose to duck radio playback or move radio playback to
the rear speakers.
When the navigation prompt is complete, the navigation application releases focus and radio playback resumes.
User launches audio book application
In this interaction sequence, the user launches an audio book application, causing radio playback to stop (pressing play in a streaming music app would be a similar trigger).
- Audio Book: Request GAIN context MEDIA from
AudioManager
. - Radio loses focus:
- AudioManager: LOSS.
- Radio:
releaseAudioPatch()
- Audio Book gains focus:
- GAIN granted, Context MEDIA routed on bus0
- Open stream, send MEDIA packets.
The request for focus by the audio book application is not transient, and so the previous focus holder (the radio application), receives a permanent focus loss, to which the radio application responds by tearing down the patch to the tuner. The mixer stops listening to the tuner and starts processing the audio delivered via the Audio HAL (it may also optionally perform a cross fade as part of the radio-to-audiobook transition).
Navigation prompt takes focus
In this interaction sequence, the audio book is playing when the navigation application generates a navigation prompt.
- Audio Book: Streaming MEDIA packets, focus no-concurrent.
- Navigation: Request GAIN TRANSIENT.
- AudioManager: LOSS TRANSIENT.
- Audio Book: Stops.
- Audio Manager: GAIN TRANSIENT granted.
- Navigation: Open stream, send packets.
- Navigation: Context GUIDANCE routed on bus1.
- Mixer: Plays bus1 (GUIDANCE).
- Navigation: Announcement over, close stream.
- Navigation: Abandon focus.
- Audio Book: GAIN.
- Audio Book: Restarts.
Because the original audio book application AudioFocusRequest
(sent when starting the AudioTrack
) included the
AUDIOFOCUS_FLAG_PAUSES_ON_DUCKABLE_LOSS
flag, the
AudioManager
determines it cannot handle ducking for the audio
book app. Instead, AudioManager
sends an
AUDIOFOCUS_LOSS_TRANSIENT
message to the audio book app, which is
expected to respond by suspending its playback.
The navigation application can now play the navigation prompt without interruption. When the navigation prompt is complete, the audio book regains focus and resumes playback.