Optimize MediaController.play to avoid redundant preparation

Added a check to verify if the requested station is already the current media item and in a valid playback state before re-setting the media item and preparing.
This commit is contained in:
Michatec committed 2026-08-16 10:47:19 +02:00
1 parent ccea722a27
commit e450526692
1 file changed
+9 -4
@@ -2,6 +2,7 @@ package com.michatec.radio.extensions
import android.content.Context
import android.os.Bundle
import androidx.media3.common.Player
import androidx.media3.session.MediaController
import androidx.media3.session.SessionCommand
import androidx.media3.session.SessionResult
@@ -54,10 +55,14 @@ fun MediaController.requestVisualizerData(): ListenableFuture<SessionResult> {
/* Starts playback with a new media item */
fun MediaController.play(context: Context, station: Station) {
// set media item, prepare and play
setMediaItem(CollectionHelper.buildMediaItem(context, station))
prepare()
play()
if (currentMediaItem?.mediaId == station.uuid && playbackState != Player.STATE_IDLE && playbackState != Player.STATE_ENDED) {
play()
} else {
// set media item, prepare and play
setMediaItem(CollectionHelper.buildMediaItem(context, station))
prepare()
play()
}
}