diff --git a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt index 9af47f861..8c46f11c8 100644 --- a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt @@ -101,7 +101,8 @@ abstract class BaseReadAloudService : BaseService(), return super.onStartCommand(intent, flags, startId) } - private fun newReadAloud(dataKey: String?, play: Boolean) { + @CallSuper + open fun newReadAloud(dataKey: String?, play: Boolean) { dataKey?.let { textChapter = IntentDataHelp.getData(dataKey) as? TextChapter textChapter?.let { textChapter -> diff --git a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt index 27d11accc..6ec27991a 100644 --- a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt @@ -11,6 +11,7 @@ import io.legado.app.utils.getPrefInt import io.legado.app.utils.getPrefString import io.legado.app.utils.postEvent import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.Job import kotlinx.coroutines.launch import org.jetbrains.anko.toast import java.io.File @@ -25,6 +26,7 @@ class HttpReadAloudService : BaseReadAloudService(), private val mediaPlayer = MediaPlayer() private lateinit var ttsFolder: String + private var job: Job? = null private var playingIndex = -1 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() { if (contentList.isEmpty()) return if (nowSpeak == 0) { @@ -70,7 +78,7 @@ class HttpReadAloudService : BaseReadAloudService(), } private fun downloadAudio() { - launch(IO) { + job = launch(IO) { FileHelp.deleteFile(ttsFolder) for (index in 0 until contentList.size) { val bytes = HttpHelper.getByteRetrofit("http://tts.baidu.com")