I'm trying to modify my application to pause audio playback when Google maps is announcing a turn by turn direction.
I've added the following code (shown below) to my application. The audio focus listener is called when applications like Pandora Radio or Spotify request audio focus in order to play music but it's not called when Google maps announces a turn by turn direction. Is there another intent I should be listening for in order to detect this behavior?
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.requestAudioFocus(new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setAudioAttributes(
new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build()
)
.setAcceptsDelayedFocusGain(true)
.setOnAudioFocusChangeListener(new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
// This is called by Pandora Radio and Spotify
Log.d("Focus change:", " Event is: " + focusChange);
}
}).build());
You won't hear Google Maps' voice navigation if your device's volume is low or muted. Press the Volume Up button on your phone to increase navigation voice volume. If your phone is hooked to your car's speaker, use your car's volume control to increase Google Maps' volume level.
You can get Google Maps to talk to you by enabling talk navigation to receive spoken directions and cues that will help you find your destination. You might need to enable or adjust the voice feature's volume to ensure that you can hear its directions.
For Android Tap the volume up button at the side of your device to increase the volume (preferably to the highest level) and check if the Google Maps voice navigation now works. Alternatively, you can navigate to Settings > Sound. Then check that Media volume isn't on the lowest level (or zero).
You will need AudioManager
's AudioPlaybackCallback
updates.
This only works on Android O and above.
To do this you have to access the audio manager -
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
And then add the listener like this -
Handler handler = new Handler();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
audioManager.registerAudioPlaybackCallback(new AudioManager.AudioPlaybackCallback() {
@Override
public void onPlaybackConfigChanged(List<AudioPlaybackConfiguration> configs) {
super.onPlaybackConfigChanged(configs);
// This will be called when navigation audio state on google maps changes
Log.d("audio active", String.valueOf(audioManager.isMusicActive()));
}
}, handler);
}
The List<AudioPlaybackConfiguration> configs
returned in the callback has a AudioAttribute
object which contains a string describing the audio playing. For Google maps navigation the String constant value is USAGE_ASSISTANCE_NAVIGATION_GUIDANCE
which you can compare to be sure that it is Google Maps announcing the navigation direction.
Programatically you can get it like this
// Loop through the configs to see the media's usage data
configs.get(0).getAudioAttributes().getUsage();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With