0

I received the following feedback when submitting an app for Android Auto:

Your app does not support all of the required voice commands. Volume doesn't reduce when voice command is initiated on the Android Auto.

I take this to mean that the app should reduce playback volume (it's a media player app) when the user presses the Speak/Mic button in the Android Auto UI (or also if they scream "Ok, Google" over the sound of their music, I suppose).

I guess the other possible interpretation is that there are voice commands for raising/lowering the volume that need to be supported, but that seems...unlikely. And I'm not seeing any such API hooks documented anywhere.

So I assume it's the former case, and I need to reduce the volume when voice recognition starts.

To do that it seems like I'd need to receive a notification of that event (and preferably, also a notification of when voice recognition has ended). Is there a boradcast intent or other way to trap this in my app so that I can reduce the media playback volume while the user is trying to say things?

aroth
  • 54,026
  • 20
  • 135
  • 176

1 Answers1

1

Found the solution. What the app needs to listen to are audio focus change events, like:

@Override
public void onAudioFocusChange(int focusChange) {
    AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
    if (focusChange == AudioManager.AUDIOFOCUS_LOSS |+ focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
        pause();
        shouldPlayOnFocus = true;
    }
    else if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
        if (! isPlaying()) {
            return;
        }

        //reduce the playback volume
    }
    else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
        if (! isPlaying() && shouldPlayOnFocus) {
            start();
            shouldPlayOnFocus = false;
        }

        //restore stream volume if we reduced it earlier
    }
}

This is covered in the reference documentation, here:

https://developer.android.com/reference/android/media/AudioManager.OnAudioFocusChangeListener.html

Android Auto triggers a transient loss of audio focus event (AUDIOFOCUS_LOSS_TRANSIENT) whenever the 'Speak' button is pressed.

aroth
  • 54,026
  • 20
  • 135
  • 176