pull/2300/head
kunfei 2 years ago
parent 5baafb2d79
commit 9ac00b6bbc
  1. 36
      app/src/main/java/io/legado/app/service/AudioPlayService.kt

@ -76,7 +76,7 @@ class AudioPlayService : BaseService(),
} }
private var mediaSessionCompat: MediaSessionCompat? = null private var mediaSessionCompat: MediaSessionCompat? = null
private var broadcastReceiver: BroadcastReceiver? = null private var broadcastReceiver: BroadcastReceiver? = null
private var audioFocusLossTransient = false private var needResumeOnAudioFocusGain = false
private var position = AudioPlay.book?.durChapterPos ?: 0 private var position = AudioPlay.book?.durChapterPos ?: 0
private var dsJob: Job? = null private var dsJob: Job? = null
private var upPlayProgressJob: Job? = null private var upPlayProgressJob: Job? = null
@ -101,7 +101,8 @@ class AudioPlayService : BaseService(),
position = AudioPlay.book?.durChapterPos ?: 0 position = AudioPlay.book?.durChapterPos ?: 0
loadContent() loadContent()
} }
IntentAction.pause -> pause(true)
IntentAction.pause -> pause()
IntentAction.resume -> resume() IntentAction.resume -> resume()
IntentAction.prev -> AudioPlay.prev(this) IntentAction.prev -> AudioPlay.prev(this)
IntentAction.next -> AudioPlay.next(this) IntentAction.next -> AudioPlay.next(this)
@ -165,9 +166,9 @@ class AudioPlayService : BaseService(),
/** /**
* 暂停播放 * 暂停播放
*/ */
private fun pause(pause: Boolean) { private fun pause() {
try { try {
AudioPlayService.pause = pause pause = true
upPlayProgressJob?.cancel() upPlayProgressJob?.cancel()
position = exoPlayer.currentPosition.toInt() position = exoPlayer.currentPosition.toInt()
if (exoPlayer.isPlaying) exoPlayer.pause() if (exoPlayer.isPlaying) exoPlayer.pause()
@ -431,7 +432,7 @@ class AudioPlayService : BaseService(),
broadcastReceiver = object : BroadcastReceiver() { broadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
if (AudioManager.ACTION_AUDIO_BECOMING_NOISY == intent.action) { if (AudioManager.ACTION_AUDIO_BECOMING_NOISY == intent.action) {
pause(true) pause()
} }
} }
} }
@ -449,23 +450,28 @@ class AudioPlayService : BaseService(),
} }
when (focusChange) { when (focusChange) {
AudioManager.AUDIOFOCUS_GAIN -> { AudioManager.AUDIOFOCUS_GAIN -> {
// 重新获得焦点, 可做恢复播放,恢复后台音量的操作 if (needResumeOnAudioFocusGain) {
audioFocusLossTransient = false AppLog.put("音频焦点获得,继续播放")
if (!pause) resume() resume()
} else {
AppLog.put("音频焦点获得")
}
} }
AudioManager.AUDIOFOCUS_LOSS -> { AudioManager.AUDIOFOCUS_LOSS -> {
// 永久丢失焦点除非重新主动获取,这种情况是被其他播放器抢去了焦点, 为避免与其他播放器混音,可将音乐暂停 AppLog.put("音频焦点丢失,暂停播放")
if (audioFocusLossTransient) { pause()
pause(true)
}
} }
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT -> { AudioManager.AUDIOFOCUS_LOSS_TRANSIENT -> {
// 暂时丢失焦点,这种情况是被其他应用申请了短暂的焦点,可压低后台音量 AppLog.put("音频焦点暂时丢失并会很快再次获得,暂停播放")
audioFocusLossTransient = true needResumeOnAudioFocusGain = true
if (!pause) pause(false) if (!pause) {
needResumeOnAudioFocusGain = true
pause()
}
} }
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> { AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> {
// 短暂丢失焦点,这种情况是被其他应用申请了短暂的焦点希望其他声音能压低音量(或者关闭声音)凸显这个声音(比如短信提示音), // 短暂丢失焦点,这种情况是被其他应用申请了短暂的焦点希望其他声音能压低音量(或者关闭声音)凸显这个声音(比如短信提示音),
AppLog.put("音频焦点短暂丢失,不做处理")
} }
} }
} }

Loading…
Cancel
Save