|
|
@ -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) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |