控制 TextToSpeech 函数调用的音量

Control the volume on TextToSpeech function call

大家好

我一直在四处寻找,但似乎找不到合适的答案来集成到我的功能中。我目前基本上使用以下代码:

    private void sayHello(String timeString) {

    textToSpeech.speak(timeString,
    TextToSpeech.QUEUE_FLUSH,
    null);
}

此代码工作正常,但声音太大,并且只能由设备本身的音量控制。我希望能够 adjust/hardcode/be 能够使用 spinner 来控制 TTS 的音量,但似乎无法相应地这样做。

该库是否提供此功能?它可以实现吗?
我还尝试在我的代码中实现以下内容:

KEY_PARAM_VOLUME

但是,我看不到任何使用它的示例,并且它显示了创建函数的错误。有什么建议吗?

public class MainActivity extends AppCompatActivity {

    int androidAPILevel = android.os.Build.VERSION.SDK_INT;
    TextToSpeech tts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int i) {
                start();
            }
        });
    }

    private void start() {
        if (androidAPILevel < 21) {
            HashMap<String,String> params = new HashMap<>();
            params.put(TextToSpeech.Engine.KEY_PARAM_VOLUME, "0.5"); // change the 0.5 to any value from 0-1 (1 is default)
            tts.speak("This is a volume test.", TextToSpeech.QUEUE_FLUSH, params);
        } else { // android API level is 21 or higher...
            Bundle params = new Bundle();
            params.putFloat(TextToSpeech.Engine.KEY_PARAM_VOLUME, 0.5f); // change the 0.5f to any value from 0f-1f (1f is default)
            tts.speak("This is a volume test.", TextToSpeech.QUEUE_FLUSH, params, null);
        }
    }
}