Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TTS volume control

Is there any way to control volume of TTS engine when sending request to TTS engine? Can I able to use AudioManager here?

Thank You.

like image 484
Chandana Avatar asked Nov 29 '22 10:11

Chandana


2 Answers

speak() method can now control TTS volume

added in API level 21

int speak (CharSequence text, 
                int queueMode, 
                Bundle: KEY_PARAM_VOLUME;..., 
                String utteranceId)

Parameter KEY_PARAM_VOLUME, key to specify the speech volume relative to the current stream type volume used when speaking text.
Volume is specified as a float ranging from 0 to 1 where 0 is silence, and 1 is the maximum volume (the default behavior).

like image 22
Gina Kalani Avatar answered Dec 06 '22 11:12

Gina Kalani


you can get this in TTS speak() methods, but only starting from API level 11.

To keep backwards compatibility, you could target the higher api level (with lower min sdk) and use a "@TargetApi(api_level)" decorator along with sdk version check.

/**  speak the single word, at a lower volume if possible */
protected void speakOneWord(String text) {
    int apiVer = android.os.Build.VERSION.SDK_INT;
    if (apiVer >= 11){
        speakApi13(text);
    } else {
        // compatibility mode
        HashMap<String, String> params = new HashMap<String, String>();
        mTts.speak(text, TextToSpeech.QUEUE_ADD, params);
    }
}   

/**  speak at a lower volume, for platform >= 13 */
@TargetApi(13)
protected void speakApi13(String text) {
    HashMap<String, String> params = new HashMap<String, String>();
    params.put(TextToSpeech.Engine.KEY_PARAM_VOLUME, "0.1");
    mTts.speak(text, TextToSpeech.QUEUE_ADD, params);
}   
like image 79
user1276782 Avatar answered Dec 06 '22 11:12

user1276782