如何在使用 Recycler View 时实现 Text To Speech 中的错误处理 [英] How to Implement Error handling in Text To Speech while working with Recycler View
问题描述
我正在将 TextToSpeech
从 Fragment 传递到我的 RecyclerView 适配器.
在传递它的同时,我还发送了一个标志 textToSpeechSupported
以确认当前设置的设备语言是否支持 TextToSpeech
公告.
I am passing TextToSpeech
from Fragment to my RecyclerView Adapter.
While passing it, I am also sending a flag textToSpeechSupported
to confirm whether the currently set device language is supported for TextToSpeech
announcement or not.
但每次将此标志的值设置为 false
时,即使我在 onCreate
中将该值设置为 true
.
我的实现方法似乎有问题.
But every time the value of this flag is being set as false
, even though I am setting the value to true
in onCreate
.
It seems there is an issue with my implementation approach.
但是我调试累了,还加了日志.
我也做了多次测试并尝试了各种其他组合.但是每次传递标志 textToSpeechSupported
值都是 false
,即使语言受支持.
我是不是在这里遗漏了什么.
But I tired to debug and also added Logs.
Also I did multiple test and tried various other combinations. But every time the flag textToSpeechSupported
value is being passed is false
, even if the language is supported.
Am I missing something here.
如果 TextToSpeech
请指导.
class WelcomeFragment : Fragment() {
private lateinit var welcomeAdapter: WelcomeAdapter
private lateinit var textToSpeech: TextToSpeech
private var textToSpeechSupported: Boolean = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
textToSpeech = TextToSpeech(requireContext()) { status ->
if (status == SUCCESS) {
val result = textToSpeech.setLanguage(Locale.getDefault())
textToSpeechSupported =
!(result == TextToSpeech.LANG_NOT_SUPPORTED || result == TextToSpeech.LANG_MISSING_DATA)
}
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
welcomeAdapter = WelcomeAdapter(textToSpeech, textToSpeechSupported)
binding.adapter = welcomeAdapter
}
}
class Welcomedapter(private val textToSpeech: TextToSpeech, private val textToSpeechSupported: Boolean) : ListAdapter<Welcome, ViewHolder>(WelcomeDiffCallback()) {
//....
class ViewHolder private constructor(val binding: ContainerWelcomeBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(item: Welcome, textToSpeech: TextToSpeech, textToSpeechSupported: Boolean) {
binding.apply {
welcomeMessageText.text = item.welcome
textToSpeechImage.setOnClickListener {
if (textToSpeechSupported) {
textToSpeech.speak(item.welcome, TextToSpeech.QUEUE_FLUSH, null)
} else {
// Send an event for Toast saying that language is not supported for Text to Speech
}
}
}
}
}
}
目的/目标是确保标记 textToSpeechSupported
的值被正确计算并传递给 Recycler View Adapter.
The objective/goal is to ensure that value of flag textToSpeechSupported
is correctly calculated and passed to Recycler View Adapter.
推荐答案
问题是 onViewCreated 在 tts 有时间初始化之前被调用,所以你太早访问 textToSpeechSupported 并且你总是得到你的默认值(false) 值.
The problem is that onViewCreated is being called BEFORE the tts has had time to initialize, so you are accessing textToSpeechSupported too early and you're always getting your default (false) value.
所以,而不是调用:
welcomeAdapter = WelcomeAdapter(textToSpeech, textToSpeechSupported)
binding.adapter = welcomeAdapter
从 onViewCreated 内部创建一个新函数:
from inside onViewCreated, make a new function:
fun thisFunctionRunsAFTERtheTTSisInitialized() {
// put that code here instead.
}
然后从您的 onCreate 内部调用该函数,以便它在 tts 初始化后运行:
And then call that function from inside your onCreate so it will run AFTER the tts has initialized:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
textToSpeech = TextToSpeech(requireContext()) { status ->
if (status == SUCCESS) {
val result = textToSpeech.setLanguage(Locale.getDefault())
textToSpeechSupported =
!(result == TextToSpeech.LANG_NOT_SUPPORTED || result == TextToSpeech.LANG_MISSING_DATA)
thisFunctionRunsAFTERtheTTSisInitialized() // <---------------
}
}
}
这篇关于如何在使用 Recycler View 时实现 Text To Speech 中的错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!