Example Interaction Sequences

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.

Figure 1. Radio gains focus and tuner play over speakers
  1. Radio: "Tune to FM 96.5."
  2. Radio: Request focus GAIN.
  3. AudioManager: GAIN granted.
  4. Radio: createAudioPatch()
  5. Radio: "Play tuner output."
  6. 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.

Figure 2. Radio playback ducks navigation prompt
  1. Radio: "Play tuner output."
  2. Externally-routed tuner: Mixer enables Tuner audio route to amplifier.
  3. Navigation: Request focus GAIN TRANSIENT from AudioManager.
  4. AudioManager: GAIN TRANSIENT to Navigation.
  5. Navigation: Open stream, send packets.
    1. Navigation: Context GUIDANCE routed on bus1.
    2. Mixer: Ducks Tuner to play bus1 GUIDANCE on Speakers.
  6. Navigation: Announcement over, close stream.
  7. 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).

Figure 3. Audio book takes focus from radio playback
  1. Audio Book: Request GAIN context MEDIA from AudioManager.
  2. Radio loses focus:
    1. AudioManager: LOSS.
    2. Radio: releaseAudioPatch()
  3. Audio Book gains focus:
    1. GAIN granted, Context MEDIA routed on bus0
    2. 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).

In this interaction sequence, the audio book is playing when the navigation application generates a navigation prompt.

Figure 4. Navigation prompt takes focus from audio book playback
  1. Audio Book: Streaming MEDIA packets, focus no-concurrent.
  2. Navigation: Request GAIN TRANSIENT.
  3. AudioManager: LOSS TRANSIENT.
  4. Audio Book: Stops.
  5. Audio Manager: GAIN TRANSIENT granted.
  6. Navigation: Open stream, send packets.
    1. Navigation: Context GUIDANCE routed on bus1.
    2. Mixer: Plays bus1 (GUIDANCE).
  7. Navigation: Announcement over, close stream.
  8. Navigation: Abandon focus.
  9. Audio Book: GAIN.
  10. 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.