I am currently working on an app where I need to use TTS. The problem is that if the user is listening to music, you cant hear TTS. I have figured out that I can use AudioFocus
to temporarily take focus, and then release it. Taking the focus pauses pandora (the application I'm currently trying to work with), but releasing it doesn't do anything.
I know when going into a phone call, it is able to pause pandora and everything else, but I couldn't find anything in the source code controlling this.
Is there any other way to resume pandora or any other external music player?
I use this to pause the music.
if(am.isMusicActive()) {
int result = am.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
if(result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED){
}
}
And release it in the onDestroy
am.abandonAudioFocus(this);
I have tried to resume it by the following sections:
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "play");
this.sendBroadcast(i);
and
Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent downEvent = new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
sendOrderedBroadcast(downIntent, null);
The first one plays plays the native music app (and it's obvious why, but I thought it was worth a shot). The second one does nothing. I've also used it to try to pause the music, but didn't work as well.