gpt4 book ai didi

android - Android Text to Speech 说完后如何 toast

转载 作者:太空狗 更新时间:2023-10-29 16:00:25 25 4
gpt4 key购买 nike

我如何在 Text to Speech 完成讲话后 toast 。其实我想做的不仅仅是日志。这是我的代码。

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener, TextToSpeech.OnUtteranceCompletedListener {

private TextToSpeech mTts;
Button btnSpeak;
EditText editTextTTS;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mTts = new TextToSpeech(this,this);
editTextTTS =(EditText)findViewById(R.id.editText);
btnSpeak = (Button)findViewById(R.id.btnSpeakTest);
btnSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
speak(editTextTTS.getText().toString());
}
});




}
private void speak(String word){
if(word != null) {
HashMap<String, String> myHashAlarm = new HashMap<String, String>();
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_ALARM));
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "Hello");
mTts.speak(word, TextToSpeech.QUEUE_FLUSH, myHashAlarm);
}
}

@Override
public void onInit(int status) {
if(status == TextToSpeech.SUCCESS) {
mTts.setOnUtteranceCompletedListener(this);
}
}

@Override
public void onUtteranceCompleted(String utteranceId) {
Log.i("CALLBACK", utteranceId); //utteranceId == "SOME MESSAGE"
Toast.makeText(getApplicationContext(),"Call Back",Toast.LENGTH_LONG).show();// I Cannot Toast here. Or do something more than Log
}

其实我想在Text to Speech说完之后调用speech to text。如何在此方法中执行某些操作。

03-14 14:35:16.652 5473-5489/com.example.thummawit.testttscallback I/CALLBACK: Hello 03-14 14:35:16.667 5473-5489/com.example.thummawit.testttscallback W/Binder: Caught a RuntimeException from the binder stub implementation. java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() at android.os.Handler.(Handler.java:200) at android.os.Handler.(Handler.java:114) at android.widget.Toast$TN.(Toast.java:459) at android.widget.Toast.(Toast.java:120) at android.widget.Toast.makeText(Toast.java:289) at com.example.thummawit.testttscallback.MainActivity.onUtteranceCompleted(MainActivity.java:59) at android.speech.tts.UtteranceProgressListener$1.onDone(UtteranceProgressListener.java:73) at android.speech.tts.TextToSpeech$Connection$1.onSuccess(TextToSpeech.java:2158) at android.speech.tts.ITextToSpeechCallback$Stub.onTransact(ITextToSpeechCallback.java:63) at android.os.Binder.execTransact(Binder.java:446)

最佳答案

您尝试在非 UI(主)线程的线程中显示 Toast。你应该改变这个

@Override
public void onUtteranceCompleted(String utteranceId) {
Log.i("CALLBACK", utteranceId); //utteranceId == "SOME MESSAGE"
Toast.makeText(getApplicationContext(),"Call Back",Toast.LENGTH_LONG).show();// I Cannot Toast here. Or do something more than Log
}

进入这个

@Override
public void onUtteranceCompleted(String utteranceId) {
Log.i("CALLBACK", utteranceId); //utteranceId == "SOME MESSAGE"

runOnUiThread(new Runnable() {

public void run() {
Toast.makeText(getApplicationContext(),"Call Back",Toast.LENGTH_LONG).show();
}
});
}

这样你的代码就会被分派(dispatch)到主线程,你可以在主线程中显示Toast

关于android - Android Text to Speech 说完后如何 toast ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35982041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com