Merge pull request #2302 from 821938089/little-fix

一些优化
pull/2306/head
kunfei 2 years ago committed by GitHub
commit 2f091df05b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      app/src/main/java/io/legado/app/service/AudioPlayService.kt
  2. 17
      app/src/main/java/io/legado/app/service/BaseReadAloudService.kt
  3. 4
      app/src/main/java/io/legado/app/service/HttpReadAloudService.kt
  4. 4
      app/src/main/java/io/legado/app/service/TTSReadAloudService.kt
  5. 3
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt

@ -121,6 +121,7 @@ class AudioPlayService : BaseService(),
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
isRun = false isRun = false
abandonFocus()
exoPlayer.release() exoPlayer.release()
mediaSessionCompat?.release() mediaSessionCompat?.release()
unregisterReceiver(broadcastReceiver) unregisterReceiver(broadcastReceiver)
@ -166,9 +167,12 @@ class AudioPlayService : BaseService(),
/** /**
* 暂停播放 * 暂停播放
*/ */
private fun pause() { private fun pause(abandonFocus: Boolean = true) {
try { try {
pause = true pause = true
if (abandonFocus) {
abandonFocus()
}
upPlayProgressJob?.cancel() upPlayProgressJob?.cancel()
position = exoPlayer.currentPosition.toInt() position = exoPlayer.currentPosition.toInt()
if (exoPlayer.isPlaying) exoPlayer.pause() if (exoPlayer.isPlaying) exoPlayer.pause()
@ -466,7 +470,7 @@ class AudioPlayService : BaseService(),
needResumeOnAudioFocusGain = true needResumeOnAudioFocusGain = true
if (!pause) { if (!pause) {
needResumeOnAudioFocusGain = true needResumeOnAudioFocusGain = true
pause() pause(false)
} }
} }
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> { AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> {
@ -549,6 +553,7 @@ class AudioPlayService : BaseService(),
} }
/** /**
* 请求音频焦点
* @return 音频焦点 * @return 音频焦点
*/ */
private fun requestFocus(): Boolean { private fun requestFocus(): Boolean {
@ -558,4 +563,12 @@ class AudioPlayService : BaseService(),
return MediaHelp.requestFocus(audioManager, mFocusRequest) return MediaHelp.requestFocus(audioManager, mFocusRequest)
} }
/**
* 放弃音频焦点
*/
private fun abandonFocus() {
@Suppress("DEPRECATION")
audioManager.abandonAudioFocus(this)
}
} }

@ -92,6 +92,7 @@ abstract class BaseReadAloudService : BaseService(),
super.onDestroy() super.onDestroy()
isRun = false isRun = false
pause = true pause = true
abandonFocus()
unregisterReceiver(broadcastReceiver) unregisterReceiver(broadcastReceiver)
postEvent(EventBus.ALOUD_STATE, Status.STOP) postEvent(EventBus.ALOUD_STATE, Status.STOP)
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_STOPPED) upMediaSessionPlaybackState(PlaybackStateCompat.STATE_STOPPED)
@ -147,8 +148,11 @@ abstract class BaseReadAloudService : BaseService(),
abstract fun playStop() abstract fun playStop()
@CallSuper @CallSuper
open fun pauseReadAloud() { open fun pauseReadAloud(abandonFocus: Boolean = true) {
pause = true pause = true
if (abandonFocus) {
abandonFocus()
}
upNotification() upNotification()
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PAUSED) upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PAUSED)
postEvent(EventBus.ALOUD_STATE, Status.PAUSE) postEvent(EventBus.ALOUD_STATE, Status.PAUSE)
@ -228,6 +232,7 @@ abstract class BaseReadAloudService : BaseService(),
} }
/** /**
* 请求音频焦点
* @return 音频焦点 * @return 音频焦点
*/ */
fun requestFocus(): Boolean { fun requestFocus(): Boolean {
@ -241,6 +246,14 @@ abstract class BaseReadAloudService : BaseService(),
return requestFocus return requestFocus
} }
/**
* 放弃音频焦点
*/
private fun abandonFocus() {
@Suppress("DEPRECATION")
audioManager.abandonAudioFocus(this)
}
/** /**
* 更新媒体状态 * 更新媒体状态
*/ */
@ -302,7 +315,7 @@ abstract class BaseReadAloudService : BaseService(),
AppLog.put("音频焦点暂时丢失并会很快再次获得,暂停朗读") AppLog.put("音频焦点暂时丢失并会很快再次获得,暂停朗读")
if (!pause) { if (!pause) {
needResumeOnAudioFocusGain = true needResumeOnAudioFocusGain = true
pauseReadAloud() pauseReadAloud(false)
} }
} }
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> { AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> {

@ -271,8 +271,8 @@ class HttpReadAloudService : BaseReadAloudService(),
} }
override fun pauseReadAloud() { override fun pauseReadAloud(abandonFocus: Boolean) {
super.pauseReadAloud() super.pauseReadAloud(abandonFocus)
kotlin.runCatching { kotlin.runCatching {
playIndexJob?.cancel() playIndexJob?.cancel()
exoPlayer.pause() exoPlayer.pause()

@ -121,8 +121,8 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
/** /**
* 暂停朗读 * 暂停朗读
*/ */
override fun pauseReadAloud() { override fun pauseReadAloud(abandonFocus: Boolean) {
super.pauseReadAloud() super.pauseReadAloud(abandonFocus)
textToSpeech?.stop() textToSpeech?.stop()
} }

@ -201,6 +201,9 @@ data class TextPage(
val maxIndex = min(lineIndex, lineSize) val maxIndex = min(lineIndex, lineSize)
for (index in 0 until maxIndex) { for (index in 0 until maxIndex) {
length += textLines[index].charSize length += textLines[index].charSize
if (textLines[index].isParagraphEnd) {
length++
}
} }
return length + columnIndex return length + columnIndex
} }

Loading…
Cancel
Save