Java 无法正确停止TextToSpeech

Java 无法正确停止TextToSpeech,java,android,text-to-speech,android-speech-api,Java,Android,Text To Speech,Android Speech Api,我有一个刷新的文本,每次都是这样 我调用位于另一个具有此结构的类中的静态方法speak(): public class Voc { static TextToSpeech mytts; public static void speak(String myText){ mytts=new TextToSpeech(c, new TextToSpeech.OnInitListener() { //.........parameters and con

我有一个刷新的文本,每次都是这样

我调用位于另一个具有此结构的类中的静态方法
speak()

public class Voc {

    static TextToSpeech mytts;

    public static void speak(String myText){
       mytts=new TextToSpeech(c, new TextToSpeech.OnInitListener() {
        //.........parameters and config not related to issue........
       }
       mytts.speak();
    }

    public static off(){
       mytts.stop();
       mytts.shutdown();
    }

}
问题是,如果在多次调用
speak()
后调用
off()
,tts将继续讲话。 如果我只调用一次
speak()
,而不是像刷新方法那样多次调用,则不会发生这种情况。 这让我怀疑
off()
方法并不适用于所有实例,尽管我已将所有
新TextToSpeech(…)
调用分配给类
Voc
中的同一静态字段


如何解决此问题?

您的问题是因为您在每次调用speak()时都实例化了一个新的TextToSpeech对象,这是不需要的。通过我在下面提供的更新,您可以关闭并重新打开单个TTS对象,而不是多个,因此当您关闭它时,单个TTS对象将停止,并且您的问题不再存在

我已经在我的手机上测试过了,它应该完全按照你的需要工作

请在下面查看我的更新代码:

更新的Voc类别

import android.content.Context;
import android.os.Build;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import java.util.Locale;

public class Voc {

    static TextToSpeech mytts = null;

    public static void init(Context c){
        mytts=new TextToSpeech(c, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if(status == TextToSpeech.SUCCESS){
                    int result=mytts.setLanguage(Locale.US);
                    if(result==TextToSpeech.LANG_NOT_SUPPORTED ||
                            result==TextToSpeech.LANG_MISSING_DATA){
                        Log.d("error", "Language not supported");
                    }
                } else
                    Log.d("error", "TTS failed :(");
            }
        });
    }

    public static void speak(final String myText){  
        if(mytts != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                mytts.speak(myText, TextToSpeech.QUEUE_FLUSH, null, null);
            } else {
                //cover all versions...
                mytts.speak(myText, TextToSpeech.QUEUE_FLUSH, null);
            }
        }
    }

   public static void off(){
        if(mytts !=null) {
            mytts.stop();
            //mytts.shutdown();  //calling this here is not what we want
        }
   }

   public static void shutdown(){
        if(mytts !=null) {
            mytts.shutdown();  //if you need call shutdown with this method
        }
   }
}
测试的主要活动

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Voc.init(getApplicationContext()); //this is the method that sets everything up

        Button onButton = (Button) findViewById(R.id.button_on);
        onButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Voc.speak("blah blah blah blah blah blah blah blah");

            }
        });

        Button offButton = (Button) findViewById(R.id.button_off);
        offButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Voc.off();
            }
        });
    }
}
测试布局示例

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.viatechsystems.tts.MainActivity">

<Button
    android:id="@+id/button_on"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click me for TTS"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintHorizontal_bias="0.292"
    app:layout_constraintVertical_bias="0.498" />

<Button
    android:id="@+id/button_off"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Off"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="@+id/button_on"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintHorizontal_bias="0.75"
    app:layout_constraintVertical_bias="0.498" />

</android.support.constraint.ConstraintLayout>


我很困惑,因为我没有在代码的任何部分使用
new Voc()
,但我使用
Voc.speak()
Voc.off()
调用speak和off作为静态方法。我想说的是,您的问题似乎是多次调用Voc.speak(),而在您提供的原始方法中,您正在创建TextToSpache的新对象,因此调用静态Voc.off()只能关闭单个正在运行的TextToSpache对象。例如,在您的代码中,每次调用Voc.speak()时都会运行一个新的TTS对象,当您调用Voc.off()时,很可能只关闭最近运行的对象。-在这种情况下,使用Voc实例可能是个好主意,我的方法不应该创建重叠的TTS对象。代码中的单例位于Voc类上,我多次调用speak,但我不会创建多个
Voc
类实例,因为我没有任何
新Voc()
。我将尝试您的方法,但我期望得到相同的结果。正如预期的那样,我得到了相同的问题以最大限度地提高代码重用,因为我必须在许多不同的活动中调用它,并且配置块(此处省略)在初始化之前使用了许多调整。