pull/1336/head
gedoor 3 years ago
parent b9dce6051c
commit 64b0aa2986
  1. 28
      app/src/main/java/io/legado/app/service/HttpReadAloudService.kt

@ -27,7 +27,7 @@ class HttpReadAloudService : BaseReadAloudService(),
MediaPlayer.OnErrorListener, MediaPlayer.OnErrorListener,
MediaPlayer.OnCompletionListener { MediaPlayer.OnCompletionListener {
private val player by lazy { MediaPlayer() } private val mediaPlayer by lazy { MediaPlayer() }
private lateinit var ttsFolder: String private lateinit var ttsFolder: String
private var task: Coroutine<*>? = null private var task: Coroutine<*>? = null
private var playingIndex = -1 private var playingIndex = -1
@ -35,19 +35,19 @@ class HttpReadAloudService : BaseReadAloudService(),
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
ttsFolder = externalCacheDir!!.absolutePath + File.separator + "httpTTS" ttsFolder = externalCacheDir!!.absolutePath + File.separator + "httpTTS"
player.setOnErrorListener(this) mediaPlayer.setOnErrorListener(this)
player.setOnPreparedListener(this) mediaPlayer.setOnPreparedListener(this)
player.setOnCompletionListener(this) mediaPlayer.setOnCompletionListener(this)
} }
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
task?.cancel() task?.cancel()
player.release() mediaPlayer.release()
} }
override fun newReadAloud(play: Boolean) { override fun newReadAloud(play: Boolean) {
player.reset() mediaPlayer.reset()
playingIndex = -1 playingIndex = -1
super.newReadAloud(play) super.newReadAloud(play)
} }
@ -71,7 +71,7 @@ class HttpReadAloudService : BaseReadAloudService(),
} }
override fun playStop() { override fun playStop() {
player.stop() mediaPlayer.stop()
} }
private fun playNext() { private fun playNext() {
@ -151,9 +151,9 @@ class HttpReadAloudService : BaseReadAloudService(),
private fun playAudio(fd: FileDescriptor) { private fun playAudio(fd: FileDescriptor) {
if (playingIndex != nowSpeak && requestFocus()) { if (playingIndex != nowSpeak && requestFocus()) {
try { try {
player.reset() mediaPlayer.reset()
player.setDataSource(fd) mediaPlayer.setDataSource(fd)
player.prepareAsync() mediaPlayer.prepareAsync()
playingIndex = nowSpeak playingIndex = nowSpeak
postEvent(EventBus.TTS_PROGRESS, readAloudNumber + 1) postEvent(EventBus.TTS_PROGRESS, readAloudNumber + 1)
} catch (e: Exception) { } catch (e: Exception) {
@ -208,7 +208,7 @@ class HttpReadAloudService : BaseReadAloudService(),
override fun pauseReadAloud(pause: Boolean) { override fun pauseReadAloud(pause: Boolean) {
super.pauseReadAloud(pause) super.pauseReadAloud(pause)
player.pause() mediaPlayer.pause()
} }
override fun resumeReadAloud() { override fun resumeReadAloud() {
@ -216,7 +216,7 @@ class HttpReadAloudService : BaseReadAloudService(),
if (playingIndex == -1) { if (playingIndex == -1) {
play() play()
} else { } else {
player.start() mediaPlayer.start()
} }
} }
@ -225,7 +225,7 @@ class HttpReadAloudService : BaseReadAloudService(),
*/ */
override fun upSpeechRate(reset: Boolean) { override fun upSpeechRate(reset: Boolean) {
task?.cancel() task?.cancel()
player.stop() mediaPlayer.stop()
playingIndex = -1 playingIndex = -1
downloadAudio() downloadAudio()
} }
@ -233,7 +233,7 @@ class HttpReadAloudService : BaseReadAloudService(),
override fun onPrepared(mp: MediaPlayer?) { override fun onPrepared(mp: MediaPlayer?) {
super.play() super.play()
if (pause) return if (pause) return
mp?.start() mediaPlayer.start()
textChapter?.let { textChapter?.let {
if (readAloudNumber + 1 > it.getReadLength(pageIndex + 1)) { if (readAloudNumber + 1 > it.getReadLength(pageIndex + 1)) {
pageIndex++ pageIndex++

Loading…
Cancel
Save