Android-如何及时更改TTS语音速率?

时间:2019-03-10 07:56:29

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

我刚刚开始使用TTS API进行编程。我有一个问题,非常感谢您的帮助。
我想实现一个功能,即当用户在TTS引擎讲话时每次单击“较慢”按钮时,语速都会立即降低。因此,我创建了一个测试项目。
在这个项目中,我在屏幕上有两个按钮,一个是“说话”,另一个是“较慢”。
这是activity_main.xml中的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Speak"
        android:id="@+id/speakBtn"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="slower"
        android:id="@+id/slowerBtn"
        android:layout_below="@id/speakBtn"
        android:layout_alignLeft="@id/speakBtn"/>

</RelativeLayout>


当用户单击“讲话”按钮时,TTS引擎将先说存储在变量“ article1”中的字符串,然后是存储在“ article2”中的字符串。我还创建了一个发言线程,以防止UI线程卡住。当用户在TTS引擎讲话时单击“较慢”按钮时,希望语速会立即降低。但是我发现只有在下次调用引擎时,语音速率才会改变。
例如,当我在引擎说第1条消息时单击较慢的按钮时,直到第2条开始讲话时,语速才及时改变。
这是MainActivity.java中的代码:

package com.yenice.test;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {
    private TextToSpeech t1;
    private Button speakBtn;
    private Button slowerBtn;
    private float speechRate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        speakBtn = (Button)findViewById(R.id.speakBtn);
        slowerBtn = (Button)findViewById(R.id.slowerBtn);
        t1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                    t1.setLanguage(Locale.US);
                    speechRate = 2;
                    t1.setSpeechRate(speechRate);
                }

            }
        });

        speakBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String article1 = "Google has already demonstrated that Settings is a prime candidate for Slices. In fact, examples the company showed off were far more advanced. One for “Mobile Data” noted inline how many gigabytes were used, while a mockup for “Brightness level” featured a slider right in the Slice. The latter does not even appear as a toggle today.";
                final String article2 = "Most interestingly in today’s announcement, though, is the reveal that this won’t be Pixel-exclusive for too much longer. Google says that Duplex will be rolling out to more Android devices, and even iOS, in the “coming weeks.” Google’s documentation suggests that any Android phone with Google Assistant will eventually be able to use the feature, as well as iOS devices using the Assistant app.";
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        t1.speak(article1, TextToSpeech.QUEUE_FLUSH, null);
                        while (t1.isSpeaking()) {
                            ;
                        }
                        t1.speak(article2, TextToSpeech.QUEUE_FLUSH, null);
                    }
                }).start();
            }
        });

        slowerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ((t1.isSpeaking()) && (speechRate > 0.1)) {
                    speechRate -= 0.1;
                    t1.setSpeechRate(speechRate);
                }
            }
        });


    }

    @Override
    public void onDestroy(){
        if(t1 !=null){
            t1.stop();
            t1.shutdown();
        }
        super.onDestroy();
    }
}


反正有及时改变语速吗? 感谢您的答复。

0 个答案:

没有答案