|
|
@ -28,13 +28,13 @@ class HttpReadAloudService : BaseReadAloudService(), |
|
|
|
|
|
|
|
|
|
|
|
private val bdRegex = "^(\\s|\\p{P})+$".toRegex() |
|
|
|
private val bdRegex = "^(\\s|\\p{P})+$".toRegex() |
|
|
|
private val mediaPlayer = MediaPlayer() |
|
|
|
private val mediaPlayer = MediaPlayer() |
|
|
|
private val ttsFolder: String by lazy { |
|
|
|
private val ttsFolderPath: String by lazy { |
|
|
|
externalCacheDir!!.absolutePath + File.separator + "httpTTS" |
|
|
|
externalCacheDir!!.absolutePath + File.separator + "httpTTS" + File.separator |
|
|
|
} |
|
|
|
} |
|
|
|
private var task: Coroutine<*>? = null |
|
|
|
private var task: Coroutine<*>? = null |
|
|
|
private var playingIndex = -1 |
|
|
|
private var playingIndex = -1 |
|
|
|
private var playIndexJob: Job? = null |
|
|
|
private var playIndexJob: Job? = null |
|
|
|
private var errorNo = 0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun onCreate() { |
|
|
|
override fun onCreate() { |
|
|
|
super.onCreate() |
|
|
|
super.onCreate() |
|
|
@ -87,6 +87,8 @@ class HttpReadAloudService : BaseReadAloudService(), |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private var downloadErrorNo: Int = 0 |
|
|
|
|
|
|
|
|
|
|
|
private fun downloadAudio() { |
|
|
|
private fun downloadAudio() { |
|
|
|
task?.cancel() |
|
|
|
task?.cancel() |
|
|
|
task = execute { |
|
|
|
task = execute { |
|
|
@ -140,12 +142,13 @@ class HttpReadAloudService : BaseReadAloudService(), |
|
|
|
playAudio(fis.fd) |
|
|
|
playAudio(fis.fd) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
downloadErrorNo = 0 |
|
|
|
} catch (e: CancellationException) { |
|
|
|
} catch (e: CancellationException) { |
|
|
|
//任务取消,不处理 |
|
|
|
//任务取消,不处理 |
|
|
|
} catch (e: SocketTimeoutException) { |
|
|
|
} catch (e: SocketTimeoutException) { |
|
|
|
removeSpeakCacheFile(fileName) |
|
|
|
removeSpeakCacheFile(fileName) |
|
|
|
errorNo++ |
|
|
|
downloadErrorNo++ |
|
|
|
if (errorNo > 5) { |
|
|
|
if (playErrorNo > 5) { |
|
|
|
createSilentSound(fileName) |
|
|
|
createSilentSound(fileName) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
toastOnUi("tts接口超时,尝试重新获取") |
|
|
|
toastOnUi("tts接口超时,尝试重新获取") |
|
|
@ -188,7 +191,6 @@ class HttpReadAloudService : BaseReadAloudService(), |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun speakFilePath() = ttsFolder + File.separator |
|
|
|
|
|
|
|
private fun md5SpeakFileName(url: String, ttsConfig: String, content: String): String { |
|
|
|
private fun md5SpeakFileName(url: String, ttsConfig: String, content: String): String { |
|
|
|
return MD5Utils.md5Encode16(textChapter!!.title) + "_" + MD5Utils.md5Encode16("$url-|-$ttsConfig-|-$content") |
|
|
|
return MD5Utils.md5Encode16(textChapter!!.title) + "_" + MD5Utils.md5Encode16("$url-|-$ttsConfig-|-$content") |
|
|
|
} |
|
|
|
} |
|
|
@ -199,26 +201,26 @@ class HttpReadAloudService : BaseReadAloudService(), |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun hasSpeakFile(name: String) = |
|
|
|
private fun hasSpeakFile(name: String) = |
|
|
|
FileUtils.exist("${speakFilePath()}$name.mp3") |
|
|
|
FileUtils.exist("${ttsFolderPath}$name.mp3") |
|
|
|
|
|
|
|
|
|
|
|
private fun hasSpeakCacheFile(name: String) = |
|
|
|
private fun hasSpeakCacheFile(name: String) = |
|
|
|
FileUtils.exist("${speakFilePath()}$name.mp3.cache") |
|
|
|
FileUtils.exist("${ttsFolderPath}$name.mp3.cache") |
|
|
|
|
|
|
|
|
|
|
|
private fun createSpeakCacheFile(name: String): File = |
|
|
|
private fun createSpeakCacheFile(name: String): File = |
|
|
|
FileUtils.createFileWithReplace("${speakFilePath()}$name.mp3.cache") |
|
|
|
FileUtils.createFileWithReplace("${ttsFolderPath}$name.mp3.cache") |
|
|
|
|
|
|
|
|
|
|
|
private fun removeSpeakCacheFile(name: String) { |
|
|
|
private fun removeSpeakCacheFile(name: String) { |
|
|
|
FileUtils.delete("${speakFilePath()}$name.mp3.cache") |
|
|
|
FileUtils.delete("${ttsFolderPath}$name.mp3.cache") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun getSpeakFileAsMd5(name: String): File = |
|
|
|
private fun getSpeakFileAsMd5(name: String): File = |
|
|
|
FileUtils.getFile(File(speakFilePath()), "$name.mp3") |
|
|
|
File("${ttsFolderPath}$name.mp3") |
|
|
|
|
|
|
|
|
|
|
|
private fun createSpeakFileAsMd5IfNotExist(name: String): File = |
|
|
|
private fun createSpeakFileAsMd5IfNotExist(name: String): File = |
|
|
|
FileUtils.createFileIfNotExist("${speakFilePath()}$name.mp3") |
|
|
|
FileUtils.createFileIfNotExist("${ttsFolderPath}$name.mp3") |
|
|
|
|
|
|
|
|
|
|
|
private fun removeCacheFile() { |
|
|
|
private fun removeCacheFile() { |
|
|
|
FileUtils.listDirsAndFiles(speakFilePath())?.forEach { |
|
|
|
FileUtils.listDirsAndFiles(ttsFolderPath)?.forEach { |
|
|
|
if (Regex(""".+\.mp3$""").matches(it.name)) { //mp3缓存文件 |
|
|
|
if (Regex(""".+\.mp3$""").matches(it.name)) { //mp3缓存文件 |
|
|
|
val reg = |
|
|
|
val reg = |
|
|
|
"""^${MD5Utils.md5Encode16(textChapter!!.title)}_[a-z0-9]{16}\.mp3$""".toRegex() |
|
|
|
"""^${MD5Utils.md5Encode16(textChapter!!.title)}_[a-z0-9]{16}\.mp3$""".toRegex() |
|
|
@ -293,14 +295,16 @@ class HttpReadAloudService : BaseReadAloudService(), |
|
|
|
upPlayPos() |
|
|
|
upPlayPos() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private var playErrorNo = 0 |
|
|
|
|
|
|
|
|
|
|
|
override fun onError(mp: MediaPlayer?, what: Int, extra: Int): Boolean { |
|
|
|
override fun onError(mp: MediaPlayer?, what: Int, extra: Int): Boolean { |
|
|
|
if (what == -38 && extra == 0) { |
|
|
|
if (what == -38 && extra == 0) { |
|
|
|
play() |
|
|
|
play() |
|
|
|
return true |
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
|
AppLog.put("朗读错误,($what, $extra)") |
|
|
|
AppLog.put("朗读错误,($what, $extra)") |
|
|
|
errorNo++ |
|
|
|
playErrorNo++ |
|
|
|
if (errorNo >= 5) { |
|
|
|
if (playErrorNo >= 5) { |
|
|
|
toastOnUi("朗读连续5次错误, 最后一次错误代码($what, $extra)") |
|
|
|
toastOnUi("朗读连续5次错误, 最后一次错误代码($what, $extra)") |
|
|
|
ReadAloud.pause(this) |
|
|
|
ReadAloud.pause(this) |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -310,7 +314,7 @@ class HttpReadAloudService : BaseReadAloudService(), |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun onCompletion(mp: MediaPlayer?) { |
|
|
|
override fun onCompletion(mp: MediaPlayer?) { |
|
|
|
errorNo = 0 |
|
|
|
playErrorNo = 0 |
|
|
|
playNext() |
|
|
|
playNext() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|