public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences gameSettings = getSharedPreferences("LangPreferences",0);
String lang=gameSettings.getString("Lang", "");
System.out.println("@@menu"+lang);
setContentView(R.layout.alarm);
toSpeak="Alarm Received weak up";
Toast.makeText(this, "Alarm Recieved", Toast.LENGTH_LONG).show();
ttobj=new TextToSpeech(Alarm.this,
new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR){
ttobj.setLanguage(Locale.UK);
}
}
});
//String toSpeak = write.getText().toString();
speak();
}
@Override
public void onPause(){
if(ttobj !=null){
ttobj.stop();
ttobj.shutdown();
}
super.onPause();
}
public void speak()
{
Toast.makeText(getApplicationContext(), toSpeak,
Toast.LENGTH_SHORT).show();
try{ ttobj.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}
catch(Exception e)
{
}
}我在做语音报警应用。我在alarm类中编写了TTS,它显示了我给TTS引擎的警报和吐司消息,但TTS引擎没有说话
logcat装箱显示它
12-17 15:00:01.264: W/TextToSpeech(31970): speak failed: not bound to TTS engine发布于 2014-12-18 13:49:26
您只能在onInit()被called.So之后调用speak(),最好在onInit()中调用speak方法。下面我将修改onInit()方法。
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
ttobj.setLanguage(Locale.US); //here u have to set your local language
}
if(status==TextToSpeech.SUCCESS){
speak();
}
}https://stackoverflow.com/questions/27522404
复制相似问题