Fade out and exit music while talking to text

I have an Android app that uses the TTS (text to speech) API. Everything works fine, but now I want to fade out / turn off or even stop the music (in case the user plays music with a pre-installed music player) when the application says the text. Right now, I think that music and TTS messages are playing in the same stream (MUSIC), and it can be difficult to understand voice messages.

I tried to play the text in another thread, for example AudioManager.STREAM_NOTIFICATIONS. He stops the music, but does not return when the text speaks, so I did not reach the goal. They haven't found anything yet, so I hope someone can help here. Thank!

+1
source share
2 answers

I finally got something that works. Not perfect. Pretty dirty trick. Just in case, this may help someone:

This is fixed in API 8 using the requestAudioFocus and abandomAudioFocus methods for AudioManager.

But for earlier versions, you can try this. Play the TTS through another stream channel, say STREAM_NOTIFICATIONS. Then you just need to return the sound focus to STREAM_MUSIC. How can you achieve this? Sending a silence line ("") to TTS, but this time through STREAM_MUSIC. The effect will be: the music is stopped, your TTS message will be spoken, and finally, your music will return after a voice notification. It’s not nice or proud of something, but ... if someone knows differently, I will appreciate it

+5
source

TextToSpeech.OnUtteranceCompletedListener AudioManager.setStreamVolume ?

0

All Articles