pull/1463/head
gedoor 3 years ago
parent eb9581e655
commit 941a21513a
  1. 60
      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)
delay(it.waitTime.toLong())
downloadAudio() downloadAudio()
} catch (e: ScriptException) { }
AppLog.put("tts接口错误\n${e.localizedMessage}", e) is ScriptException, is WrappedException -> {
toastOnUi("js错误\n${e.localizedMessage}") AppLog.put("js错误\n${it.localizedMessage}", it)
Timber.e(e) toastOnUi("js错误\n${it.localizedMessage}")
Timber.e(it)
cancel() cancel()
stopSelf() stopSelf()
} catch (e: SocketTimeoutException) {
removeSpeakCache(fileName)
downloadErrorNo++
if (playErrorNo > 5) {
createSilentSound(fileName)
} else {
downloadAudio()
} }
} catch (e: ConnectException) { is SocketTimeoutException, is ConnectException -> {
removeSpeakCache(fileName) removeSpeakCache(fileName)
downloadErrorNo++ downloadErrorNo++
if (playErrorNo > 5) { if (playErrorNo > 5) {
val msg = "tts超时或连接错误超过5次\n${it.localizedMessage}"
AppLog.put(msg, it)
toastOnUi(msg)
createSilentSound(fileName) createSilentSound(fileName)
} else { if (playErrorNo > 10) {
AppLog.put("tts接口网络错误\n${e.localizedMessage}", e) cancel()
toastOnUi("tts接口网络错误\n${e.localizedMessage}") stopSelf()
downloadAudio()
} }
} catch (e: IOException) {
removeSpeakCache(fileName)
downloadErrorNo++
if (playErrorNo > 5) {
createSilentSound(fileName)
} else { } else {
AppLog.put("tts下载音频错误\n${e.localizedMessage}", e)
toastOnUi("tts下载音频错误\n${e.localizedMessage}")
downloadAudio() downloadAudio()
} }
} catch (e: Exception) { }
else -> {
removeSpeakCache(fileName) removeSpeakCache(fileName)
createSilentSound(fileName) createSilentSound(fileName)
AppLog.put("tts接口错误\n${e.localizedMessage}", e) val msg = "tts下载错误\n${it.localizedMessage}"
toastOnUi("tts接口错误\n${e.localizedMessage}") AppLog.put(msg, it)
Timber.e(e) toastOnUi(msg)
Timber.e(it)
}
}
} }
} }
} }

Loading…
Cancel
Save