当TTS(文本到语音)播放所有可用列表项时,我如何在recyclerView中显示/隐藏imageView,一一播放!
活动方法 -这种方法称为带有循环的r(不起作用,没有错误,但根本没有给出我的预期输出
int position=0;
public void convertTextToSpeech() {
Multiples multiples1=items.get(position);
for (Multiples item : items) {
text = item.first + " " + item.getSecond() + " Za " + item.getResult() + ".";
tts.speak(text, TextToSpeech.QUEUE_ADD, null);
boolean speakingEnd = tts.isSpeaking();
if (speakingEnd) {
Toast.makeText(getApplicationContext(), "Speaking...."+position, Toast.LENGTH_SHORT).show();
multiples1.setImage_show(true);
mAdapter.notifyItemChanged(position);
} else {
Toast.makeText(getApplicationContext(), "Done...."+position, Toast.LENGTH_SHORT).show();
}
position++;
}
}
下面的完整代码可提供更多理解,
首先,我在recyclerView中显示了所有项目。之后,我在具有for循环到TTS的方法中调用显示的项目来播放每行(列表项目)。我现在面临的问题是,由于TTS正在读取每个recyclerView项目,因此无法显示imageView。
期望的输出是每当TTS为每个行项目textView播放时,imageView(#image1)应该同时显示
DisplayActivityResultAdapter.java
......
.......
int position = 0;
public void convertTextToSpeech() {
Multiples multiples1=items.get(position);
for (Multiples item : items) {
text = item.first + " " + item.getSecond() + " Za " + item.getResult() + ".";
tts.speak(text, TextToSpeech.QUEUE_ADD, null);
boolean speakingEnd = tts.isSpeaking();
// Toast.makeText(getApplicationContext(), "Speaking...."+position, Toast.LENGTH_SHORT).show();
// multiples1.setImage_show(true);
// mAdapter.notifyItemChanged(position);
// Log.i("values","------------------------------------------Row value-"+item.getFirst()+" X "+item.getSecond()+", Position="+position);
//------------------------
MyAdapter m= (MyAdapter) mAdapter;
m.updateItem(item,position);
position++;
}
}
}
MyAdapter.java
.....
.....
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
if (holder instanceof MyAdapter.MyViewHolder) {
final MyAdapter.MyViewHolder view = (MyAdapter.MyViewHolder) holder;
Multiples p = items.get(position);
view.name.setText(p.first + " X " + p.getSecond() + "= "+p.getResult());
if(position>0) {
if (p.image_show) {
view.image1.setVisibility(View.VISIBLE);
view.image.setVisibility(View.INVISIBLE);
} else {
view.image1.setVisibility(View.INVISIBLE);
view.image.setVisibility(View.VISIBLE);
}
}
}
}
public void updateItem(Multiples newItem, int pos) {
Log.i("values","-----In updateItem Log----Row value-"+newItem.getFirst()+" X "+newItem.getSecond()+", Position="+pos);
items.set(pos, newItem); //update passed value in your adapter's data structure
Log.e("msg","-----items.get(pos)------------->"+items.get(pos).getFirst()+" X " +items.get(pos).getSecond());
notifyItemChanged(pos,newItem);
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return items.size();
}
}
答案 0 :(得分:4)
您的主要失败是没有TTS侦听器。没有它,您将不知道何时更新RecyclerView
。
侦听器如下所示:
class MyListener extends UtteranceProgressListener {
@Override
public void onStart(String utteranceId) {
int currentIndex = Integer.parseInt(utteranceId);
mMainAdapter.setCurrentPosition(currentIndex);
handler.post(new Runnable() {
@Override
public void run() {
mMainAdapter.notifyDataSetChanged();
}
});
}
@Override
public void onDone(String utteranceId) {
int currentIndex = Integer.parseInt(utteranceId);
mMainAdapter.setCurrentPosition(-1);
handler.post(new Runnable() {
@Override
public void run() {
mMainAdapter.notifyDataSetChanged();
}
});
if (currentIndex < data.size() - 1) {
playSound(currentIndex + 1);
}
}
@Override
public void onError(String utteranceId) {
}
}
答案 1 :(得分:-1)
将以下代码段放入SELECT classID,
ARRAY_TO_STRING(ARRAY_AGG(firstName || secondName || fathersName), ', ')
FROM Student, Class
WHERE studentGender = Male
GROUP BY classID
ORDER BY studentID;
中的Adapter
中
OnBindViewHolder
这对您有帮助吗?
答案 2 :(得分:-1)
请检查一下。在您的代码中,您错过了UtteranceProgressListener
。
您必须添加UtteranceProgressListener
,这将为您提供语音完成监听器事件。另外,您将需要发给anceanceID的utteranceID,以帮助您识别语音。
取一个变量
tts.speak()
然后在您的String utterId = "";
TextToSpeech
中调用onInit
,然后将此监听器注册到您的tts。
convertTextToSpeech
最初,将0传递给...
else{
tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String utteranceId) {
}
@Override
public void onDone(String utteranceId) {
convertTextToSpeech(position);
}
@Override
public void onError(String utteranceId) {
}
});
convertTextToSpeech(0);
}
,因为这是第一次调用。在这里,我对convertTextToSpeech
进行了一些更改。在此,我删除了您之前使用的循环,它将是一个递归函数。
convertTextToSpeech
如您所见, public void convertTextToSpeech(int pos) {
if (pos >= items.size()) return;
position = pos;
final Multiples multiples = items.get(position);
text = multiples.first + " " + multiples.getSecond() + " Za " + multiples.getResult() + ".";
utterId = (new Random().nextInt() % 9999999) + ""; // "" is String force
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Bundle params = new Bundle();
params.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utterId);
tts.speak(text, TextToSpeech.QUEUE_FLUSH, params, utterId);
} else {
HashMap<String, String> params = new HashMap<>();
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utterId);
tts.speak(text, TextToSpeech.QUEUE_FLUSH, params);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
MyAdapter m = (MyAdapter) mAdapter;
multiples.setImage_show(true);
m.updateItem(multiples, position);
position++;
}
});
}
已过时,您应该使用新版本,以避免最新的android操作系统出现问题。
在您的tts.speak()
中,您必须将上一个项目MyAdapter
设置为false。
image_show
检查上传的gif。请保持更新。
如果您需要任何帮助,可以询问。谢谢