Merge pull request #2260 from 821938089/little-fix

优化TTS
pull/2268/head
kunfei 2 years ago committed by GitHub
commit d81ee90534
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 24
      app/src/main/java/io/legado/app/service/HttpReadAloudService.kt

@ -77,6 +77,7 @@ class HttpReadAloudService : BaseReadAloudService(),
} }
}.onFailure { }.onFailure {
toastOnUi("朗读出错:${it.localizedMessage}") toastOnUi("朗读出错:${it.localizedMessage}")
AppLog.put("朗读出错:${it.localizedMessage}", it)
} }
} }
} }
@ -115,7 +116,12 @@ class HttpReadAloudService : BaseReadAloudService(),
playAudio(file) playAudio(file)
} }
} else if (speakText.isEmpty()) { } else if (speakText.isEmpty()) {
AppLog.put("阅读段落内容为空,使用无声音频代替。")
createSilentSound(fileName) createSilentSound(fileName)
if (index == nowSpeak) {
val file = getSpeakFileAsMd5(fileName)
playAudio(file)
}
return@forEachIndexed return@forEachIndexed
} else { } else {
runCatching { runCatching {
@ -138,7 +144,7 @@ class HttpReadAloudService : BaseReadAloudService(),
}?.let { ct -> }?.let { ct ->
response.headers["Content-Type"]?.let { contentType -> response.headers["Content-Type"]?.let { contentType ->
if (!contentType.matches(ct.toRegex())) { if (!contentType.matches(ct.toRegex())) {
throw NoStackTraceException(response.body!!.string()) throw NoStackTraceException("TTS服务器返回错误:" + response.body!!.string())
} }
} }
} }
@ -182,9 +188,16 @@ class HttpReadAloudService : BaseReadAloudService(),
AppLog.put(msg, it) AppLog.put(msg, it)
it.printOnDebug() it.printOnDebug()
if (downloadErrorNo > 5) { if (downloadErrorNo > 5) {
AppLog.put("TTS服务器连续5次错误,已暂停阅读。")
toastOnUi("TTS服务器连续5次错误,已暂停阅读。")
pauseReadAloud(true) pauseReadAloud(true)
} else { } else {
AppLog.put("TTS下载音频出错,使用无声音频代替。")
createSilentSound(fileName) createSilentSound(fileName)
if (index == nowSpeak) {
val file = getSpeakFileAsMd5(fileName)
playAudio(file)
}
} }
} }
} }
@ -194,7 +207,7 @@ class HttpReadAloudService : BaseReadAloudService(),
}.onStart { }.onStart {
downloadTaskIsActive = true downloadTaskIsActive = true
}.onError { }.onError {
AppLog.put("朗读下载出错\n${it.localizedMessage}") AppLog.put("朗读下载出错\n${it.localizedMessage}", it)
}.onFinally { }.onFinally {
downloadTaskIsActive = false downloadTaskIsActive = false
} }
@ -245,8 +258,10 @@ class HttpReadAloudService : BaseReadAloudService(),
private fun removeCacheFile() { private fun removeCacheFile() {
val titleMd5 = MD5Utils.md5Encode16(textChapter?.title ?: "") val titleMd5 = MD5Utils.md5Encode16(textChapter?.title ?: "")
FileUtils.listDirsAndFiles(ttsFolderPath)?.forEach { FileUtils.listDirsAndFiles(ttsFolderPath)?.forEach {
if (!it.name.startsWith(titleMd5) val isSilentSound = it.length() == 2160L
&& System.currentTimeMillis() - it.lastModified() > 600000 if ((!it.name.startsWith(titleMd5)
&& System.currentTimeMillis() - it.lastModified() > 600000)
|| isSilentSound
) { ) {
FileUtils.delete(it.absolutePath) FileUtils.delete(it.absolutePath)
} }
@ -334,6 +349,7 @@ class HttpReadAloudService : BaseReadAloudService(),
playErrorNo++ playErrorNo++
if (playErrorNo >= 5) { if (playErrorNo >= 5) {
toastOnUi("朗读连续5次错误, 最后一次错误代码(${error.localizedMessage})") toastOnUi("朗读连续5次错误, 最后一次错误代码(${error.localizedMessage})")
AppLog.put("朗读连续5次错误, 最后一次错误代码(${error.localizedMessage})", error)
ReadAloud.pause(this) ReadAloud.pause(this)
} else { } else {
playNext() playNext()

Loading…
Cancel
Save