[英]how to check whether tts has finished speaking?
我创建了一个 Text To Speech android 应用程序。 我想将按钮文本从说话更改为停止,然后在完成说话时再次说话。
它有两个按钮——全部清除和说话。 说话应更改为单击时停止,并在说话完成后再次说话。
我在 StackOverflow 和网络上搜索了很多,包括他们的官方文档,但找不到适合我的程序的解决方案。
请帮助我,我是 android 新手。 谢谢你!
//text to speech
t1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR)
t1.setLanguage(Locale.UK);
}
});
//onClick
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String toSpeak = ed1.getText().toString();
if(flag==0) { //flag variable to check state and to change button text
if(toSpeak=="")
{
Toast.makeText(getApplicationContext(), "Empty, enter something!", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext(), "Speaking", Toast.LENGTH_SHORT).show();
t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
flag = 1;
b1.setText("Stop");
flag = 1;
}
}
else {
flag=0;
String te=ed1.getText().toString();
ed1.setText("");
toSpeak = ed1.getText().toString();
ed1.setText(te);
t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
b1.setText("Play");
Toast.makeText(getApplicationContext(), "Stopped", Toast.LENGTH_SHORT).show();
}
}
});
//clear all button. clears the text field and changes text on the button1 to play
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ed1.setText("");
b1.setText("Play");flag=0;
}
});
不要使用 'TextToSpeech.OnUtteranceCompletedListener' 使用下面的一个:
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
yourTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onDone(String utteranceId) {
// Log.d("YourActivity", "TtS Compeleted ");
}
@Override
public void onError(String utteranceId) {
}
@Override
public void onStart(String utteranceId) {
}
});
} else {
Log.e("YourActivity", " TTS Failed!");
}
}
尝试这个
textToSpeech=new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status==TextToSpeech.SUCCESS){
int result=textToSpeech.setLanguage(Locale.ENGLISH);
if (result==TextToSpeech.LANG_MISSING_DATA||result==TextToSpeech.LANG_NOT_SUPPORTED){
Log.i("TextToSpeech","Language Not Supported");
}
textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String utteranceId) {
Log.i("TextToSpeech","On Start");
}
@Override
public void onDone(String utteranceId) {
Log.i("TextToSpeech","On Done");
}
@Override
public void onError(String utteranceId) {
Log.i("TextToSpeech","On Error");
}
});
}else {
Log.i("TextToSpeech","Initialization Failed");
}
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH,null,TextToSpeech.ACTION_TTS_QUEUE_PROCESSING_COMPLETED);
}
覆盖TextToSpeech.OnUtteranceCompletedListener
方法。 完整的文档在这里。 当 TTS 服务完成合成话语时将调用的侦听器。
@Override
public void onUtteranceCompleted(String utteranceId) {
Log.i("CALLBACK", utteranceId);
}
希望能帮到你!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.