|
|
@ -17,9 +17,12 @@ import io.legado.app.utils.postEvent |
|
|
|
import kotlinx.coroutines.ensureActive |
|
|
|
import kotlinx.coroutines.ensureActive |
|
|
|
import kotlinx.coroutines.isActive |
|
|
|
import kotlinx.coroutines.isActive |
|
|
|
import org.jetbrains.anko.collections.forEachWithIndex |
|
|
|
import org.jetbrains.anko.collections.forEachWithIndex |
|
|
|
|
|
|
|
import org.jetbrains.anko.runOnUiThread |
|
|
|
|
|
|
|
import org.jetbrains.anko.toast |
|
|
|
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.* |
|
|
@ -105,22 +108,38 @@ class HttpReadAloudService : BaseReadAloudService(), |
|
|
|
speakSpeed = AppConfig.ttsSpeechRate |
|
|
|
speakSpeed = AppConfig.ttsSpeechRate |
|
|
|
).getByteArray().let { bytes -> |
|
|
|
).getByteArray().let { bytes -> |
|
|
|
ensureActive() |
|
|
|
ensureActive() |
|
|
|
val file = getSpeakFileAsMd5(fileName) |
|
|
|
|
|
|
|
|
|
|
|
val file = getSpeakFileAsMd5IfNotExist(fileName) |
|
|
|
//val file = getSpeakFile(index) |
|
|
|
//val file = getSpeakFile(index) |
|
|
|
file.writeBytes(bytes) |
|
|
|
file.writeBytes(bytes) |
|
|
|
removeSpeakCacheFile(fileName) |
|
|
|
removeSpeakCacheFile(fileName) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val fis = FileInputStream(file) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 用来检测下载的文件是否为可正常播放的音频 (如果不是的话抛出异常,没找到更秒的办法,先这么着吧) |
|
|
|
|
|
|
|
MediaPlayer().apply { |
|
|
|
|
|
|
|
setDataSource(fis.fd) |
|
|
|
|
|
|
|
prepare() |
|
|
|
|
|
|
|
release() |
|
|
|
|
|
|
|
} |
|
|
|
if (index == nowSpeak) { |
|
|
|
if (index == nowSpeak) { |
|
|
|
@Suppress("BlockingMethodInNonBlockingContext") |
|
|
|
@Suppress("BlockingMethodInNonBlockingContext") |
|
|
|
val fis = FileInputStream(file) |
|
|
|
|
|
|
|
playAudio(fis.fd) |
|
|
|
playAudio(fis.fd) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (e: SocketTimeoutException) { |
|
|
|
} catch (e: SocketTimeoutException) { |
|
|
|
removeSpeakCacheFile(fileName) |
|
|
|
removeSpeakCacheFile(fileName) |
|
|
|
// delay(2000) |
|
|
|
runOnUiThread { toast("tts接口超时,尝试重新获取") } |
|
|
|
// downloadAudio() |
|
|
|
downloadAudio() |
|
|
|
} catch (e: ConnectException) { |
|
|
|
} catch (e: ConnectException) { |
|
|
|
removeSpeakCacheFile(fileName) |
|
|
|
removeSpeakCacheFile(fileName) |
|
|
|
|
|
|
|
runOnUiThread { toast("网络错误") } |
|
|
|
|
|
|
|
} catch (e: IOException) { |
|
|
|
|
|
|
|
val file = getSpeakFileAsMd5(fileName) |
|
|
|
|
|
|
|
if (file.exists()) { |
|
|
|
|
|
|
|
FileUtils.deleteFile(file.absolutePath) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
runOnUiThread { toast("tts文件解析错误") } |
|
|
|
} catch (e: Exception) { |
|
|
|
} catch (e: Exception) { |
|
|
|
removeSpeakCacheFile(fileName) |
|
|
|
removeSpeakCacheFile(fileName) |
|
|
|
} |
|
|
|
} |
|
|
@ -165,6 +184,9 @@ class HttpReadAloudService : BaseReadAloudService(), |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun getSpeakFileAsMd5(name: String): File = |
|
|
|
private fun getSpeakFileAsMd5(name: String): File = |
|
|
|
|
|
|
|
FileUtils.getFile(File(speakFilePath()), "$name.mp3") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun getSpeakFileAsMd5IfNotExist(name: String): File = |
|
|
|
FileUtils.createFileIfNotExist("${speakFilePath()}$name.mp3") |
|
|
|
FileUtils.createFileIfNotExist("${speakFilePath()}$name.mp3") |
|
|
|
|
|
|
|
|
|
|
|
private fun removeCacheFile() { |
|
|
|
private fun removeCacheFile() { |
|
|
|