Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I lower the volume of music playing in another app while my Text to Speech "talks"?

I have an app that uses Text To Speech, and also allows the user to call up their music player. [for now I'm trying to avoid coding my own player] I would like to lower the volume or pause the music being play in the background [not my app] while my TTP is talking.

I was able to request sound focus using AudioManager just before my TTS, but then I don't know how to give it back. I have been searching for a while and I can't seem to get it right.

I appreciate any help you can give me, Thank you.

PS: I am aiming for Android version 2.2+

...
//Intent to load player    
Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
startActivity(intent);
...
//requesting focus
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);

these are some of the links I have look and gotten me closer [I think]

  • fade in and out music while speaking a text
  • http://developer.android.com/training/managing-audio/audio-focus.html
  • Android launching music player using intent
like image 772
Huasillo Avatar asked Jan 22 '12 05:01

Huasillo


1 Answers

I was able to request sound focus using AudioManager just before my TTS, but then I don't know how to give it back.

Call abandonAudioFocus().

like image 98
CommonsWare Avatar answered Sep 22 '22 09:09

CommonsWare