我无法让TextToSpeech工作。
在我看来,我无法通过onInit方法初始化,因为我似乎无法调试此代码。因此,我想知道如何以编程方式解雇它。
或许我的环境设置错了。我正在运行Android Studio Beta 3分钟SDK 22.
public class MyActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
TextToSpeech tospeech;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tree_play);
tospeech = new TextToSpeech(this, this);
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tospeech.setLanguage(Locale.UK);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
}
else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
tospeech.speak("I am a dog",TextToSpeech.QUEUE_FLUSH,null,null);
}
else
{
tospeech.speak("I am a dog", TextToSpeech.QUEUE_FLUSH, null);
}
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
}
答案 0 :(得分:0)
好的,上面的代码没错。我的问题是我有一个异步任务,它会在onInit触发之前运行完毕。
解决方案是将异步任务的执行调用放入onInit方法。这样,可以保证在继续之前初始化TextToSpeech变量。