From 4dd307ab22e8491471407fca5630d48e273c087e Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 27 Feb 2020 17:42:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/service/HttpReadAloudService.kt | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) 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 2cf46831c..c30180391 100644 --- a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt @@ -5,16 +5,14 @@ import android.media.MediaPlayer import io.legado.app.constant.EventBus import io.legado.app.help.AppConfig import io.legado.app.help.IntentHelp +import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.http.HttpHelper import io.legado.app.help.http.api.HttpPostApi import io.legado.app.service.help.ReadBook import io.legado.app.utils.FileUtils import io.legado.app.utils.LogUtils import io.legado.app.utils.postEvent -import kotlinx.coroutines.Dispatchers.IO -import kotlinx.coroutines.Job import kotlinx.coroutines.isActive -import kotlinx.coroutines.launch import java.io.File import java.io.FileDescriptor import java.io.FileInputStream @@ -27,7 +25,7 @@ class HttpReadAloudService : BaseReadAloudService(), private val mediaPlayer = MediaPlayer() private lateinit var ttsFolder: String - private var job: Job? = null + private var task: Coroutine<*>? = null private var playingIndex = -1 override fun onCreate() { @@ -40,13 +38,13 @@ class HttpReadAloudService : BaseReadAloudService(), override fun onDestroy() { super.onDestroy() - job?.cancel() + task?.cancel() mediaPlayer.release() } override fun newReadAloud(dataKey: String?, play: Boolean) { mediaPlayer.reset() - job?.cancel() + task?.cancel() playingIndex = -1 super.newReadAloud(dataKey, play) } @@ -64,7 +62,7 @@ class HttpReadAloudService : BaseReadAloudService(), } private fun downloadAudio() { - job = launch(IO) { + task = execute { FileUtils.deleteFile(ttsFolder) for (index in 0 until contentList.size) { if (isActive) { @@ -150,7 +148,7 @@ class HttpReadAloudService : BaseReadAloudService(), * 更新朗读速度 */ override fun upSpeechRate(reset: Boolean) { - job?.cancel() + task?.cancel() mediaPlayer.stop() playingIndex = -1 downloadAudio()