|
|
@ -31,6 +31,7 @@ class HttpReadAloudService : BaseReadAloudService(), |
|
|
|
private val ttsFolderPath: String by lazy { |
|
|
|
private val ttsFolderPath: String by lazy { |
|
|
|
externalCacheDir!!.absolutePath + File.separator + "httpTTS" + File.separator |
|
|
|
externalCacheDir!!.absolutePath + File.separator + "httpTTS" + File.separator |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private val cacheFiles = hashSetOf<String>() |
|
|
|
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 |
|
|
@ -91,6 +92,7 @@ class HttpReadAloudService : BaseReadAloudService(), |
|
|
|
private fun downloadAudio() { |
|
|
|
private fun downloadAudio() { |
|
|
|
task?.cancel() |
|
|
|
task?.cancel() |
|
|
|
task = execute { |
|
|
|
task = execute { |
|
|
|
|
|
|
|
clearSpeakCache() |
|
|
|
removeCacheFile() |
|
|
|
removeCacheFile() |
|
|
|
ReadAloud.httpTTS?.let { httpTts -> |
|
|
|
ReadAloud.httpTTS?.let { httpTts -> |
|
|
|
contentList.forEachIndexed { index, item -> |
|
|
|
contentList.forEachIndexed { index, item -> |
|
|
@ -108,7 +110,7 @@ class HttpReadAloudService : BaseReadAloudService(), |
|
|
|
val fis = FileInputStream(file) |
|
|
|
val fis = FileInputStream(file) |
|
|
|
playAudio(fis.fd) |
|
|
|
playAudio(fis.fd) |
|
|
|
} |
|
|
|
} |
|
|
|
} else if (hasSpeakCacheFile(fileName)) { //缓存文件还在,可能还没下载完 |
|
|
|
} else if (hasSpeakCache(fileName)) { //缓存文件还在,可能还没下载完 |
|
|
|
return@let |
|
|
|
return@let |
|
|
|
} else { //没有下载并且没有缓存文件 |
|
|
|
} else { //没有下载并且没有缓存文件 |
|
|
|
try { |
|
|
|
try { |
|
|
@ -117,7 +119,7 @@ class HttpReadAloudService : BaseReadAloudService(), |
|
|
|
createSilentSound(fileName) |
|
|
|
createSilentSound(fileName) |
|
|
|
return@let |
|
|
|
return@let |
|
|
|
} |
|
|
|
} |
|
|
|
createSpeakCacheFile(fileName) |
|
|
|
createSpeakCache(fileName) |
|
|
|
val analyzeUrl = AnalyzeUrl( |
|
|
|
val analyzeUrl = AnalyzeUrl( |
|
|
|
httpTts.url, |
|
|
|
httpTts.url, |
|
|
|
speakText = speakText, |
|
|
|
speakText = speakText, |
|
|
@ -135,7 +137,7 @@ class HttpReadAloudService : BaseReadAloudService(), |
|
|
|
ensureActive() |
|
|
|
ensureActive() |
|
|
|
val file = createSpeakFileAsMd5IfNotExist(fileName) |
|
|
|
val file = createSpeakFileAsMd5IfNotExist(fileName) |
|
|
|
file.writeBytes(bytes) |
|
|
|
file.writeBytes(bytes) |
|
|
|
removeSpeakCacheFile(fileName) |
|
|
|
removeSpeakCache(fileName) |
|
|
|
val fis = FileInputStream(file) |
|
|
|
val fis = FileInputStream(file) |
|
|
|
if (index == nowSpeak) { |
|
|
|
if (index == nowSpeak) { |
|
|
|
playAudio(fis.fd) |
|
|
|
playAudio(fis.fd) |
|
|
@ -143,10 +145,10 @@ class HttpReadAloudService : BaseReadAloudService(), |
|
|
|
} |
|
|
|
} |
|
|
|
downloadErrorNo = 0 |
|
|
|
downloadErrorNo = 0 |
|
|
|
} catch (e: CancellationException) { |
|
|
|
} catch (e: CancellationException) { |
|
|
|
removeSpeakCacheFile(fileName) |
|
|
|
removeSpeakCache(fileName) |
|
|
|
//任务取消,不处理 |
|
|
|
//任务取消,不处理 |
|
|
|
} catch (e: SocketTimeoutException) { |
|
|
|
} catch (e: SocketTimeoutException) { |
|
|
|
removeSpeakCacheFile(fileName) |
|
|
|
removeSpeakCache(fileName) |
|
|
|
downloadErrorNo++ |
|
|
|
downloadErrorNo++ |
|
|
|
if (playErrorNo > 5) { |
|
|
|
if (playErrorNo > 5) { |
|
|
|
createSilentSound(fileName) |
|
|
|
createSilentSound(fileName) |
|
|
@ -155,7 +157,7 @@ class HttpReadAloudService : BaseReadAloudService(), |
|
|
|
downloadAudio() |
|
|
|
downloadAudio() |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (e: ConnectException) { |
|
|
|
} catch (e: ConnectException) { |
|
|
|
removeSpeakCacheFile(fileName) |
|
|
|
removeSpeakCache(fileName) |
|
|
|
downloadErrorNo++ |
|
|
|
downloadErrorNo++ |
|
|
|
if (playErrorNo > 5) { |
|
|
|
if (playErrorNo > 5) { |
|
|
|
createSilentSound(fileName) |
|
|
|
createSilentSound(fileName) |
|
|
@ -165,12 +167,17 @@ class HttpReadAloudService : BaseReadAloudService(), |
|
|
|
downloadAudio() |
|
|
|
downloadAudio() |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (e: IOException) { |
|
|
|
} catch (e: IOException) { |
|
|
|
removeSpeakCacheFile(fileName) |
|
|
|
removeSpeakCache(fileName) |
|
|
|
|
|
|
|
downloadErrorNo++ |
|
|
|
|
|
|
|
if (playErrorNo > 5) { |
|
|
|
createSilentSound(fileName) |
|
|
|
createSilentSound(fileName) |
|
|
|
AppLog.put("tts文件解析错误") |
|
|
|
} else { |
|
|
|
toastOnUi("tts文件解析错误\n${e.localizedMessage}") |
|
|
|
AppLog.put("tts下载音频错误\n${e.localizedMessage}", e) |
|
|
|
|
|
|
|
toastOnUi("tts下载音频错误\n${e.localizedMessage}") |
|
|
|
|
|
|
|
downloadAudio() |
|
|
|
|
|
|
|
} |
|
|
|
} catch (e: Exception) { |
|
|
|
} catch (e: Exception) { |
|
|
|
removeSpeakCacheFile(fileName) |
|
|
|
removeSpeakCache(fileName) |
|
|
|
createSilentSound(fileName) |
|
|
|
createSilentSound(fileName) |
|
|
|
AppLog.put("tts接口错误\n${e.localizedMessage}", e) |
|
|
|
AppLog.put("tts接口错误\n${e.localizedMessage}", e) |
|
|
|
toastOnUi("tts接口错误\n${e.localizedMessage}") |
|
|
|
toastOnUi("tts接口错误\n${e.localizedMessage}") |
|
|
@ -207,18 +214,20 @@ class HttpReadAloudService : BaseReadAloudService(), |
|
|
|
file.writeBytes(resources.openRawResource(R.raw.silent_sound).readBytes()) |
|
|
|
file.writeBytes(resources.openRawResource(R.raw.silent_sound).readBytes()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun hasSpeakFile(name: String) = |
|
|
|
@Synchronized |
|
|
|
FileUtils.exist("${ttsFolderPath}$name.mp3") |
|
|
|
private fun clearSpeakCache() = cacheFiles.clear() |
|
|
|
|
|
|
|
|
|
|
|
private fun hasSpeakCacheFile(name: String) = |
|
|
|
@Synchronized |
|
|
|
FileUtils.exist("${ttsFolderPath}$name.mp3.cache") |
|
|
|
private fun hasSpeakCache(name: String) = cacheFiles.contains(name) |
|
|
|
|
|
|
|
|
|
|
|
private fun createSpeakCacheFile(name: String): File = |
|
|
|
@Synchronized |
|
|
|
FileUtils.createFileWithReplace("${ttsFolderPath}$name.mp3.cache") |
|
|
|
private fun createSpeakCache(name: String) = cacheFiles.add(name) |
|
|
|
|
|
|
|
|
|
|
|
private fun removeSpeakCacheFile(name: String) { |
|
|
|
@Synchronized |
|
|
|
FileUtils.delete("${ttsFolderPath}$name.mp3.cache") |
|
|
|
private fun removeSpeakCache(name: String) = cacheFiles.remove(name) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun hasSpeakFile(name: String) = |
|
|
|
|
|
|
|
FileUtils.exist("${ttsFolderPath}$name.mp3") |
|
|
|
|
|
|
|
|
|
|
|
private fun getSpeakFileAsMd5(name: String): File = |
|
|
|
private fun getSpeakFileAsMd5(name: String): File = |
|
|
|
File("${ttsFolderPath}$name.mp3") |
|
|
|
File("${ttsFolderPath}$name.mp3") |
|
|
|