Merge pull request #2302 from 821938089/little-fix

一些优化
This commit is contained in:
kunfei
2022-09-19 11:25:07 +08:00
committed by GitHub
5 changed files with 37 additions and 8 deletions
@@ -121,6 +121,7 @@ class AudioPlayService : BaseService(),
override fun onDestroy() {
super.onDestroy()
isRun = false
abandonFocus()
exoPlayer.release()
mediaSessionCompat?.release()
unregisterReceiver(broadcastReceiver)
@@ -166,9 +167,12 @@ class AudioPlayService : BaseService(),
/**
* 暂停播放
*/
private fun pause() {
private fun pause(abandonFocus: Boolean = true) {
try {
pause = true
if (abandonFocus) {
abandonFocus()
}
upPlayProgressJob?.cancel()
position = exoPlayer.currentPosition.toInt()
if (exoPlayer.isPlaying) exoPlayer.pause()
@@ -466,7 +470,7 @@ class AudioPlayService : BaseService(),
needResumeOnAudioFocusGain = true
if (!pause) {
needResumeOnAudioFocusGain = true
pause()
pause(false)
}
}
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> {
@@ -549,6 +553,7 @@ class AudioPlayService : BaseService(),
}
/**
* 请求音频焦点
* @return 音频焦点
*/
private fun requestFocus(): Boolean {
@@ -558,4 +563,12 @@ class AudioPlayService : BaseService(),
return MediaHelp.requestFocus(audioManager, mFocusRequest)
}
/**
* 放弃音频焦点
*/
private fun abandonFocus() {
@Suppress("DEPRECATION")
audioManager.abandonAudioFocus(this)
}
}
@@ -92,6 +92,7 @@ abstract class BaseReadAloudService : BaseService(),
super.onDestroy()
isRun = false
pause = true
abandonFocus()
unregisterReceiver(broadcastReceiver)
postEvent(EventBus.ALOUD_STATE, Status.STOP)
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_STOPPED)
@@ -147,8 +148,11 @@ abstract class BaseReadAloudService : BaseService(),
abstract fun playStop()
@CallSuper
open fun pauseReadAloud() {
open fun pauseReadAloud(abandonFocus: Boolean = true) {
pause = true
if (abandonFocus) {
abandonFocus()
}
upNotification()
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PAUSED)
postEvent(EventBus.ALOUD_STATE, Status.PAUSE)
@@ -228,6 +232,7 @@ abstract class BaseReadAloudService : BaseService(),
}
/**
* 请求音频焦点
* @return 音频焦点
*/
fun requestFocus(): Boolean {
@@ -241,6 +246,14 @@ abstract class BaseReadAloudService : BaseService(),
return requestFocus
}
/**
* 放弃音频焦点
*/
private fun abandonFocus() {
@Suppress("DEPRECATION")
audioManager.abandonAudioFocus(this)
}
/**
* 更新媒体状态
*/
@@ -302,7 +315,7 @@ abstract class BaseReadAloudService : BaseService(),
AppLog.put("音频焦点暂时丢失并会很快再次获得,暂停朗读")
if (!pause) {
needResumeOnAudioFocusGain = true
pauseReadAloud()
pauseReadAloud(false)
}
}
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> {
@@ -271,8 +271,8 @@ class HttpReadAloudService : BaseReadAloudService(),
}
override fun pauseReadAloud() {
super.pauseReadAloud()
override fun pauseReadAloud(abandonFocus: Boolean) {
super.pauseReadAloud(abandonFocus)
kotlin.runCatching {
playIndexJob?.cancel()
exoPlayer.pause()
@@ -121,8 +121,8 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
/**
* 暂停朗读
*/
override fun pauseReadAloud() {
super.pauseReadAloud()
override fun pauseReadAloud(abandonFocus: Boolean) {
super.pauseReadAloud(abandonFocus)
textToSpeech?.stop()
}
@@ -201,6 +201,9 @@ data class TextPage(
val maxIndex = min(lineIndex, lineSize)
for (index in 0 until maxIndex) {
length += textLines[index].charSize
if (textLines[index].isParagraphEnd) {
length++
}
}
return length + columnIndex
}