Search code examples
androidtext-to-speech

Android text to speech volume not changing


In an Android Studio app, the volume in the following is not changing; it remains at the loudest setting. The commented code is depreciated, but is working. How do I use the bundle in the new API?

public void speakthetext2(String s) {
    final String msg = s;
    tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if (status != TextToSpeech.ERROR) {

                //HashMap<String, String> params = new HashMap<>();
                //params.put(KEY_PARAM_VOLUME, "0.20");

                Bundle b = new Bundle();
                b.putString(KEY_PARAM_VOLUME,"0.10");
                tts.speak(msg, TextToSpeech.QUEUE_ADD, b, "1");

//noinspection deprecation
                //tts.speak(msg, TextToSpeech.QUEUE_FLUSH, params);

            } else {
                Toast.makeText(getApplicationContext(), "tts error!", Toast.LENGTH_LONG).show();
            }
        }
    });
}

Solution

  • Use

    b.putFloat(KEY_PARAM_VOLUME, 0.1f);
    

    instead of putString.