From f8224b0637147a54f5282d4c390b720530de3148 Mon Sep 17 00:00:00 2001 From: Horis <8674809+821938089@users.noreply.github.com> Date: Fri, 19 Dec 2025 09:51:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20#5612?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/service/HttpReadAloudService.kt | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt index 286423590..27f0588af 100644 --- a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt @@ -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") }