I want to have two or more TTS in one activity. I have 4 textViews and want to be able to play each one seperatly.
I found a code and can play one, but can not play two. I copied the code and changed the variables but did not work.
Pretty basic in Android so any help is great, if this question has been asked before I apologize as I could not find an answer.
Please, Kotlin answers only. My code is shown below.
package com.example.testmisc1
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.speech.tts.TextToSpeech
import android.util.Log
import android.widget.Button
import android.widget.TextView
import java.util.*
class ListeningActivity : AppCompatActivity(),
TextToSpeech.OnInitListener {
private var tts: TextToSpeech? = null
private var btnSpeak: Button? = null
private var etSpeak: TextView? = null
private var btnSpeak2: Button? = null
private var etSpeak2: TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.listening)
// view binding button and edit text
btnSpeak = findViewById(R.id.btnText)
etSpeak = findViewById(R.id.textlike1)
btnSpeak2 = findViewById(R.id.btnText2)
etSpeak2 = findViewById(R.id.textlike2)
btnSpeak!!.isEnabled = false
// TextToSpeech(Context: this, OnInitListener: this)
tts = TextToSpeech(this, this)
btnSpeak!!.setOnClickListener { speakOut() }
}
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 {
btnSpeak!!.isEnabled = true
}
}
}
private fun speakOut() {
val text = etSpeak!!.text.toString()
tts!!.speak(text, TextToSpeech.QUEUE_FLUSH, null,"")
}
public override fun onDestroy() {
// Shutdown TTS when
// activity is destroyed
if (tts != null) {
tts!!.stop()
tts!!.shutdown()
}
super.onDestroy()
}
}
class ListeningActivity2 : AppCompatActivity(),
TextToSpeech.OnInitListener {
private var tts: TextToSpeech? = null
private var btnSpeak2: Button? = null
private var etSpeak2: TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.listening)
// view binding button and edit text
btnSpeak2 = findViewById(R.id.btnText2)
etSpeak2 = findViewById(R.id.textlike2)
btnSpeak2!!.isEnabled = false
// TextToSpeech(Context: this, OnInitListener: this)
tts = TextToSpeech(this, this)
btnSpeak2!!.setOnClickListener { speakOut1() }
}
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 {
btnSpeak2!!.isEnabled = true
}
}
}
private fun speakOut1() {
val text = etSpeak2!!.text.toString()
tts!!.speak(text, TextToSpeech.QUEUE_FLUSH, null,"")
}
public override fun onDestroy() {
// Shutdown TTS when
// activity is destroyed
if (tts != null) {
tts!!.stop()
tts!!.shutdown()
}
super.onDestroy()
}
}