安卓 TextToSpeech : speak failed: not bound to TTS engine

标签 android text-to-speech activity-lifecycle

我的 TextToSpeech 在第一次运行时工作正常,但在使用“后退”键关闭应用程序并重新打开后它就不起作用了。错误是 TextToSpeech: speak failed: not bound to TTS engineonInit 中的 statusERROR

我有一个类来处理 TTS:

public class VoiceGenerator {
    public TextToSpeech tts;
    private Context context;
    private String TAG = "Voice Generator";

    private static VoiceGenerator instance;

    private VoiceGenerator(Context context){
        this.context = context;
    }

    public static VoiceGenerator getInstance(Context context){
        if (instance == null) {
            instance = new VoiceGenerator(context);
        }

        return instance;
    }

    public void initializeTTS() {
        if (tts == null) {
            Log.d(TAG, "initialize tts");
            tts = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
                @Override
                public void onInit(int status) {
                    if (status != TextToSpeech.ERROR) {
                        Log.d(TAG, "initialize tts success");
                        tts.setLanguage(...);                           
                    }      
                }
            });    
        }
    }

    public void speak(){
        tts.speak(...)
    }

    public void shutdown(){
        if(tts != null) {   
            tts.stop();
            tts.shutdown();
            tts=null;
            Log.d(TAG, "TTS Destroyed");
        }
    }

}

我在 onCreate 中获取了 VoiceGenerator 的实例:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);   
    voiceGenerator = VoiceGenerator.getInstance(this);
}

在 onStart 中初始化 TTS:

@Override
protected void onStart(){
    super.onStart();
    voiceGenerator.initializeTTS();
}

并在 onDestroy 中关闭它:

@Override
protected void onDestroy() {
super.onDestroy();
voiceGenerator.shutdown();    
}

对我做错了什么有什么想法吗?

最佳答案

您需要在 MainActivity 上实现 TextToSpeech.OnInitListener。 这是一个很好的例子。

代码:

public class MainActivity extends ActionBarActivity  implements TextToSpeech.OnInitListener{

    private TextToSpeech engine;
    private EditText text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = (EditText) findViewById(R.id.text);
        engine = new TextToSpeech(this, this);
    }



    public void speakText(View v) {

        String textContents = text.getText().toString();
        engine.speak(textContents, TextToSpeech.QUEUE_FLUSH, null, null);

    }

    @Override
    public void onInit(int i) {


        if (i == TextToSpeech.SUCCESS) {
            //Setting speech Language
            engine.setLanguage(Locale.ENGLISH);
            engine.setPitch(1);
        }
    }

}

关于安卓 TextToSpeech : speak failed: not bound to TTS engine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41284509/

相关文章:

android - 从相机应用程序返回时重新创建 Activity

android - 如何从 Realm 对象中删除所有记录而不在android中获取它?

multithreading - 如何在python线程中使用pyttsx

objective-c - iOS7 和 iOS 9 中的 AVSpeechSynthesizer 发音率不同

c# - 如何在 Visual C#/C++ 中实现文本转语音 (TTS)?

java - Android 中是否有类似于 C/C++ 中的 "int main"的函数,其中包含程序的主循环?

android - 使用多个Activity时,Activity来到前台后如何处理retained data?

android - 预期 BEGIN_OBJECT 但在第 1 行第 1 列路径中为 STRING - Laravel 到 Retrofit 2

android - TextView "fill_parent"没有填充父级

android - onScroll 事件未针对 ListView 触发