How to set volume for text-to-speech “speak” method?

后端 未结 2 466
春和景丽
春和景丽 2020-12-28 10:43

I\'m at a lost. I want to be able to adjust the speak volume. Whatever I do, I can\'t increase its volume. How do I make it as loud as that found in the Android settings (as

2条回答
  •  失恋的感觉
    2020-12-28 11:10

    Try using AudioManager.STREAM_MUSIC when calling the setStreamVolume(...) method. The example speech is affected by the media volume if I adjust the volume of music playback on my phone so I guess STREAM_MUSIC is what you need.

    EDIT: This code works perfectly for me...

    AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    int amStreamMusicMaxVol = am.getStreamMaxVolume(am.STREAM_MUSIC);
    am.setStreamVolume(am.STREAM_MUSIC, amStreamMusicMaxVol, 0);
    tts.speak("Hello", TextToSpeech.QUEUE_FLUSH, null);
    

    The max volume for STREAM_MUSIC on my phone is 15 and I've even tested this by replacing amStreamMusicMaxVol in my call to am.setStreamVolume(...) above with the values 3, 6, 9, 12, 15 and the volume of the speech is correctly set.

提交回复
热议问题