Код: Выделить всё
releaseOfTtsResources
Код: Выделить всё
toSpeak
import android.content.Context
import android.provider.Settings
import android.speech.tts.TextToSpeech
import java.util.Locale
import android.util.Log
class TextToSpeechHelper(context: Context) : TextToSpeech.OnInitListener {
private var tts: TextToSpeech? = null
private var pendingText: String? = null
//private val ttsCreated: Boolean = false
init {
// initialize the synthesizer
Log.d(TTSTAG, "initialize the synthesizer")
//val tempTTS = TextToSpeech(context, null)
val userTTS = context.getSystemService(TextToSpeech::class.java)?.defaultEngine ?: "com.google.android.tts" //TextToSpeech.Engine.DEFAULT_ENGINE
//val userTTS = Settings.Secure.getString(context.contentResolver, Settings.Secure.TTS_DEFAULT_SYNTH) ?: "com.google.android.tts"
Log.d(TTSTAG, "Motor TTS select: $userTTS")
tts = TextToSpeech(context, this, userTTS)
}
override fun onInit(status: Int) {
//Set preferred language default is system language
Log.d(TTSTAG, "Set preferred language default is system language")
if (status == TextToSpeech.SUCCESS){
Log.d(TTSTAG, "TTS initialized successfully")
tts?.language = Locale.getDefault()
pendingText?.let { toSpeak(it) }
pendingText = null
} else{
Log.e(TTSTAG, "TTS initialization failed")
}
}
// Function to speak the text received as a parameter
fun toSpeak(text: String){
Log.d(TTSTAG, "the text is sent to the synthesizer")
if (tts?.language == null){
Log.d(TTSTAG, "TTS not initialized yet, storing text for later")
pendingText = text
} else{
tts?.speak(text, TextToSpeech.QUEUE_FLUSH, null, null)
}
}
// Release TTS resources (called in activity to avoid memory leaks)
fun releaseOfTtsResources(){
Log.d(TTSTAG, "Release TTS resources")
tts?.stop()
tts?.shutdown()
tts = null
}
companion object{
const val TTSTAG = "TextToSpeechHelper"
}
}
< /code>
Проблема, с которой я сталкиваюсь, заключается в том, что моя функция всегда использует синтезатор по умолчанию системы вместо того, что выбрано пользователем. < /p>
Например, если Пользователь установил Smart Voice на свой телефон и установил его в качестве предпочтительного двигателя TTS, моя функция по -прежнему использует синтезатор по умолчанию системы (например, Google TTS или Samsung TTS). < /p>
Как я могу Убедитесь, что моя функция использует отобранный пользователь двигатель TTS вместо по умолчанию системы? Любое руководство или примеры будут высоко оценены. И то, что я нашел, уже устарело, потому что он использовался в старых версиях Android. Вот почему мне нужна помощь, пожалуйста.
Подробнее здесь: https://stackoverflow.com/questions/794 ... -of-the-tt