views:

106

answers:

2

I have an Android application that makes use of TTS (Text to speech) API. Everything is working perfectly, but now i want to fade in/out or even stop music (in case user is playing music with prebuilt Music Player), when application speaks a text. Right now, i think both music and TTS messages are played on the same stream (MUSIC), and it can be difficult to understand the voice messages.

I've tried to play the text on a different stream, like AudioManager.STREAM_NOTIFICATIONS. It does stop the music, but doesn't come back when the text is spoken, so i didn't achieve the goal. Haven't found anything yet, so i hope someone can help here. Thanks!

+1  A: 

I finally got something that is working. Not perfect though. A quite dirty trick. Just in case it can help to someone:

This is fixed on API 8 with requestAudioFocus and abandomAudioFocus methods of AudioManager.

But for former versions, you can try this. Play TTS through a different stream channel, let's say STREAM_NOTIFICATIONS. Then you just need to return audio focus to STREAM_MUSIC. How can you achieve that?. Sending a silence string (" ") to TTS but this time through STREAM_MUSIC. The effect will be: music is stopped, your TTS message gets spoken, and finally your music is back after the voice alert. Not nice or something to feel proud of, but... if someone knows of a different way, i will appreciate it

ggomeze
A: 

Could you use the TextToSpeech.OnUtteranceCompletedListener along with AudioManager.setStreamVolume to achieve this?

aganders3
Don't think that work, in the case you play your TTS message through STREAM_MUSIC, which is the same as background music. If you play it through another stream, like STREAM_NOTIFICATION, you will still have to deal with focus return. And i've only found one way to fix it, which is the dirty hack explained on my previous post
ggomeze
I see. I realize now this was more about returning focus after the "utterance," which you're right - it doesn't look like it was possible without `requestAudioFocus`.
aganders3