This commit is contained in:
Horis
2025-12-19 09:51:39 +08:00
parent 51f7198ea5
commit f8224b0637
@@ -36,6 +36,7 @@ import io.legado.app.help.http.okHttpClient
import io.legado.app.model.ReadAloud
import io.legado.app.model.ReadBook
import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.ui.book.read.page.entities.TextChapter
import io.legado.app.utils.FileUtils
import io.legado.app.utils.MD5Utils
import io.legado.app.utils.printOnDebug
@@ -45,6 +46,7 @@ import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.delay
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.launch
@@ -58,7 +60,6 @@ import java.io.File
import java.io.InputStream
import java.net.ConnectException
import java.net.SocketTimeoutException
import kotlin.coroutines.coroutineContext
/**
* 在线朗读
@@ -196,8 +197,8 @@ class HttpReadAloudService : BaseReadAloudService(),
.take(10)
.toList()
contentList.forEach { content ->
coroutineContext.ensureActive()
val fileName = md5SpeakFileName(content)
currentCoroutineContext().ensureActive()
val fileName = md5SpeakFileName(content, textChapter)
val speakText = content.replace(AppPattern.notReadAloudRegex, "")
if (speakText.isEmpty()) {
createSilentSound(fileName)
@@ -265,8 +266,8 @@ class HttpReadAloudService : BaseReadAloudService(),
.take(10)
.toList()
contentList.forEach { content ->
coroutineContext.ensureActive()
val fileName = md5SpeakFileName(content)
currentCoroutineContext().ensureActive()
val fileName = md5SpeakFileName(content, textChapter)
val speakText = content.replace(AppPattern.notReadAloudRegex, "")
val dataSourceFactory = createDataSourceFactory(httpTts, speakText)
val downloader = createDownloader(dataSourceFactory, fileName)
@@ -331,10 +332,10 @@ class HttpReadAloudService : BaseReadAloudService(),
speakSpeed = speechRate,
source = httpTts,
readTimeout = 300 * 1000L,
coroutineContext = coroutineContext
coroutineContext = currentCoroutineContext()
)
var response = analyzeUrl.getResponseAwait()
coroutineContext.ensureActive()
currentCoroutineContext().ensureActive()
val checkJs = httpTts.loginCheckJs
if (checkJs?.isNotBlank() == true) {
response = analyzeUrl.evalJS(checkJs, response) as Response
@@ -352,7 +353,7 @@ class HttpReadAloudService : BaseReadAloudService(),
}
}
}
coroutineContext.ensureActive()
currentCoroutineContext().ensureActive()
response.body.byteStream().let { stream ->
downloadErrorNo = 0
return stream
@@ -395,7 +396,7 @@ class HttpReadAloudService : BaseReadAloudService(),
return null
}
private fun md5SpeakFileName(content: String): String {
private fun md5SpeakFileName(content: String, textChapter: TextChapter? = this.textChapter): String {
return MD5Utils.md5Encode16(textChapter?.title ?: "") + "_" +
MD5Utils.md5Encode16("${ReadAloud.httpTTS?.url}-|-$speechRate-|-$content")
}