Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android TextToSpeech:speak失败:未绑定到TTS引擎_Android_Text To Speech_Activity Lifecycle - Fatal编程技术网

Android TextToSpeech:speak失败:未绑定到TTS引擎

Android TextToSpeech:speak失败:未绑定到TTS引擎,android,text-to-speech,activity-lifecycle,Android,Text To Speech,Activity Lifecycle,我的TextToSpeech在第一次运行时工作正常,但在使用“back”键关闭并重新打开应用程序后,它就不工作了。错误为TextToSpeech:speak failed:未绑定到TTS引擎,而onInit中的状态为错误 我有一个类来处理TTS: public class VoiceGenerator { public TextToSpeech tts; private Context context; private String TAG = "Voice Genera

我的TextToSpeech在第一次运行时工作正常,但在使用“back”键关闭并重新打开应用程序后,它就不工作了。错误为
TextToSpeech:speak failed:未绑定到TTS引擎
,而
onInit中的
状态
错误

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

你知道我做错了什么吗?

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


您需要在
main活动
上实现
TextToSpeech.OnInitListener
。 这里有一个很好的例子

代码:

在简历上使用

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

问题是状态是错误的,所以这没有帮助。
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);
        }
    }

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