pull/34/head
kunfei 5 years ago
parent cd6c1d9c49
commit b3e6451d5c
  1. 69
      app/src/main/java/io/legado/app/service/HttpReadAloudService.kt
  2. 4
      app/src/main/java/io/legado/app/service/TTSReadAloudService.kt

@ -1,29 +1,92 @@
package io.legado.app.service 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() { override fun onCreate() {
super.onCreate() super.onCreate()
mediaPlayer.setOnErrorListener(this)
mediaPlayer.setOnPreparedListener(this)
mediaPlayer.setOnCompletionListener(this)
} }
override fun onDestroy() { override fun onDestroy() {
super.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() { 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() { override fun prevP() {
if (nowSpeak > 0) {
mediaPlayer.stop()
nowSpeak--
readAloudNumber -= contentList[nowSpeak].length.minus(1)
play()
}
} }
override fun nextP() { 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)
}
}
} }

@ -63,7 +63,7 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
override fun play() { override fun play() {
if (contentList.size < 1 || !ttsIsSuccess) { if (contentList.isEmpty() || !ttsIsSuccess) {
return return
} }
if (requestFocus()) { if (requestFocus()) {
@ -166,7 +166,7 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
override fun onDone(s: String) { override fun onDone(s: String) {
readAloudNumber += contentList[nowSpeak].length + 1 readAloudNumber += contentList[nowSpeak].length + 1
nowSpeak += 1 nowSpeak++
if (nowSpeak >= contentList.size) { if (nowSpeak >= contentList.size) {
postEvent(Bus.TTS_TURN_PAGE, 2) postEvent(Bus.TTS_TURN_PAGE, 2)
} }

Loading…
Cancel
Save