Android - Google Navigation and Music Player Audio interaction

   Google Navigation's audible instructions works well with music playing applications like Play Music,  Amazon prime music. Music apps basically reduce their stream volume during the navigation instructions and eventually reverts back to the original volume. This implementation is based on AudioManager APIs.

   Navigation app requests for a transient audio focus which is relayed by the framework to the music application holding audio focus.

       AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE);

       am.requestAudioFocus(afChangeListener,
                             AudioManager.STREAM_MUSIC,
                             AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

 This event is notified to music app via its OnAudioFocusChangeListener,

OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() {
    public void onAudioFocusChange(int focusChange) {
        if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
            ...
        } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
            ...
        }
    }
};


 Apps like Play music and Amazon music traps AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK to reduce the volume and restore it when they are notified of AUDIOFOCUS_GAIN. This seems to be recommended behavior by Google. However, applications could choose to pause and resume playback too. It makes more sense for application to give users an option to pick the desired behavior with a default implementation. Users listening to a live radio stream would rather like their content to be paused then being mixed with audible driving instructions.

No comments: