pull/1395/head
gedoor 3 years ago
parent da7b458644
commit 0e24588776
  1. 21
      app/src/main/java/io/legado/app/service/HttpReadAloudService.kt

@ -25,6 +25,7 @@ class HttpReadAloudService : BaseReadAloudService(),
MediaPlayer.OnErrorListener,
MediaPlayer.OnCompletionListener {
private val bdRegex = "^(\\s|\\p{P})+$".toRegex()
private val mediaPlayer = MediaPlayer()
private val ttsFolder: String by lazy {
externalCacheDir!!.absolutePath + File.separator + "httpTTS"
@ -91,8 +92,13 @@ class HttpReadAloudService : BaseReadAloudService(),
ReadAloud.httpTTS?.let { httpTts ->
contentList.forEachIndexed { index, item ->
if (isActive) {
val speakText = item.replace(bdRegex, "")
val fileName =
md5SpeakFileName(httpTts.url, AppConfig.ttsSpeechRate.toString(), item)
md5SpeakFileName(
httpTts.url,
AppConfig.ttsSpeechRate.toString(),
speakText
)
if (hasSpeakFile(fileName)) { //已经下载好的语音缓存
if (index == nowSpeak) {
val file = getSpeakFileAsMd5(fileName)
@ -103,10 +109,15 @@ class HttpReadAloudService : BaseReadAloudService(),
return@let
} else { //没有下载并且没有缓存文件
try {
if (speakText.isEmpty()) {
ensureActive()
createSpeakFileAsMd5IfNotExist(fileName)
return@let
}
createSpeakCacheFile(fileName)
val analyzeUrl = AnalyzeUrl(
httpTts.url,
speakText = item,
speakText = speakText,
speakSpeed = AppConfig.ttsSpeechRate,
source = httpTts,
headerMapF = httpTts.getHeaderMap(true)
@ -119,7 +130,7 @@ class HttpReadAloudService : BaseReadAloudService(),
}
response.body!!.bytes().let { bytes ->
ensureActive()
val file = getSpeakFileAsMd5IfNotExist(fileName)
val file = createSpeakFileAsMd5IfNotExist(fileName)
file.writeBytes(bytes)
removeSpeakCacheFile(fileName)
val fis = FileInputStream(file)
@ -187,7 +198,7 @@ class HttpReadAloudService : BaseReadAloudService(),
private fun getSpeakFileAsMd5(name: String): File =
FileUtils.getFile(File(speakFilePath()), "$name.mp3")
private fun getSpeakFileAsMd5IfNotExist(name: String): File =
private fun createSpeakFileAsMd5IfNotExist(name: String): File =
FileUtils.createFileIfNotExist("${speakFilePath()}$name.mp3")
private fun removeCacheFile() {
@ -276,7 +287,7 @@ class HttpReadAloudService : BaseReadAloudService(),
AppLog.put("朗读错误,($what, $extra)")
errorNo++
if (errorNo >= 5) {
toastOnUi("朗读连续3次错误, 最后一次错误代码($what, $extra)")
toastOnUi("朗读连续5次错误, 最后一次错误代码($what, $extra)")
ReadAloud.pause(this)
} else {
playNext()

Loading…
Cancel
Save