mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-08 06:42:21 -04:00
Merge pull request #3427 from kiwix/Issue#3408
Fixes of Text to Speech init failed
This commit is contained in:
commit
c29f40e128
@ -18,6 +18,13 @@
|
||||
|
||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
||||
|
||||
<queries>
|
||||
<intent>
|
||||
<action android:name="android.intent.action.TTS_SERVICE" />
|
||||
</intent>
|
||||
</queries>
|
||||
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:fullBackupContent="@xml/backup_rules"
|
||||
|
@ -326,6 +326,7 @@ abstract class CoreReaderFragment :
|
||||
private lateinit var serviceConnection: ServiceConnection
|
||||
private var readAloudService: ReadAloudService? = null
|
||||
private var navigationHistoryList: MutableList<NavigationHistoryListItem> = ArrayList()
|
||||
private var isReadSelection = false
|
||||
|
||||
private var storagePermissionForNotesLauncher: ActivityResultLauncher<String>? =
|
||||
registerForActivityResult(
|
||||
@ -366,7 +367,12 @@ abstract class CoreReaderFragment :
|
||||
|
||||
protected open fun configureWebViewSelectionHandler(menu: Menu?) {
|
||||
menu?.findItem(R.id.menu_speak_text)?.setOnMenuItemClickListener {
|
||||
getCurrentWebView()?.let { currentWebView -> tts?.readSelection(currentWebView) }
|
||||
if (tts?.isInitialized == false) {
|
||||
isReadSelection = true
|
||||
tts?.initializeTTS()
|
||||
} else {
|
||||
startReadSelection()
|
||||
}
|
||||
actionMode?.finish()
|
||||
true
|
||||
}
|
||||
@ -853,7 +859,11 @@ abstract class CoreReaderFragment :
|
||||
requireActivity(),
|
||||
object : OnInitSucceedListener {
|
||||
override fun onInitSucceed() {
|
||||
// do nothing it's default override method
|
||||
if (isReadSelection) {
|
||||
startReadSelection()
|
||||
} else {
|
||||
startReadAloud()
|
||||
}
|
||||
}
|
||||
},
|
||||
object : OnSpeakingListener {
|
||||
@ -900,6 +910,18 @@ abstract class CoreReaderFragment :
|
||||
}
|
||||
}
|
||||
|
||||
private fun startReadAloud() {
|
||||
getCurrentWebView()?.let {
|
||||
tts?.readAloud(it)
|
||||
}
|
||||
}
|
||||
|
||||
private fun startReadSelection() {
|
||||
getCurrentWebView()?.let {
|
||||
tts?.readSelection(it)
|
||||
}
|
||||
}
|
||||
|
||||
@OnClick(R2.id.activity_main_button_pause_tts)
|
||||
fun pauseTts() {
|
||||
if (tts?.currentTTSTask == null) {
|
||||
@ -1130,7 +1152,12 @@ abstract class CoreReaderFragment :
|
||||
if (isBackToTopEnabled) {
|
||||
backToTopButton?.hide()
|
||||
}
|
||||
getCurrentWebView()?.let { tts?.readAloud(it) }
|
||||
if (tts?.isInitialized == false) {
|
||||
isReadSelection = false
|
||||
tts?.initializeTTS()
|
||||
} else {
|
||||
startReadAloud()
|
||||
}
|
||||
}
|
||||
View.VISIBLE -> {
|
||||
if (isBackToTopEnabled) {
|
||||
|
@ -28,7 +28,6 @@ import android.speech.tts.TextToSpeech
|
||||
import android.speech.tts.TextToSpeech.Engine
|
||||
import android.speech.tts.TextToSpeech.LANG_MISSING_DATA
|
||||
import android.speech.tts.TextToSpeech.LANG_NOT_SUPPORTED
|
||||
import android.speech.tts.TextToSpeech.OnInitListener
|
||||
import android.speech.tts.TextToSpeech.QUEUE_ADD
|
||||
import android.speech.tts.TextToSpeech.SUCCESS
|
||||
import android.speech.tts.UtteranceProgressListener
|
||||
@ -36,7 +35,6 @@ import android.util.Log
|
||||
import android.webkit.JavascriptInterface
|
||||
import android.webkit.WebView
|
||||
import android.widget.Toast
|
||||
import org.kiwix.kiwixmobile.core.CoreApp.Companion.instance
|
||||
import org.kiwix.kiwixmobile.core.R
|
||||
import org.kiwix.kiwixmobile.core.extensions.toast
|
||||
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer
|
||||
@ -54,7 +52,7 @@ import java.util.concurrent.atomic.AtomicInteger
|
||||
*/
|
||||
class KiwixTextToSpeech internal constructor(
|
||||
val context: Context,
|
||||
onInitSucceedListener: OnInitSucceedListener,
|
||||
private val onInitSucceedListener: OnInitSucceedListener,
|
||||
val onSpeakingListener: OnSpeakingListener,
|
||||
private var onAudioFocusChangeListener: OnAudioFocusChangeListener? = null,
|
||||
private val zimReaderContainer: ZimReaderContainer
|
||||
@ -63,26 +61,35 @@ class KiwixTextToSpeech internal constructor(
|
||||
private val focusLock: Any = Any()
|
||||
private val am: AudioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
|
||||
@JvmField var currentTTSTask: TTSTask? = null
|
||||
private val tts: TextToSpeech = TextToSpeech(
|
||||
instance,
|
||||
OnInitListener { status: Int ->
|
||||
if (status == SUCCESS) {
|
||||
private lateinit var tts: TextToSpeech
|
||||
|
||||
/**
|
||||
* Initializes the TextToSpeech object.
|
||||
*/
|
||||
fun initializeTTS() {
|
||||
tts = TextToSpeech(
|
||||
context
|
||||
) { status: Int ->
|
||||
if (status == TextToSpeech.SUCCESS) {
|
||||
Log.d(TAG_KIWIX, "TextToSpeech was initialized successfully.")
|
||||
this.isInitialized = true
|
||||
onInitSucceedListener.onInitSucceed()
|
||||
} else {
|
||||
Log.e(TAG_KIWIX, "Initialization of TextToSpeech Failed!")
|
||||
context.toast(R.string.texttospeech_initialization_failed, Toast.LENGTH_SHORT)
|
||||
context.toast(
|
||||
R.string.texttospeech_initialization_failed,
|
||||
Toast.LENGTH_SHORT
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the TTS is initialized.
|
||||
*
|
||||
* @return `true` if TTS is initialized; `false` otherwise
|
||||
*/
|
||||
private var isInitialized = false
|
||||
var isInitialized = false
|
||||
|
||||
init {
|
||||
Log.d(TAG_KIWIX, "Initializing TextToSpeech")
|
||||
@ -214,7 +221,9 @@ class KiwixTextToSpeech internal constructor(
|
||||
* {@link https://developer.android.com/guide/topics/media-apps/audio-focus#audio-focus-change }
|
||||
*/
|
||||
fun shutdown() {
|
||||
tts.shutdown()
|
||||
if (::tts.isInitialized) {
|
||||
tts.shutdown()
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
focusRequest?.let(am::abandonAudioFocusRequest)
|
||||
focusRequest = null
|
||||
|
Loading…
x
Reference in New Issue
Block a user