我有一个能说话的简单应用。
事情是我在logcat中遇到了这个错误:
TextToSpeech Sucessfully bound to com.ivona.tts
TextToSpeech speak failed : not bound to TTS engine
我在Android清单中没有添加任何特别内容,是否必须?
代码
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
public class Main extends Activity implements OnInitListener{
TextToSpeech tts;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tts = new TextToSpeech(this, this);
tts.speak("Hello World", TextToSpeech.QUEUE_FLUSH, null);
}
@Override
protected void onDestroy() {
MyTTS.release();
super.onDestroy();
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
tts.setLanguage(Locale.getDefault());
} else {
Log.e("TTS", "Initialization failed");
}
}
}
我不知道为什么会出现这种错误。 谁能帮我? 提前谢谢
编辑:我不想把说法放在onInit方法中。我该怎么办?
答案 0 :(得分:2)
在onInit完成后,你只能让引擎说话,所以在onInit()中执行以下操作:
if (status == TextToSpeech.SUCCESS) {
tts.speak("Hello World", TextToSpeech.QUEUE_FLUSH, null);
}