Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine when Google maps is Speaking in Android

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());
like image 309
William Seemann Avatar asked Feb 07 '19 18:02

William Seemann


People also ask

Why is my Google Maps not talking on my Android phone?

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.

How do I get Google Maps to speak the directions to me?

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.

Why can't I hear my directions on Google Maps?

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).


1 Answers

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();
like image 171
k1slay Avatar answered Nov 22 '22 22:11

k1slay