From 5c684b6e67015c371260f68ed5a531ee9524ad27 Mon Sep 17 00:00:00 2001 From: Antoine POPINEAU Date: Sun, 12 Jul 2020 20:46:33 +0200 Subject: [PATCH] Fixed audio focus stealing. --- .../otter/playback/MediaControlsManager.kt | 3 - .../apognu/otter/playback/PlayerService.kt | 72 +++++++++++-------- 2 files changed, 43 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/com/github/apognu/otter/playback/MediaControlsManager.kt b/app/src/main/java/com/github/apognu/otter/playback/MediaControlsManager.kt index e53d201..b42d02d 100644 --- a/app/src/main/java/com/github/apognu/otter/playback/MediaControlsManager.kt +++ b/app/src/main/java/com/github/apognu/otter/playback/MediaControlsManager.kt @@ -14,7 +14,6 @@ import com.github.apognu.otter.R import com.github.apognu.otter.activities.MainActivity import com.github.apognu.otter.utils.AppContext import com.github.apognu.otter.utils.Track -import com.github.apognu.otter.utils.log import com.github.apognu.otter.utils.maybeNormalizeUrl import com.squareup.picasso.Picasso import kotlinx.coroutines.CoroutineScope @@ -29,8 +28,6 @@ class MediaControlsManager(val context: Service, private val scope: CoroutineSco private var notification: Notification? = null fun updateNotification(track: Track?, playing: Boolean) { - "updateNotification".log() - if (notification == null && !playing) return track?.let { diff --git a/app/src/main/java/com/github/apognu/otter/playback/PlayerService.kt b/app/src/main/java/com/github/apognu/otter/playback/PlayerService.kt index 903aac0..c946c15 100644 --- a/app/src/main/java/com/github/apognu/otter/playback/PlayerService.kt +++ b/app/src/main/java/com/github/apognu/otter/playback/PlayerService.kt @@ -12,6 +12,7 @@ import android.media.MediaMetadata import android.os.Build import android.os.IBinder import android.support.v4.media.MediaMetadataCompat +import android.view.KeyEvent import androidx.core.app.NotificationManagerCompat import androidx.media.session.MediaButtonReceiver import com.github.apognu.otter.Otter @@ -58,7 +59,15 @@ class PlayerService : Service() { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { intent?.action?.let { if (it == Intent.ACTION_MEDIA_BUTTON) { - MediaButtonReceiver.handleIntent(Otter.get().mediaSession.session, intent) + intent.extras?.getParcelable(Intent.EXTRA_KEY_EVENT)?.let { key -> + when (key.keyCode) { + KeyEvent.KEYCODE_MEDIA_PLAY, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE -> { + if (hasAudioFocus(true)) MediaButtonReceiver.handleIntent(Otter.get().mediaSession.session, intent) + Unit + } + else -> MediaButtonReceiver.handleIntent(Otter.get().mediaSession.session, intent) + } + } } } @@ -71,6 +80,7 @@ class PlayerService : Service() { return START_STICKY } + @SuppressLint("NewApi") override fun onCreate() { super.onCreate() @@ -79,7 +89,7 @@ class PlayerService : Service() { audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + Build.VERSION_CODES.O.onApi { audioFocusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN).run { setAudioAttributes(AudioAttributes.Builder().run { setUsage(AudioAttributes.USAGE_MEDIA) @@ -253,7 +263,6 @@ class PlayerService : Service() { super.onDestroy() } - @SuppressLint("NewApi") private fun setPlaybackState(state: Boolean) { if (!state) { val (progress, _, _) = getProgress() @@ -265,32 +274,7 @@ class PlayerService : Service() { player.prepare(queue.datasources) } - var allowed = !state - - if (!allowed) { - Build.VERSION_CODES.O.onApi( - { - audioFocusRequest?.let { - allowed = when (audioManager.requestAudioFocus(it)) { - AudioManager.AUDIOFOCUS_REQUEST_GRANTED -> true - else -> false - } - } - }, - { - - @Suppress("DEPRECATION") - audioManager.requestAudioFocus(audioFocusChangeListener, AudioAttributes.CONTENT_TYPE_MUSIC, AudioManager.AUDIOFOCUS_GAIN).let { - allowed = when (it) { - AudioManager.AUDIOFOCUS_REQUEST_GRANTED -> true - else -> false - } - } - } - ) - } - - if (allowed) { + if (hasAudioFocus(state)) { player.playWhenReady = state EventBus.send(Event.StateChanged(state)) @@ -358,6 +342,36 @@ class PlayerService : Service() { return mediaMetadataBuilder.build() } + @SuppressLint("NewApi") + private fun hasAudioFocus(state: Boolean): Boolean { + var allowed = !state + + if (!allowed) { + Build.VERSION_CODES.O.onApi( + { + audioFocusRequest?.let { + allowed = when (audioManager.requestAudioFocus(it)) { + AudioManager.AUDIOFOCUS_REQUEST_GRANTED -> true + else -> false + } + } + }, + { + + @Suppress("DEPRECATION") + audioManager.requestAudioFocus(audioFocusChangeListener, AudioAttributes.CONTENT_TYPE_MUSIC, AudioManager.AUDIOFOCUS_GAIN).let { + allowed = when (it) { + AudioManager.AUDIOFOCUS_REQUEST_GRANTED -> true + else -> false + } + } + } + ) + } + + return allowed + } + @SuppressLint("NewApi") inner class PlayerEventListener : Player.EventListener { override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {