Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit dbd8eb9a authored by Sebastiano Barezzi's avatar Sebastiano Barezzi Committed by Luca Stefani
Browse files

Twelve: Let ExoPlayer generate an audio session ID for us

There's a funny race condition where AudioManager will refuse
to generate one because DefaultAudioSink is already doing that,
ending up with a `-1` ID, killing playback altogether.

Change-Id: I34b7e989a84bb6598658aa1d4105a43494081153
parent 12701a5d
Loading
Loading
Loading
Loading
+7 −11
Original line number Diff line number Diff line
@@ -26,7 +26,6 @@ import androidx.media3.common.Player
import androidx.media3.common.Rating
import androidx.media3.common.listen
import androidx.media3.common.util.UnstableApi
import androidx.media3.common.util.Util
import androidx.media3.exoplayer.DefaultLoadControl
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.session.CommandButton
@@ -194,10 +193,6 @@ class PlaybackService : MediaLibraryService(), LifecycleOwner {
        (application as TwelveApplication).providersRepository
    }

    private val audioSessionId by lazy {
        Util.generateAudioSessionIdV21(this)
    }

    private val mediaLibrarySessionCallback = object : MediaLibrarySession.Callback {
        override fun onConnect(
            session: MediaSession,
@@ -381,7 +376,7 @@ class PlaybackService : MediaLibraryService(), LifecycleOwner {
                CustomCommand.GET_AUDIO_SESSION_ID -> {
                    SessionResult(
                        SessionResult.RESULT_SUCCESS,
                        bundleOf(CustomCommand.RSP_VALUE to audioSessionId),
                        bundleOf(CustomCommand.RSP_VALUE to player.audioSessionId),
                    )
                }

@@ -440,11 +435,8 @@ class PlaybackService : MediaLibraryService(), LifecycleOwner {
            .build()
            .apply {
                setOffloadEnabled(sharedPreferences.enableOffload)
                audioSessionId = this@PlaybackService.audioSessionId
            }

        openAudioEffectSession()

        mediaLibrarySession = MediaLibrarySession.Builder(
            this, player, mediaLibrarySessionCallback
        )
@@ -499,6 +491,10 @@ class PlaybackService : MediaLibraryService(), LifecycleOwner {
                ) {
                    mediaLibrarySession.setCustomLayout(getCustomLayout())
                }

                if (events.contains(Player.EVENT_AUDIO_SESSION_ID)) {
                    openAudioEffectSession()
                }
            }
        }
    }
@@ -560,7 +556,7 @@ class PlaybackService : MediaLibraryService(), LifecycleOwner {
    private fun openAudioEffectSession() {
        Intent(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION).apply {
            putExtra(AudioEffect.EXTRA_PACKAGE_NAME, application.packageName)
            putExtra(AudioEffect.EXTRA_AUDIO_SESSION, audioSessionId)
            putExtra(AudioEffect.EXTRA_AUDIO_SESSION, player.audioSessionId)
            putExtra(AudioEffect.EXTRA_CONTENT_TYPE, AudioEffect.CONTENT_TYPE_MUSIC)
            sendBroadcast(this)
        }
@@ -569,7 +565,7 @@ class PlaybackService : MediaLibraryService(), LifecycleOwner {
    private fun closeAudioEffectSession() {
        Intent(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION).apply {
            putExtra(AudioEffect.EXTRA_PACKAGE_NAME, application.packageName)
            putExtra(AudioEffect.EXTRA_AUDIO_SESSION, audioSessionId)
            putExtra(AudioEffect.EXTRA_AUDIO_SESSION, player.audioSessionId)
            putExtra(AudioEffect.EXTRA_CONTENT_TYPE, AudioEffect.CONTENT_TYPE_MUSIC)
            sendBroadcast(this)
        }