pull/32/head
kunfei 5 years ago
parent f5c2cf40a5
commit f4805c6c8d
  1. 19
      app/src/main/java/io/legado/app/service/ReadAloudService.kt

@ -11,10 +11,13 @@ import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat
import io.legado.app.R
import io.legado.app.base.BaseService
import io.legado.app.constant.Bus
import io.legado.app.constant.Status
import io.legado.app.help.IntentHelp
import io.legado.app.help.MediaHelp
import io.legado.app.receiver.MediaButtonReceiver
import io.legado.app.service.notification.ReadAloudNotification
import io.legado.app.utils.postEvent
import io.legado.app.utils.toast
import kotlinx.coroutines.launch
import java.util.*
@ -145,12 +148,21 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
}
}
@Suppress("DEPRECATION")
private fun playTTS() {
if (contentList.size < 1 || !ttsIsSuccess) {
return
}
if (!pause && requestFocus()) {
postEvent(Bus.ALOUD_STATE, Status.PLAY)
ReadAloudNotification.upNotification(this)
for (i in nowSpeak until contentList.size) {
if (i == 0) {
textToSpeech?.speak(contentList[i], TextToSpeech.QUEUE_FLUSH, null, "content")
} else {
textToSpeech?.speak(contentList[i], TextToSpeech.QUEUE_ADD, null, "content")
}
}
}
}
@ -189,6 +201,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
}
private fun pauseReadAloud(pause: Boolean) {
this.pause = pause
textToSpeech?.stop()
}
@ -230,12 +243,18 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
when (focusChange) {
AudioManager.AUDIOFOCUS_GAIN -> {
// 重新获得焦点, 可做恢复播放,恢复后台音量的操作
if (!pause) {
resumeReadAloud()
}
}
AudioManager.AUDIOFOCUS_LOSS -> {
// 永久丢失焦点除非重新主动获取,这种情况是被其他播放器抢去了焦点, 为避免与其他播放器混音,可将音乐暂停
}
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT -> {
// 暂时丢失焦点,这种情况是被其他应用申请了短暂的焦点,可压低后台音量
if (!pause) {
pauseReadAloud(false)
}
}
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> {
// 短暂丢失焦点,这种情况是被其他应用申请了短暂的焦点希望其他声音能压低音量(或者关闭声音)凸显这个声音(比如短信提示音),

Loading…
Cancel
Save