如何使TextToSpeech静音?

时间:2019-06-04 16:17:45

标签: android text-to-speech google-text-to-speech

我希望用户能够关闭TextToSpeech的音量。我对此有一个按钮。在此刻,我关闭了TTS并在每次用户重新打开它时创建新实例,这似乎是一轮完整的操作方式。

有没有一种更有效的方法可以做到这一点,我看不到默认方法可以做到这一点?

enter image description here

1 个答案:

答案 0 :(得分:0)

解决方案是创建一个包并将其作为第三个参数传递。在代码中,您应该使用params.putFloat(KEY_PARAM_VOLUME, 0.0f);该代码对其进行更好的解释:

private Bundle params;

public TextToSpeechHelper(Context context) {
    mTTS = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if (status == TextToSpeech.SUCCESS) {
                int result = mTTS.setLanguage(Locale.ENGLISH);
                params = new Bundle();

                if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                    Log.e("TTS", "Language not supported");
                }
            } else {
                Log.e("TTS", "Initialization failed");
            }
        }
    });
}

public void speakThis(String whatToSay){
    String speech = whatToSay;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mTTS.speak(speech,TextToSpeech.QUEUE_ADD, params, null);
    } else {
        mTTS.speak(speech, TextToSpeech.QUEUE_ADD, null);
    }
}

public void mute() {

    params.putFloat(KEY_PARAM_VOLUME, 0.0f);

}

public void soundOn() {

    params.putFloat(KEY_PARAM_VOLUME, 1.0f);

}