|
| 1 | +package com.kienhoang.dualsubreplay.ui |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.content.Intent |
| 5 | +import android.media.AudioAttributes |
| 6 | +import android.os.Bundle |
| 7 | +import android.os.Handler |
| 8 | +import android.os.Looper |
| 9 | +import android.provider.Settings |
| 10 | +import android.speech.tts.TextToSpeech |
| 11 | +import android.speech.tts.UtteranceProgressListener |
| 12 | +import kotlinx.coroutines.CompletableDeferred |
| 13 | +import java.util.Locale |
| 14 | + |
| 15 | +internal fun installedPronunciationEngines(context: Context): List<String?> { |
| 16 | + val preferred = Settings.Secure.getString(context.contentResolver, Settings.Secure.TTS_DEFAULT_SYNTH) |
| 17 | + val installed = |
| 18 | + context.packageManager |
| 19 | + .queryIntentServices(Intent(TextToSpeech.Engine.INTENT_ACTION_TTS_SERVICE), 0) |
| 20 | + .map { it.serviceInfo.packageName } |
| 21 | + return (listOf(preferred) + installed).distinct() |
| 22 | +} |
| 23 | + |
| 24 | +internal class AndroidPronunciationEngine( |
| 25 | + context: Context, |
| 26 | + engineName: String?, |
| 27 | +) : PronunciationEngine { |
| 28 | + private val handler = Handler(Looper.getMainLooper()) |
| 29 | + private val initialized = CompletableDeferred<Boolean>() |
| 30 | + private var closed = false |
| 31 | + private var nextUtterance = 0L |
| 32 | + private var utteranceId: String? = null |
| 33 | + private var playback: CompletableDeferred<Boolean>? = null |
| 34 | + private val tts = |
| 35 | + TextToSpeech(context.applicationContext, { status -> |
| 36 | + // onInit may run before the constructor returns. Post before touching tts or UI state. |
| 37 | + handler.post { if (!closed) initialized.complete(status == TextToSpeech.SUCCESS) } |
| 38 | + }, engineName) |
| 39 | + |
| 40 | + override suspend fun initialize(): Boolean { |
| 41 | + if (!initialized.await()) return false |
| 42 | + tts.setAudioAttributes( |
| 43 | + AudioAttributes |
| 44 | + .Builder() |
| 45 | + .setUsage(AudioAttributes.USAGE_MEDIA) |
| 46 | + .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH) |
| 47 | + .build(), |
| 48 | + ) |
| 49 | + tts.setOnUtteranceProgressListener( |
| 50 | + object : UtteranceProgressListener() { |
| 51 | + override fun onStart(id: String?) = Unit |
| 52 | + |
| 53 | + override fun onDone(id: String?) = complete(id, true) |
| 54 | + |
| 55 | + @Deprecated("Required by UtteranceProgressListener") |
| 56 | + override fun onError(id: String?) = complete(id, false) |
| 57 | + |
| 58 | + override fun onError( |
| 59 | + id: String?, |
| 60 | + errorCode: Int, |
| 61 | + ) = complete(id, false) |
| 62 | + |
| 63 | + override fun onStop( |
| 64 | + id: String?, |
| 65 | + interrupted: Boolean, |
| 66 | + ) = complete(id, false) |
| 67 | + }, |
| 68 | + ) |
| 69 | + return true |
| 70 | + } |
| 71 | + |
| 72 | + private fun complete( |
| 73 | + id: String?, |
| 74 | + success: Boolean, |
| 75 | + ) { |
| 76 | + handler.post { |
| 77 | + if (!closed && id == utteranceId) playback?.complete(success) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + override fun voices(): List<PronunciationVoice> = |
| 82 | + tts.voices.orEmpty().map { |
| 83 | + PronunciationVoice( |
| 84 | + it.name, |
| 85 | + it.locale, |
| 86 | + it.isNetworkConnectionRequired, |
| 87 | + TextToSpeech.Engine.KEY_FEATURE_NOT_INSTALLED !in it.features.orEmpty(), |
| 88 | + ) |
| 89 | + } |
| 90 | + |
| 91 | + override fun selectVoice(voice: PronunciationVoice): Boolean { |
| 92 | + val native = tts.voices.orEmpty().firstOrNull { it.name == voice.name } ?: return false |
| 93 | + return tts.setVoice(native) == TextToSpeech.SUCCESS |
| 94 | + } |
| 95 | + |
| 96 | + override fun selectLanguage(locale: Locale): Boolean = tts.setLanguage(locale) >= TextToSpeech.LANG_AVAILABLE |
| 97 | + |
| 98 | + override suspend fun speak(word: String): Boolean { |
| 99 | + val result = CompletableDeferred<Boolean>() |
| 100 | + playback = result |
| 101 | + val id = "word-${++nextUtterance}" |
| 102 | + utteranceId = id |
| 103 | + val parameters = Bundle().apply { putFloat(TextToSpeech.Engine.KEY_PARAM_VOLUME, 1f) } |
| 104 | + return try { |
| 105 | + if (tts.speak(word, TextToSpeech.QUEUE_FLUSH, parameters, id) == TextToSpeech.ERROR) false else result.await() |
| 106 | + } finally { |
| 107 | + utteranceId = null |
| 108 | + playback = null |
| 109 | + tts.stop() |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + override fun close() { |
| 114 | + closed = true |
| 115 | + utteranceId = null |
| 116 | + playback?.cancel() |
| 117 | + initialized.cancel() |
| 118 | + handler.removeCallbacksAndMessages(null) |
| 119 | + try { |
| 120 | + tts.stop() |
| 121 | + } finally { |
| 122 | + tts.shutdown() |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments