[英]textToSpeech - unresolved reference in Kotlin
I have been trying to implement textToSpeech on a microphone that you can press on, but the textToSpeech turns red and "unresolved reference" is displayed.我一直在尝试在您可以按下的麦克风上实现 textToSpeech,但 textToSpeech 变为红色并显示“未解析的参考”。
Here is the code from the Main Activity:以下是主要活动的代码:
vh.miketv.setOnClickListener {
val text: String =
celldata!![position].getText_eng().toString() + " " + celldata[position].getText_spn()
textToSpeech.speak(text, QUEUE_FLUSH, null)
}
return view
Also, here is everything that I have imported:另外,这是我导入的所有内容:
import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import android.speech.tts.TextToSpeech
import android.speech.tts.TextToSpeech.*
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.GridView
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.cardview.widget.CardView
import main.com.cellappkotlin.model.CellDataBean
import java.util.*
I have also tried in Java, and it worked with the following code, but I can't figure it out how to make it work in Kotlin as well:我也在 Java 中尝试过,它使用以下代码,但我不知道如何使它在 Kotlin 中也能工作:
public void onClick(View view) {
String text = celldata.get(i).getText_eng()+" "+celldata.get(i).getText_spn();
textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH,null);
}
The text to speech works generally in this app, I have implemented it somewhere else and everything was okay, but only in this particular case, when I want to press on that microphone, it doesn't seem to work.文本到语音通常在这个应用程序中工作,我已经在其他地方实现了它,一切都很好,但只有在这种特殊情况下,当我想按下那个麦克风时,它似乎不起作用。
Could someone help me understand what is wrong, please?有人可以帮我理解什么是错的吗? Thank you very much.
非常感谢。
Step 1: Extend your activity with TextToSpeech.OnInitListener.第 1 步:使用 TextToSpeech.OnInitListener 扩展您的活动。
class MainActivity : AppCompatActivity(),TextToSpeech.OnInitListener {
private var tts: TextToSpeech? = null
Step 2: Set Language第 2 步:设置语言
override fun onInit(status: Int) {
if (status == TextToSpeech.SUCCESS) {
val result = tts!!.setLanguage(Locale.US)
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS","The Language not supported!")
} else {
//Do something such as enable the button
}
}
}
Step 3: Initialise TextToSpeech class variable in onCreate.第 3 步:在 onCreate 中初始化 TextToSpeech class 变量。
tts = TextToSpeech(this, this)
Step 4: speak the text when you click it.第 4 步:单击文本时说出文本。
vh.miketv.setOnClickListener {
val text: String = "Hello"
tts!!.speak(text, TextToSpeech.QUEUE_FLUSH, null,"")
}
return view
Step 5: Stop TextToSpeech Engine when your activity is destroyed.第 5 步:当您的活动被销毁时停止 TextToSpeech 引擎。
public override fun onDestroy() {
// Shutdown TTS
if (tts != null) {
tts!!.stop()
tts!!.shutdown()
}
super.onDestroy()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.