pull/1440/head
gedoor 4 years ago committed by RinOrz
parent 31ae208bd8
commit e90e989f9f
  1. 88
      app/src/main/java/io/legado/app/service/HttpReadAloudService.kt

@ -18,11 +18,11 @@ import kotlinx.coroutines.*
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.sync.withLock
import okhttp3.Response import okhttp3.Response
import org.mozilla.javascript.WrappedException
import timber.log.Timber import timber.log.Timber
import java.io.File import java.io.File
import java.io.FileDescriptor import java.io.FileDescriptor
import java.io.FileInputStream import java.io.FileInputStream
import java.io.IOException
import java.net.ConnectException import java.net.ConnectException
import java.net.SocketTimeoutException import java.net.SocketTimeoutException
import java.util.* import java.util.*
@ -123,7 +123,7 @@ class HttpReadAloudService : BaseReadAloudService(),
createSilentSound(fileName) createSilentSound(fileName)
return@forEachIndexed return@forEachIndexed
} else { } else {
try { runCatching {
createSpeakCache(fileName) createSpeakCache(fileName)
val analyzeUrl = AnalyzeUrl( val analyzeUrl = AnalyzeUrl(
httpTts.url, httpTts.url,
@ -162,52 +162,46 @@ class HttpReadAloudService : BaseReadAloudService(),
} }
} }
downloadErrorNo = 0 downloadErrorNo = 0
} catch (e: CancellationException) { }.onFailure {
removeSpeakCache(fileName) when (it) {
//任务取消,不处理 is CancellationException -> removeSpeakCache(fileName)
} catch (e: ConcurrentException) { is ConcurrentException -> {
removeSpeakCache(fileName) removeSpeakCache(fileName)
downloadAudio() delay(it.waitTime.toLong())
} catch (e: ScriptException) { downloadAudio()
AppLog.put("tts接口错误\n${e.localizedMessage}", e) }
toastOnUi("js错误\n${e.localizedMessage}") is ScriptException, is WrappedException -> {
Timber.e(e) AppLog.put("js错误\n${it.localizedMessage}", it)
cancel() toastOnUi("js错误\n${it.localizedMessage}")
stopSelf() Timber.e(it)
} catch (e: SocketTimeoutException) { cancel()
removeSpeakCache(fileName) stopSelf()
downloadErrorNo++ }
if (playErrorNo > 5) { is SocketTimeoutException, is ConnectException -> {
createSilentSound(fileName) removeSpeakCache(fileName)
} else { downloadErrorNo++
downloadAudio() if (playErrorNo > 5) {
} val msg = "tts超时或连接错误超过5次\n${it.localizedMessage}"
} catch (e: ConnectException) { AppLog.put(msg, it)
removeSpeakCache(fileName) toastOnUi(msg)
downloadErrorNo++ createSilentSound(fileName)
if (playErrorNo > 5) { if (playErrorNo > 10) {
createSilentSound(fileName) cancel()
} else { stopSelf()
AppLog.put("tts接口网络错误\n${e.localizedMessage}", e) }
toastOnUi("tts接口网络错误\n${e.localizedMessage}") } else {
downloadAudio() downloadAudio()
} }
} catch (e: IOException) { }
removeSpeakCache(fileName) else -> {
downloadErrorNo++ removeSpeakCache(fileName)
if (playErrorNo > 5) { createSilentSound(fileName)
createSilentSound(fileName) val msg = "tts下载错误\n${it.localizedMessage}"
} else { AppLog.put(msg, it)
AppLog.put("tts下载音频错误\n${e.localizedMessage}", e) toastOnUi(msg)
toastOnUi("tts下载音频错误\n${e.localizedMessage}") Timber.e(it)
downloadAudio() }
} }
} catch (e: Exception) {
removeSpeakCache(fileName)
createSilentSound(fileName)
AppLog.put("tts接口错误\n${e.localizedMessage}", e)
toastOnUi("tts接口错误\n${e.localizedMessage}")
Timber.e(e)
} }
} }
} }

Loading…
Cancel
Save