Search code examples
androidnavigationmedia-playerspeech-synthesis

Pausing Android MediaPlayer for Synthesized Speech


I have an Android MediaPlayer running that I want to pause when the speech synthesizer starts speaking (specifically, when the Google Navigation app gives directions). I'm pretty sure this is possible, as the standard music player seems to have this behavior.

I've already learned (from the Stack Overflow Gods) how to pause the MediaPlayer during phone calls:

Pause music player on a phone call and again resume it after phone call in android

I'm surprised that I haven't been able to find a similar mechanism to listen for events from the speech synth. I assume I've missed something, and if anyone can just point me at link or even hint at an answer here, I'd be most grateful.


Solution

  • I have found the answer for this:

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

    The navigation app calls for audio focus using the "Notifications" stream. I requested audio focus for that stream (and the music stream as well) in my application. Pause/restart the player in the callback method for audio focus changes and you're good to go.

    Note: This won't work for all synthesized speech. I've tried it using the test button in the speech synthesis settings menu and that doesn't fire the callback. It only works if the "competing" applications are nice enough to have requested audio focus for an appropriate stream.