pull/849/head
gedoor 4 years ago
parent de64357570
commit e1ad15cdee
  1. 3
      app/build.gradle
  2. 28
      app/src/main/java/io/legado/app/service/HttpReadAloudService.kt

@ -203,6 +203,9 @@ dependencies {
implementation 'io.noties.markwon:ext-tables:4.6.0' implementation 'io.noties.markwon:ext-tables:4.6.0'
implementation 'io.noties.markwon:html:4.6.0' implementation 'io.noties.markwon:html:4.6.0'
//ExoPlayer
implementation 'com.google.android.exoplayer:exoplayer:2.13.0'
// //
implementation 'com.hankcs:hanlp:portable-1.7.8' implementation 'com.hankcs:hanlp:portable-1.7.8'

@ -26,7 +26,7 @@ class HttpReadAloudService : BaseReadAloudService(),
MediaPlayer.OnErrorListener, MediaPlayer.OnErrorListener,
MediaPlayer.OnCompletionListener { MediaPlayer.OnCompletionListener {
private val mediaPlayer = MediaPlayer() private val player 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
@ -34,9 +34,9 @@ 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"
mediaPlayer.setOnErrorListener(this) player.setOnErrorListener(this)
mediaPlayer.setOnPreparedListener(this) player.setOnPreparedListener(this)
mediaPlayer.setOnCompletionListener(this) player.setOnCompletionListener(this)
} }
override fun onTaskRemoved(rootIntent: Intent?) { override fun onTaskRemoved(rootIntent: Intent?) {
@ -47,11 +47,11 @@ class HttpReadAloudService : BaseReadAloudService(),
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
task?.cancel() task?.cancel()
mediaPlayer.release() player.release()
} }
override fun newReadAloud(dataKey: String?, play: Boolean) { override fun newReadAloud(dataKey: String?, play: Boolean) {
mediaPlayer.reset() player.reset()
playingIndex = -1 playingIndex = -1
super.newReadAloud(dataKey, play) super.newReadAloud(dataKey, play)
} }
@ -142,9 +142,9 @@ class HttpReadAloudService : BaseReadAloudService(),
private fun playAudio(fd: FileDescriptor) { private fun playAudio(fd: FileDescriptor) {
if (playingIndex != nowSpeak && requestFocus()) { if (playingIndex != nowSpeak && requestFocus()) {
try { try {
mediaPlayer.reset() player.reset()
mediaPlayer.setDataSource(fd) player.setDataSource(fd)
mediaPlayer.prepareAsync() player.prepareAsync()
playingIndex = nowSpeak playingIndex = nowSpeak
postEvent(EventBus.TTS_PROGRESS, readAloudNumber + 1) postEvent(EventBus.TTS_PROGRESS, readAloudNumber + 1)
} catch (e: Exception) { } catch (e: Exception) {
@ -199,7 +199,7 @@ class HttpReadAloudService : BaseReadAloudService(),
override fun pauseReadAloud(pause: Boolean) { override fun pauseReadAloud(pause: Boolean) {
super.pauseReadAloud(pause) super.pauseReadAloud(pause)
mediaPlayer.pause() player.pause()
} }
override fun resumeReadAloud() { override fun resumeReadAloud() {
@ -207,7 +207,7 @@ class HttpReadAloudService : BaseReadAloudService(),
if (playingIndex == -1) { if (playingIndex == -1) {
play() play()
} else { } else {
mediaPlayer.start() player.start()
} }
} }
@ -216,7 +216,7 @@ class HttpReadAloudService : BaseReadAloudService(),
*/ */
override fun upSpeechRate(reset: Boolean) { override fun upSpeechRate(reset: Boolean) {
task?.cancel() task?.cancel()
mediaPlayer.stop() player.stop()
playingIndex = -1 playingIndex = -1
downloadAudio() downloadAudio()
} }
@ -226,7 +226,7 @@ class HttpReadAloudService : BaseReadAloudService(),
*/ */
override fun prevP() { override fun prevP() {
if (nowSpeak > 0) { if (nowSpeak > 0) {
mediaPlayer.stop() player.stop()
nowSpeak-- nowSpeak--
readAloudNumber -= contentList[nowSpeak].length.minus(1) readAloudNumber -= contentList[nowSpeak].length.minus(1)
play() play()
@ -238,7 +238,7 @@ class HttpReadAloudService : BaseReadAloudService(),
*/ */
override fun nextP() { override fun nextP() {
if (nowSpeak < contentList.size - 1) { if (nowSpeak < contentList.size - 1) {
mediaPlayer.stop() player.stop()
readAloudNumber += contentList[nowSpeak].length.plus(1) readAloudNumber += contentList[nowSpeak].length.plus(1)
nowSpeak++ nowSpeak++
play() play()

Loading…
Cancel
Save