mirror of
https://github.com/gedoor/legado.git
synced 2024-07-06 23:47:49 +08:00
优化
This commit is contained in:
parent
20a88174eb
commit
197360b10d
@ -14,7 +14,11 @@ import io.legado.app.help.coroutine.Coroutine
|
|||||||
import io.legado.app.lib.dialogs.SelectItem
|
import io.legado.app.lib.dialogs.SelectItem
|
||||||
import io.legado.app.model.ReadAloud
|
import io.legado.app.model.ReadAloud
|
||||||
import io.legado.app.model.ReadBook
|
import io.legado.app.model.ReadBook
|
||||||
import io.legado.app.utils.*
|
import io.legado.app.utils.GSON
|
||||||
|
import io.legado.app.utils.LogUtils
|
||||||
|
import io.legado.app.utils.fromJsonObject
|
||||||
|
import io.legado.app.utils.servicePendingIntent
|
||||||
|
import io.legado.app.utils.toastOnUi
|
||||||
import kotlinx.coroutines.ensureActive
|
import kotlinx.coroutines.ensureActive
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -26,6 +30,7 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
|
|||||||
private var ttsInitFinish = false
|
private var ttsInitFinish = false
|
||||||
private val ttsUtteranceListener = TTSUtteranceListener()
|
private val ttsUtteranceListener = TTSUtteranceListener()
|
||||||
private var speakJob: Coroutine<*>? = null
|
private var speakJob: Coroutine<*>? = null
|
||||||
|
private val TAG = "TTSReadAloudService"
|
||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
@ -41,6 +46,7 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
|
|||||||
private fun initTts() {
|
private fun initTts() {
|
||||||
ttsInitFinish = false
|
ttsInitFinish = false
|
||||||
val engine = GSON.fromJsonObject<SelectItem<String>>(ReadAloud.ttsEngine).getOrNull()?.value
|
val engine = GSON.fromJsonObject<SelectItem<String>>(ReadAloud.ttsEngine).getOrNull()?.value
|
||||||
|
LogUtils.d(TAG, "initTts engine:$engine")
|
||||||
textToSpeech = if (engine.isNullOrBlank()) {
|
textToSpeech = if (engine.isNullOrBlank()) {
|
||||||
TextToSpeech(this, this)
|
TextToSpeech(this, this)
|
||||||
} else {
|
} else {
|
||||||
@ -84,6 +90,8 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
|
|||||||
MediaHelp.playSilentSound(this@TTSReadAloudService)
|
MediaHelp.playSilentSound(this@TTSReadAloudService)
|
||||||
speakJob?.cancel()
|
speakJob?.cancel()
|
||||||
speakJob = execute {
|
speakJob = execute {
|
||||||
|
LogUtils.d(TAG, "朗读列表大小 ${contentList.size}")
|
||||||
|
LogUtils.d(TAG, "朗读页数 ${textChapter?.pageSize}")
|
||||||
val tts = textToSpeech ?: throw NoStackTraceException("tts is null")
|
val tts = textToSpeech ?: throw NoStackTraceException("tts is null")
|
||||||
var result = tts.runCatching {
|
var result = tts.runCatching {
|
||||||
speak("", TextToSpeech.QUEUE_FLUSH, null, null)
|
speak("", TextToSpeech.QUEUE_FLUSH, null, null)
|
||||||
@ -92,6 +100,7 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
|
|||||||
TextToSpeech.ERROR
|
TextToSpeech.ERROR
|
||||||
}
|
}
|
||||||
if (result == TextToSpeech.ERROR) {
|
if (result == TextToSpeech.ERROR) {
|
||||||
|
AppLog.put("tts出错 尝试重新初始化")
|
||||||
clearTTS()
|
clearTTS()
|
||||||
initTts()
|
initTts()
|
||||||
return@execute
|
return@execute
|
||||||
@ -116,6 +125,7 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
|
|||||||
AppLog.put("tts朗读出错:$text")
|
AppLog.put("tts朗读出错:$text")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
LogUtils.d(TAG, "朗读内容添加完成")
|
||||||
}.onError {
|
}.onError {
|
||||||
AppLog.put("tts朗读出错\n${it.localizedMessage}", it, true)
|
AppLog.put("tts朗读出错\n${it.localizedMessage}", it, true)
|
||||||
}
|
}
|
||||||
@ -166,7 +176,10 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
|
|||||||
*/
|
*/
|
||||||
private inner class TTSUtteranceListener : UtteranceProgressListener() {
|
private inner class TTSUtteranceListener : UtteranceProgressListener() {
|
||||||
|
|
||||||
|
private val TAG = "TTSUtteranceListener"
|
||||||
|
|
||||||
override fun onStart(s: String) {
|
override fun onStart(s: String) {
|
||||||
|
LogUtils.d(TAG, "onStart nowSpeak:$nowSpeak pageIndex:$pageIndex utteranceId:$s")
|
||||||
textChapter?.let {
|
textChapter?.let {
|
||||||
if (readAloudNumber + 1 > it.getReadLength(pageIndex + 1)) {
|
if (readAloudNumber + 1 > it.getReadLength(pageIndex + 1)) {
|
||||||
pageIndex++
|
pageIndex++
|
||||||
@ -177,6 +190,7 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onDone(s: String) {
|
override fun onDone(s: String) {
|
||||||
|
LogUtils.d(TAG, "onDone utteranceId:$s")
|
||||||
//跳过全标点段落
|
//跳过全标点段落
|
||||||
do {
|
do {
|
||||||
readAloudNumber += contentList[nowSpeak].length + 1 - paragraphStartPos
|
readAloudNumber += contentList[nowSpeak].length + 1 - paragraphStartPos
|
||||||
@ -191,6 +205,9 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
|
|||||||
|
|
||||||
override fun onRangeStart(utteranceId: String?, start: Int, end: Int, frame: Int) {
|
override fun onRangeStart(utteranceId: String?, start: Int, end: Int, frame: Int) {
|
||||||
super.onRangeStart(utteranceId, start, end, frame)
|
super.onRangeStart(utteranceId, start, end, frame)
|
||||||
|
val msg =
|
||||||
|
"$TAG onRangeStart nowSpeak:$nowSpeak pageIndex:$pageIndex utteranceId:$utteranceId start:$start end:$end frame:$frame"
|
||||||
|
LogUtils.d(TAG, msg)
|
||||||
textChapter?.let {
|
textChapter?.let {
|
||||||
if (readAloudNumber + start > it.getReadLength(pageIndex + 1)) {
|
if (readAloudNumber + start > it.getReadLength(pageIndex + 1)) {
|
||||||
pageIndex++
|
pageIndex++
|
||||||
@ -200,6 +217,13 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onError(utteranceId: String?, errorCode: Int) {
|
||||||
|
LogUtils.d(
|
||||||
|
TAG,
|
||||||
|
"onError nowSpeak:$nowSpeak pageIndex:$pageIndex utteranceId:$utteranceId errorCode:$errorCode"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
@Deprecated("Deprecated in Java")
|
@Deprecated("Deprecated in Java")
|
||||||
override fun onError(s: String) {
|
override fun onError(s: String) {
|
||||||
//nothing
|
//nothing
|
||||||
|
Loading…
Reference in New Issue
Block a user