我试图让活动在它完成发言后结束,但由于某种原因,我无法理解它告诉我,setOnUtteranceCompleted不适用于文本到语音。我是android编程新手,所以请温文尔雅:-)
这是代码。
public class SpeakActivity extends Activity implements OnUtteranceCompletedListener{
Random randnum = new Random();
TextToSpeech tts = null;
private boolean ttsIsInit = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_speak);
// Show the Up button in the action bar.
setupActionBar();
startTextToSpeech();
}
void startTextToSpeech(){
final int randint = randnum.nextInt(4);
final String text = ((GlobVars) this.getApplication()).getResponse(randint);
tts = new TextToSpeech(this, new OnInitListener() {
public void onInit(int status) {
tts.setOnUtteranceCompletedListener(this);
if (status == TextToSpeech.SUCCESS) {
ttsIsInit = true;
if (tts.isLanguageAvailable(Locale.ENGLISH) >= 0){
tts.setLanguage(Locale.ENGLISH);
}
tts.setPitch(0.5f);
tts.setSpeechRate(0.5f);
if (tts != null && ttsIsInit) {
Log.d("got ere", "spoken");
tts.speak(text, TextToSpeech.QUEUE_ADD, null);
}
}
}
});
}
// shut down tts to free the TTS resources
@Override
public void onDestroy() {
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
@Override
public void onUtteranceCompleted(String arg0) {
((GlobVars) this.getApplication()).setListen(true);
this.finish();
}
}发布于 2013-07-13 04:45:52
我不确定,但根据setOnUtteranceCompletedListener()的文档,您可能需要使用TextToSpeech.OnUtteranceCompletedListener listener作为参数。我认为这个函数的使用方法如下所示。请注意,如果您希望在调用onUtteranceCompleted函数时对UI进行任何更改,请使用runOnUIThread方法。
TextToSpeech tts= new TextToSpeech(context, new OnInitListener() {
@Override
public void onInit(int status) {
tts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {
@Override
public void onUtteranceCompleted(String utteranceId) {
//Do things here
}
});
}
});以上来源:检查onUtteranceCompleted does not get called?问题。
希望这能有所帮助。
https://stackoverflow.com/questions/17623201
复制相似问题