pull/1814/head
kunfei 3 years ago
parent 1359bb95d1
commit e99459e2e7
  1. 26
      app/src/main/java/io/legado/app/service/HttpReadAloudService.kt

@ -19,8 +19,6 @@ import io.legado.app.model.ReadBook
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import okhttp3.Response import okhttp3.Response
import org.mozilla.javascript.WrappedException import org.mozilla.javascript.WrappedException
import java.io.File import java.io.File
@ -43,9 +41,10 @@ class HttpReadAloudService : BaseReadAloudService(),
} }
private var speechRate: Int = AppConfig.speechRatePlay private var speechRate: Int = AppConfig.speechRatePlay
private val cacheFiles = hashSetOf<String>() private val cacheFiles = hashSetOf<String>()
private var task: Coroutine<*>? = null private var downloadTask: Coroutine<*>? = null
private var playIndexJob: Job? = null private var playIndexJob: Job? = null
private val mutex = Mutex() private var downloadErrorNo: Int = 0
private var playErrorNo = 0
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
@ -54,7 +53,7 @@ class HttpReadAloudService : BaseReadAloudService(),
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
task?.cancel() downloadTask?.cancel()
exoPlayer.release() exoPlayer.release()
} }
@ -73,7 +72,7 @@ class HttpReadAloudService : BaseReadAloudService(),
AppConfig.ttsSpeechRate.toString(), AppConfig.ttsSpeechRate.toString(),
contentList[nowSpeak] contentList[nowSpeak]
) )
if (nowSpeak == 0) { if (nowSpeak == 0 && downloadTask?.isActive != true) {
downloadAudio() downloadAudio()
} else { } else {
val file = getSpeakFileAsMd5(fileName) val file = getSpeakFileAsMd5(fileName)
@ -103,11 +102,9 @@ class HttpReadAloudService : BaseReadAloudService(),
} }
} }
private var downloadErrorNo: Int = 0
private fun downloadAudio() { private fun downloadAudio() {
task?.cancel() downloadTask?.cancel()
task = execute { downloadTask = execute {
clearSpeakCache() clearSpeakCache()
removeCacheFile() removeCacheFile()
val httpTts = ReadAloud.httpTTS ?: return@execute val httpTts = ReadAloud.httpTTS ?: return@execute
@ -136,9 +133,7 @@ class HttpReadAloudService : BaseReadAloudService(),
source = httpTts, source = httpTts,
headerMapF = httpTts.getHeaderMap(true) headerMapF = httpTts.getHeaderMap(true)
) )
var response = mutex.withLock { var response = analyzeUrl.getResponseAwait()
analyzeUrl.getResponseAwait()
}
ensureActive() ensureActive()
httpTts.loginCheckJs?.takeIf { checkJs -> httpTts.loginCheckJs?.takeIf { checkJs ->
checkJs.isNotBlank() checkJs.isNotBlank()
@ -156,7 +151,6 @@ class HttpReadAloudService : BaseReadAloudService(),
} }
ensureActive() ensureActive()
response.body!!.bytes().let { bytes -> response.body!!.bytes().let { bytes ->
ensureActive()
val file = createSpeakFileAsMd5IfNotExist(fileName) val file = createSpeakFileAsMd5IfNotExist(fileName)
file.writeBytes(bytes) file.writeBytes(bytes)
removeSpeakCache(fileName) removeSpeakCache(fileName)
@ -316,7 +310,7 @@ class HttpReadAloudService : BaseReadAloudService(),
* 更新朗读速度 * 更新朗读速度
*/ */
override fun upSpeechRate(reset: Boolean) { override fun upSpeechRate(reset: Boolean) {
task?.cancel() downloadTask?.cancel()
exoPlayer.stop() exoPlayer.stop()
speechRate = AppConfig.speechRatePlay speechRate = AppConfig.speechRatePlay
downloadAudio() downloadAudio()
@ -345,8 +339,6 @@ class HttpReadAloudService : BaseReadAloudService(),
} }
} }
private var playErrorNo = 0
override fun onPlayerError(error: PlaybackException) { override fun onPlayerError(error: PlaybackException) {
super.onPlayerError(error) super.onPlayerError(error)
AppLog.put("朗读错误\n${contentList[nowSpeak]}", error) AppLog.put("朗读错误\n${contentList[nowSpeak]}", error)

Loading…
Cancel
Save