pull/34/head
kunfei 5 years ago
parent af22c60100
commit fd1570de81
  1. 25
      app/src/main/java/io/legado/app/service/BaseReadAloudService.kt
  2. 33
      app/src/main/java/io/legado/app/service/TTSReadAloudService.kt

@ -17,10 +17,12 @@ import io.legado.app.constant.Action
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
import io.legado.app.constant.Bus import io.legado.app.constant.Bus
import io.legado.app.constant.Status import io.legado.app.constant.Status
import io.legado.app.help.IntentDataHelp
import io.legado.app.help.MediaHelp import io.legado.app.help.MediaHelp
import io.legado.app.help.PendingIntentHelp import io.legado.app.help.PendingIntentHelp
import io.legado.app.receiver.MediaButtonReceiver import io.legado.app.receiver.MediaButtonReceiver
import io.legado.app.ui.widget.page.TextChapter import io.legado.app.ui.widget.page.TextChapter
import io.legado.app.utils.getPrefBoolean
import io.legado.app.utils.postEvent import io.legado.app.utils.postEvent
abstract class BaseReadAloudService : BaseService(), abstract class BaseReadAloudService : BaseService(),
@ -166,7 +168,28 @@ abstract class BaseReadAloudService : BaseService(),
return super.onStartCommand(intent, flags, startId) return super.onStartCommand(intent, flags, startId)
} }
abstract fun newReadAloud(dataKey: String?, play: Boolean) private fun newReadAloud(dataKey: String?, play: Boolean) {
dataKey?.let {
textChapter = IntentDataHelp.getData(dataKey) as? TextChapter
textChapter?.let { textChapter ->
nowSpeak = 0
readAloudNumber = textChapter.getReadLength(pageIndex)
contentList.clear()
if (getPrefBoolean("readAloudByPage")) {
for (index in pageIndex..textChapter.lastIndex()) {
textChapter.page(index)?.text?.split("\n")?.let {
contentList.addAll(it)
}
}
} else {
contentList.addAll(textChapter.getUnRead(pageIndex).split("\n"))
}
if (play) play()
} ?: stopSelf()
} ?: stopSelf()
}
abstract fun play()
@CallSuper @CallSuper
open fun pauseReadAloud(pause: Boolean) { open fun pauseReadAloud(pause: Boolean) {

@ -8,9 +8,7 @@ import io.legado.app.R
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
import io.legado.app.constant.Bus import io.legado.app.constant.Bus
import io.legado.app.constant.Status import io.legado.app.constant.Status
import io.legado.app.help.IntentDataHelp
import io.legado.app.help.IntentHelp import io.legado.app.help.IntentHelp
import io.legado.app.ui.widget.page.TextChapter
import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.getPrefBoolean
import io.legado.app.utils.getPrefInt import io.legado.app.utils.getPrefInt
import io.legado.app.utils.postEvent import io.legado.app.utils.postEvent
@ -55,7 +53,7 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
} else { } else {
textToSpeech?.setOnUtteranceProgressListener(TTSUtteranceListener()) textToSpeech?.setOnUtteranceProgressListener(TTSUtteranceListener())
ttsIsSuccess = true ttsIsSuccess = true
playTTS() play()
} }
} else { } else {
toast(R.string.tts_init_failed) toast(R.string.tts_init_failed)
@ -63,29 +61,8 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
} }
} }
override fun newReadAloud(dataKey: String?, play: Boolean) {
dataKey?.let {
textChapter = IntentDataHelp.getData(dataKey) as? TextChapter
textChapter?.let { textChapter ->
nowSpeak = 0
readAloudNumber = textChapter.getReadLength(pageIndex)
contentList.clear()
if (getPrefBoolean("readAloudByPage")) {
for (index in pageIndex..textChapter.lastIndex()) {
textChapter.page(index)?.text?.split("\n")?.let {
contentList.addAll(it)
}
}
} else {
contentList.addAll(textChapter.getUnRead(pageIndex).split("\n"))
}
if (play) playTTS()
} ?: stopSelf()
} ?: stopSelf()
}
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
private fun playTTS() { override fun play() {
if (contentList.size < 1 || !ttsIsSuccess) { if (contentList.size < 1 || !ttsIsSuccess) {
return return
} }
@ -138,7 +115,7 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
textToSpeech?.stop() textToSpeech?.stop()
nowSpeak-- nowSpeak--
readAloudNumber -= contentList[nowSpeak].length.minus(1) readAloudNumber -= contentList[nowSpeak].length.minus(1)
playTTS() play()
} }
} }
@ -150,7 +127,7 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
textToSpeech?.stop() textToSpeech?.stop()
readAloudNumber += contentList[nowSpeak].length.plus(1) readAloudNumber += contentList[nowSpeak].length.plus(1)
nowSpeak++ nowSpeak++
playTTS() play()
} }
} }
@ -169,7 +146,7 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
*/ */
override fun resumeReadAloud() { override fun resumeReadAloud() {
super.resumeReadAloud() super.resumeReadAloud()
playTTS() play()
} }
/** /**

Loading…
Cancel
Save