6

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]

Community
  • 1
  • 1
Huasillo
  • 71
  • 1
  • 6

1 Answers1

3

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

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I think this where I am having problems. I missing something here. I don't know what to use for AudioManager.OnAudioFocusChangeListener. when using audioManager.requestAudioFocus I was able to "get away" will declaring that null. [I'm thinking it should be something else, but not sure what or how], Thanks! abandonAudioFocus (AudioManager.OnAudioFocusChangeListener l) – Huasillo Jan 22 '12 at 13:17
  • @Huasillo: You provide, to each of those, an instance of some object that implements the `AudioManager.OnAudioFocusChangeListener` interface. This is no different than how you handle any other listener in Android. – CommonsWare Jan 22 '12 at 13:26
  • Thank you!!... I tried that before, but it was late at night, and I wasn't saving "AudioManager.OnAudioFocusChangeListener l" , this morning and after your comment, it worked. Much appreciated! – Huasillo Jan 22 '12 at 13:29
  • If I don't need to know when the focus is changed may I put null in the listener? – melanke Jul 21 '14 at 14:20
  • @melanke: I do not know -- I have not tried that. I would hope that it would work, but I cannot guarantee it. – CommonsWare Jul 21 '14 at 16:31
  • Well I tested and its not throwing exceptions and working fine so far. – melanke Jul 21 '14 at 23:47