Android TextToSpeech:说不通:没有绑定到TTS引擎

时间:2016-12-22 13:40:01

标签: android text-to-speech activity-lifecycle

我的TextToSpeech在第一次运行时工作正常,但在使用“后退”键关闭应用程序并重新打开后,它无效。错误为TextToSpeech: speak failed: not bound to TTS enginestatus中的onInitERROR

我有一个处理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();    
}

关于我做错的任何想法?

3 个答案:

答案 0 :(得分:2)

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

代码:

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);
        }
    }

}

答案 1 :(得分:0)

使用onResume

@Override
protected void onResume() {
super.onResume();
myTTS = new TextToSpeech(this, this);
     }
}

答案 2 :(得分:-1)

在onInit完成后,你只能让引擎说话,所以在onInit()中执行以下操作:

   if (status == TextToSpeech.SUCCESS) {
     tts.speak("Hello World", TextToSpeech.QUEUE_FLUSH, null);      

   }
相关问题