gpt4 book ai didi

android - 尝试在 setOnUtteranceProgressListener 中调用 Toaster 或自定义函数

转载 作者:搜寻专家 更新时间:2023-11-01 09:36:14 27 4
gpt4 key购买 nike

在我的主 Fragment 中,我实现了 TextToSpeech mainFragment extends Fragment implements TextToSpeech.OnInitListener

文本转语音工作正常,我还添加了一个正在工作的 UtteranceProgressListener

我的问题:我无法调用任何自定义函数,例如 gotoNextChapter() 甚至是简单的 Toaster。出现错误 Can't create handler inside thread that has not called Looper.prepare()

欢迎提出任何建议或解决方案....

我的代码 fragment :

UtteranceProgressListener SpeechListener = new UtteranceProgressListener() {
@Override
public void onStart(String utteranceId) {
Toast.makeText(getActivity(),utteranceId, Toast.LENGTH_LONG).show();
}

@Override
public void onDone(String utteranceId) {
Toast.makeText(getActivity(),utteranceId, Toast.LENGTH_LONG).show();

gotoNextChapter(); <====function I want to call
read_mode = 0;
}

@Override
public void onError(String utteranceId) {
Toast.makeText(getActivity(),utteranceId, Toast.LENGTH_LONG).show();
}
};

设置话语完整的监听器:

myTTS.setOnUtteranceProgressListener(SpeechListener);

获取错误:

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.<init>(Handler.java:209)
at
android.os.Handler.<init>(Handler.java:123)
at
android.widget.Toast$TN.<init>(Toast.java:350)
at
android.widget.Toast.<init>(Toast.java:106)
at
android.widget.Toast.makeText(Toast.java:264)
at com.MainActivity$Read$10.onDone(MainActivity.java:1252)

最佳答案

好的,我有一个解决方案,现在我的问题看起来微不足道了

我更新后的代码如下所示:

@Override
public void onDone(String utteranceId) {

runOnUiThread(new Runnable() {

public void run() {
Toast.makeText(getContext(),utteranceId,Toast.LENGTH_LONG).show();
gotoNextChapter(); <====function I want to call
read_mode = 0;
}
});
}

在此感谢这些家伙: How can I Toast after Text to Speech finish speaking AndroidWhen may we need to use runOnUiThread in android application?

关于android - 尝试在 setOnUtteranceProgressListener 中调用 Toaster 或自定义函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43017607/

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