添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Hi guys

I've been looking around but cannot seem to find a suitable answer to integrate into my function. I am basically using the following code currently:

    private void sayHello(String timeString) {
    textToSpeech.speak(timeString,
    TextToSpeech.QUEUE_FLUSH,
    null);

This code works fine but it's too loud and it can only be controlled by the volume of the device itself. I want to be able to adjust/hardcode/be able to use spinner to control the volume of the TTS but cannot seem to do so accordingly.

Is this functionality available for this library? Is it achievable? I've also tried to implement the following into my code:

KEY_PARAM_VOLUME

However, I cannot see any examples of this being used and it's showing up with the error to create a function. Any advice?

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);
                Thanks for the answer Bunz, works as expected. However, the main volume of the phone still impacts the TTS. For example: if I set the volume to max manually in my code and mute the volume on the phone, I won't hear anything (as you'd expect). Is there a way I can program the function to ignore the volume of the phone?
– Nero
                Sep 30, 2018 at 20:09
                I think you could technically ignore volume button presses while your app is in the foreground, but you can't simply ignore the system volume setting.  Either way, it doesn't sound like a good idea because you're not responsible for whether the phone is muted or not.  One thing you could do is calculate the volume setting in your code based on the volume of the device.  Could ask a new question though as I am not actually sure.
– Nerdy Bunz
                Oct 1, 2018 at 3:25
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.