feat: 优化

pull/209/head
kunfei 5 years ago
parent 51b2ee20e8
commit 4fcee4175e
  1. 37
      app/src/main/java/io/legado/app/service/TTSReadAloudService.kt

@ -1,7 +1,6 @@
package io.legado.app.service package io.legado.app.service
import android.app.PendingIntent import android.app.PendingIntent
import android.os.Build
import android.speech.tts.TextToSpeech import android.speech.tts.TextToSpeech
import android.speech.tts.UtteranceProgressListener import android.speech.tts.UtteranceProgressListener
import io.legado.app.R import io.legado.app.R
@ -33,6 +32,8 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
} }
} }
private val ttsUtteranceListener = TTSUtteranceListener()
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
initTts() initTts()
@ -41,7 +42,9 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
private fun initTts() { private fun initTts() {
ttsInitFinish = false ttsInitFinish = false
textToSpeech = TextToSpeech(this, this) textToSpeech = TextToSpeech(this, this).apply {
setOnUtteranceProgressListener(ttsUtteranceListener)
}
} }
override fun onDestroy() { override fun onDestroy() {
@ -54,10 +57,9 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
if (status == TextToSpeech.SUCCESS) { if (status == TextToSpeech.SUCCESS) {
textToSpeech?.let { textToSpeech?.let {
it.language = Locale.CHINA it.language = Locale.CHINA
it.setOnUtteranceProgressListener(TTSUtteranceListener())
ttsInitFinish = true ttsInitFinish = true
play()
} }
play()
} else { } else {
launch { launch {
toast(R.string.tts_init_failed) toast(R.string.tts_init_failed)
@ -73,29 +75,18 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
if (requestFocus()) { if (requestFocus()) {
MediaHelp.playSilentSound(this) MediaHelp.playSilentSound(this)
super.play() super.play()
textToSpeech?.stop()
for (i in nowSpeak until contentList.size) { for (i in nowSpeak until contentList.size) {
if (i == 0) { textToSpeech?.speak(
speak(contentList[i], TextToSpeech.QUEUE_FLUSH, AppConst.APP_TAG + i) contentList[i],
} else { TextToSpeech.QUEUE_ADD,
speak(contentList[i], TextToSpeech.QUEUE_ADD, AppConst.APP_TAG + i) null,
} AppConst.APP_TAG + i
)
} }
} }
} }
private fun speak(content: String, queueMode: Int, utteranceId: String) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
textToSpeech?.speak(content, queueMode, null, utteranceId)
} else {
@Suppress("DEPRECATION")
textToSpeech?.speak(
content,
queueMode,
hashMapOf(Pair(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utteranceId))
)
}
}
/** /**
* 更新朗读速度 * 更新朗读速度
*/ */
@ -161,8 +152,8 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
pageIndex++ pageIndex++
ReadBook.moveToNextPage() ReadBook.moveToNextPage()
} }
postEvent(EventBus.TTS_PROGRESS, readAloudNumber + 1)
} }
postEvent(EventBus.TTS_PROGRESS, readAloudNumber + 1)
} }
override fun onDone(s: String) { override fun onDone(s: String) {

Loading…
Cancel
Save