From b3e6451d5c1b8ccdc40d231801d0225403962e5a Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 11 Sep 2019 17:40:31 +0800 Subject: [PATCH] up --- .../app/service/HttpReadAloudService.kt | 69 ++++++++++++++++++- .../legado/app/service/TTSReadAloudService.kt | 4 +- 2 files changed, 68 insertions(+), 5 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 5def5d8a9..43bca75b2 100644 --- a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt @@ -1,29 +1,92 @@ package io.legado.app.service -class HttpReadAloudService : BaseReadAloudService() { +import android.media.MediaPlayer +import io.legado.app.constant.Bus +import io.legado.app.utils.postEvent +import kotlinx.coroutines.launch +import org.jetbrains.anko.toast + +class HttpReadAloudService : BaseReadAloudService(), + MediaPlayer.OnPreparedListener, + MediaPlayer.OnErrorListener, + MediaPlayer.OnCompletionListener { + + private var mediaPlayer = MediaPlayer() override fun onCreate() { super.onCreate() + mediaPlayer.setOnErrorListener(this) + mediaPlayer.setOnPreparedListener(this) + mediaPlayer.setOnCompletionListener(this) } override fun onDestroy() { super.onDestroy() + mediaPlayer.release() + } + + private fun getAudioPath(): String { + val audioText = contentList[nowSpeak] + return "http://tts.baidu.com/text2audio?idx=1&tex=$audioText&cuid=baidu_speech_demo&cod=2&lan=zh&ctp=1&pdt=1&spd=4&per=6&vol=5&pit=5&_res_tag_=audio" } override fun play() { + if (contentList.isEmpty()) return + if (requestFocus()) { + mediaPlayer.reset() + mediaPlayer.setDataSource(getAudioPath()) + mediaPlayer.prepareAsync() + } + } + override fun pauseReadAloud(pause: Boolean) { + super.pauseReadAloud(pause) + mediaPlayer.pause() } - override fun upSpeechRate(reset: Boolean) { + override fun resumeReadAloud() { + super.resumeReadAloud() + mediaPlayer.start() + } + override fun upSpeechRate(reset: Boolean) { + mediaPlayer.stop() + play() } override fun prevP() { - + if (nowSpeak > 0) { + mediaPlayer.stop() + nowSpeak-- + readAloudNumber -= contentList[nowSpeak].length.minus(1) + play() + } } override fun nextP() { + if (nowSpeak < contentList.size - 1) { + mediaPlayer.stop() + readAloudNumber += contentList[nowSpeak].length.plus(1) + nowSpeak++ + play() + } + } + override fun onPrepared(mp: MediaPlayer?) { + mp?.start() } + override fun onError(mp: MediaPlayer?, what: Int, extra: Int): Boolean { + launch { toast("播放出错") } + return false + } + + override fun onCompletion(mp: MediaPlayer?) { + if (nowSpeak < contentList.size) { + nowSpeak++ + play() + } else { + postEvent(Bus.TTS_TURN_PAGE, 2) + } + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/service/TTSReadAloudService.kt b/app/src/main/java/io/legado/app/service/TTSReadAloudService.kt index 15064598c..eef74c2fe 100644 --- a/app/src/main/java/io/legado/app/service/TTSReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/TTSReadAloudService.kt @@ -63,7 +63,7 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener @Suppress("DEPRECATION") override fun play() { - if (contentList.size < 1 || !ttsIsSuccess) { + if (contentList.isEmpty() || !ttsIsSuccess) { return } if (requestFocus()) { @@ -166,7 +166,7 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener override fun onDone(s: String) { readAloudNumber += contentList[nowSpeak].length + 1 - nowSpeak += 1 + nowSpeak++ if (nowSpeak >= contentList.size) { postEvent(Bus.TTS_TURN_PAGE, 2) }