pull/42/head
kunfei 5 years ago
parent 0c67447779
commit 3d21e636dd
  1. 3
      app/src/main/java/io/legado/app/service/BaseReadAloudService.kt
  2. 10
      app/src/main/java/io/legado/app/service/HttpReadAloudService.kt

@ -101,7 +101,8 @@ abstract class BaseReadAloudService : BaseService(),
return super.onStartCommand(intent, flags, startId) return super.onStartCommand(intent, flags, startId)
} }
private fun newReadAloud(dataKey: String?, play: Boolean) { @CallSuper
open fun newReadAloud(dataKey: String?, play: Boolean) {
dataKey?.let { dataKey?.let {
textChapter = IntentDataHelp.getData(dataKey) as? TextChapter textChapter = IntentDataHelp.getData(dataKey) as? TextChapter
textChapter?.let { textChapter -> textChapter?.let { textChapter ->

@ -11,6 +11,7 @@ import io.legado.app.utils.getPrefInt
import io.legado.app.utils.getPrefString import io.legado.app.utils.getPrefString
import io.legado.app.utils.postEvent import io.legado.app.utils.postEvent
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.jetbrains.anko.toast import org.jetbrains.anko.toast
import java.io.File import java.io.File
@ -25,6 +26,7 @@ class HttpReadAloudService : BaseReadAloudService(),
private val mediaPlayer = MediaPlayer() private val mediaPlayer = MediaPlayer()
private lateinit var ttsFolder: String private lateinit var ttsFolder: String
private var job: Job? = null
private var playingIndex = -1 private var playingIndex = -1
override fun onCreate() { override fun onCreate() {
@ -57,6 +59,12 @@ class HttpReadAloudService : BaseReadAloudService(),
) )
} }
override fun newReadAloud(dataKey: String?, play: Boolean) {
mediaPlayer.reset()
job?.cancel()
super.newReadAloud(dataKey, play)
}
override fun play() { override fun play() {
if (contentList.isEmpty()) return if (contentList.isEmpty()) return
if (nowSpeak == 0) { if (nowSpeak == 0) {
@ -70,7 +78,7 @@ class HttpReadAloudService : BaseReadAloudService(),
} }
private fun downloadAudio() { private fun downloadAudio() {
launch(IO) { job = launch(IO) {
FileHelp.deleteFile(ttsFolder) FileHelp.deleteFile(ttsFolder)
for (index in 0 until contentList.size) { for (index in 0 until contentList.size) {
val bytes = HttpHelper.getByteRetrofit("http://tts.baidu.com") val bytes = HttpHelper.getByteRetrofit("http://tts.baidu.com")

Loading…
Cancel
Save