I am trying to use TextToSpeech in an Activity.
And I also want to move this to a seperate class file called Helper, and load it from there.
So I created the code below.
// Helper.kt
class Helper {
private var tts: TextToSpeech? = null
fun initTextToSpeech(context: Context) {
// 롤리팝(API 21, Android 5.0)이상에서만 지원됨
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Toast.makeText(context, "지원하지 않는 SDK 버전입니다.", Toast.LENGTH_SHORT).show()
return
}
tts = TextToSpeech(context) {
if (it == TextToSpeech.SUCCESS) {
val result = tts?.setLanguage(Locale.KOREAN)
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(context, "지원하지 않는 언어입니다.", Toast.LENGTH_SHORT).show()
// return@TextToSpeech
}
Toast.makeText(context, "TTS 설정 성공", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(context, "TTS 설정 실패", Toast.LENGTH_SHORT).show()
}
}
}
fun ttsSpeak(str: String) {
tts?.speak(str, TextToSpeech.QUEUE_FLUSH, null, "")
}
}
I called it like this in the activity as below, but it didn’t work.
There is no error, but it does not work either.
class QuizQuestionsActivity : AppCompatActivity(), View.OnClickListener {
private var btnAudio: ImageButton? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_quiz_questions)
Helper().initTextToSpeech(this)
btnAudio = findViewById(R.id.btn_audio)
btnAudio?.setOnClickListener(this)
}
override fun onClick(view: View?) {
// 클릭된 view 요소에 따라 메서드 실행
when (view?.id) {
R.id.btn_audio -> {
val question = mQuestionList?.get(mCurrentPosition - 1)
question?.let { Helper().ttsSpeak(question.question) }
}
}
}
}
It works well if I include it in the Activity itself as below.
(* Please note that some parts that are not directly related have been omitted.)
// QuizQuestionsActivity.kt
class QuizQuestionsActivity : AppCompatActivity(), View.OnClickListener {
private var btnAudio: ImageButton? = null
var tts: TextToSpeech? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_quiz_questions)
initTextToSpeech()
btnAudio = findViewById(R.id.btn_audio)
btnAudio?.setOnClickListener(this)
}
fun initTextToSpeech() {
// 롤리팝(API 21, Android 5.0)이상에서만 지원됨
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Toast.makeText(this, "지원하지 않는 SDK 버전입니다.", Toast.LENGTH_SHORT).show()
return
}
tts = TextToSpeech(this) {
if (it == TextToSpeech.SUCCESS) {
val result = tts?.setLanguage(Locale.KOREAN)
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(this, "지원하지 않는 언어입니다.", Toast.LENGTH_SHORT).show()
// return@TextToSpeech
}
Toast.makeText(this, "TTS 설정 성공", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "TTS 설정 실패", Toast.LENGTH_SHORT).show()
}
}
}
fun ttsSpeak(str: String) {
tts?.speak(str, TextToSpeech.QUEUE_ADD, null, "")
}
override fun onClick(view: View?) {
// 클릭된 view 요소에 따라 메서드 실행
when (view?.id) {
R.id.btn_audio -> {
val question = mQuestionList?.get(mCurrentPosition - 1)
question?.let { ttsSpeak(question.question) }
}
}
}
}
I’m still new to Kotlin, so I don’t know the cause.
Why the first one doesn’t work and how can I make it work?
I don’t understand because actually there are other methods in the Helper class and they works fine in the activity.