From 8b4d9bbd08b029be8da4b4ecf7f621ba2708f85c Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 16 Oct 2021 09:52:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/service/HttpReadAloudService.kt | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 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 fd5349ee9..efbcad3b3 100644 --- a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt @@ -35,7 +35,6 @@ class HttpReadAloudService : BaseReadAloudService(), private var playingIndex = -1 private var playIndexJob: Job? = null - override fun onCreate() { super.onCreate() mediaPlayer.setOnErrorListener(this) @@ -144,6 +143,7 @@ class HttpReadAloudService : BaseReadAloudService(), } downloadErrorNo = 0 } catch (e: CancellationException) { + removeSpeakCacheFile(fileName) //任务取消,不处理 } catch (e: SocketTimeoutException) { removeSpeakCacheFile(fileName) @@ -156,7 +156,14 @@ class HttpReadAloudService : BaseReadAloudService(), } } catch (e: ConnectException) { removeSpeakCacheFile(fileName) - toastOnUi("tts接口网络错误\n${e.localizedMessage}") + downloadErrorNo++ + if (playErrorNo > 5) { + createSilentSound(fileName) + } else { + AppLog.put("tts接口网络错误\n${e.localizedMessage}", e) + toastOnUi("tts接口网络错误\n${e.localizedMessage}") + downloadAudio() + } } catch (e: IOException) { removeSpeakCacheFile(fileName) createSilentSound(fileName) @@ -220,10 +227,10 @@ class HttpReadAloudService : BaseReadAloudService(), FileUtils.createFileIfNotExist("${ttsFolderPath}$name.mp3") private fun removeCacheFile() { + val cacheRegex = Regex(""".+\.mp3$""") + val reg = """^${MD5Utils.md5Encode16(textChapter!!.title)}_[a-z0-9]{16}\.mp3$""".toRegex() FileUtils.listDirsAndFiles(ttsFolderPath)?.forEach { - if (Regex(""".+\.mp3$""").matches(it.name)) { //mp3缓存文件 - val reg = - """^${MD5Utils.md5Encode16(textChapter!!.title)}_[a-z0-9]{16}\.mp3$""".toRegex() + if (cacheRegex.matches(it.name)) { //mp3缓存文件 if (!reg.matches(it.name)) { FileUtils.deleteFile(it.absolutePath) }