TTS对象tts始终保持null
。
onInit
始终显示Toast消息失败。
我已经尝试了所有其他创建onInitlistener的方法作为单独的方法。
但onInit的状态代码始终为-1
,即错误值。
请帮助!!
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSpeechButton = (Button) findViewById(R.id.speechButton);
mSpeechButton.setEnabled(false);
mSpeechTextView = (TextView) findViewById(R.id.speechText);
// Hide the ActionBar
getActionBar().hide();
tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status != TextToSpeech.ERROR) {
tts.setLanguage(Locale.getDefault());
mSpeechButton.setEnabled(true);
mTrue = true;
} else {
mTrue = false;
Toast.makeText(getApplicationContext(), "Failed",
Toast.LENGTH_LONG).show();
}
}
});
mSpeechButton.setOnClickListener(this);
}
答案 0 :(得分:0)
package com.example.textspeech;
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
public class MainActivity extends Activity {
TextToSpeech mTextToSpeech;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextToSpeech = new TextToSpeech(getApplicationContext(),
new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
mTextToSpeech.setLanguage(Locale.UK);
mTextToSpeech.speak("You will use Eclipse IDE to create an Android application and name it as TextToSpeech under a package", TextToSpeech.QUEUE_FLUSH, null);
}
});
}
}