mirror of
https://github.com/aniyomiorg/aniyomi.git
synced 2024-11-24 05:38:01 +03:00
feat(player): Pause player when headset is disconnected (#1103)
This commit is contained in:
parent
2a8699db8f
commit
a7067f411f
1 changed files with 24 additions and 6 deletions
|
@ -153,6 +153,8 @@ class PlayerActivity : BaseActivity() {
|
|||
|
||||
private val playbackStateBuilder = PlaybackStateCompat.Builder()
|
||||
|
||||
private lateinit var headsetReceiver: BroadcastReceiver
|
||||
|
||||
internal val player get() = binding.player
|
||||
|
||||
internal val playerControls get() = binding.playerControls
|
||||
|
@ -376,6 +378,11 @@ class PlayerActivity : BaseActivity() {
|
|||
}
|
||||
|
||||
playerIsDestroyed = false
|
||||
|
||||
registerReceiver(
|
||||
headsetReceiver,
|
||||
IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY),
|
||||
)
|
||||
}
|
||||
|
||||
private fun setupPlayerControls() {
|
||||
|
@ -468,15 +475,11 @@ class PlayerActivity : BaseActivity() {
|
|||
setCallback(
|
||||
object : MediaSessionCompat.Callback() {
|
||||
override fun onPlay() {
|
||||
player.paused = false
|
||||
playerControls.toggleControls(isTapped = true)
|
||||
updatePlaybackState()
|
||||
pauseByIntents(false)
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
player.paused = true
|
||||
playerControls.toggleControls()
|
||||
updatePlaybackState(pause = true)
|
||||
pauseByIntents(true)
|
||||
}
|
||||
|
||||
override fun onSkipToPrevious() {
|
||||
|
@ -493,6 +496,20 @@ class PlayerActivity : BaseActivity() {
|
|||
MediaControllerCompat(this, mediaSession).also { mediaController ->
|
||||
MediaControllerCompat.setMediaController(this, mediaController)
|
||||
}
|
||||
|
||||
headsetReceiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
if (intent.action == AudioManager.ACTION_AUDIO_BECOMING_NOISY) {
|
||||
pauseByIntents(true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun pauseByIntents(pause: Boolean) {
|
||||
player.paused = pause
|
||||
playerControls.toggleControls(!pause)
|
||||
updatePlaybackState(pause = pause)
|
||||
}
|
||||
|
||||
private fun updatePlaybackState(cachePause: Boolean = false, pause: Boolean = false) {
|
||||
|
@ -590,6 +607,7 @@ class PlayerActivity : BaseActivity() {
|
|||
override fun onDestroy() {
|
||||
mediaSession.isActive = false
|
||||
mediaSession.release()
|
||||
unregisterReceiver(headsetReceiver)
|
||||
|
||||
playerPreferences.playerVolumeValue().set(fineVolume)
|
||||
playerPreferences.playerBrightnessValue().set(brightness)
|
||||
|
|
Loading…
Reference in a new issue