hotfix: resolve compatibility issues with Android Text-to-Speech API (#1394)

This commit is contained in:
Huang Xin 2025-06-14 21:39:12 +08:00 committed by GitHub
parent 89d48c72b0
commit 84328dcfb2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 51 additions and 30 deletions

View file

@ -1,3 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<queries>
<intent>
<action android:name="android.intent.action.TTS_SERVICE" />
</intent>
</queries>
</manifest>

View file

@ -3,6 +3,7 @@ package com.readest.native_tts
import android.os.Bundle
import android.app.Activity
import android.content.Context
import android.provider.Settings
import android.speech.tts.TextToSpeech
import android.speech.tts.UtteranceProgressListener
import android.speech.tts.Voice
@ -97,7 +98,11 @@ class NativeTTSPlugin(private val activity: Activity) : Plugin(activity) {
private suspend fun initializeTTS(): Boolean = suspendCancellableCoroutine { continuation ->
try {
textToSpeech = TextToSpeech(activity) { status ->
val preferredEngine = Settings.Secure.getString(
activity.contentResolver,
Settings.Secure.TTS_DEFAULT_SYNTH
)
textToSpeech = TextToSpeech(activity, { status ->
when (status) {
TextToSpeech.SUCCESS -> {
setupTTSListener()
@ -111,7 +116,7 @@ class NativeTTSPlugin(private val activity: Activity) : Plugin(activity) {
continuation.resume(false) {}
}
}
}
}, preferredEngine)
} catch (e: Exception) {
Log.e(TAG, "Exception during TTS initialization", e)
@OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)