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:html:4.6.0'
//ExoPlayer
implementation 'com.google.android.exoplayer:exoplayer:2.13.0'
//
implementation 'com.hankcs:hanlp:portable-1.7.8'

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

Loading…
Cancel
Save