diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/PlaybackService.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/PlaybackService.kt index 07a3b166..88860d74 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/PlaybackService.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/PlaybackService.kt @@ -9,14 +9,13 @@ import androidx.media3.common.* import androidx.media3.common.util.UnstableApi import androidx.media3.session.* import com.simplemobiletools.commons.extensions.hasPermission -import com.simplemobiletools.commons.extensions.toast +import com.simplemobiletools.commons.extensions.showErrorToast import com.simplemobiletools.musicplayer.R import com.simplemobiletools.musicplayer.extensions.* import com.simplemobiletools.musicplayer.helpers.NotificationHelper import com.simplemobiletools.musicplayer.helpers.PlaybackSetting import com.simplemobiletools.musicplayer.helpers.getPermissionToRequest import com.simplemobiletools.musicplayer.playback.library.MediaItemProvider -import com.simplemobiletools.musicplayer.playback.player.PlayerListener import com.simplemobiletools.musicplayer.playback.player.SimpleMusicPlayer import com.simplemobiletools.musicplayer.playback.player.initializeSessionAndPlayer @@ -24,10 +23,10 @@ import com.simplemobiletools.musicplayer.playback.player.initializeSessionAndPla class PlaybackService : MediaLibraryService(), MediaSessionService.Listener { internal lateinit var player: SimpleMusicPlayer internal lateinit var playerThread: HandlerThread + internal lateinit var playerListener: Player.Listener internal lateinit var mediaSession: MediaLibrarySession internal lateinit var mediaItemProvider: MediaItemProvider - internal var listener: PlayerListener? = null internal var currentRoot = "" override fun onCreate() { @@ -68,7 +67,7 @@ class PlaybackService : MediaLibraryService(), MediaSessionService.Listener { private fun releaseMediaSession() { mediaSession.release() withPlayer { - removeListener(listener!!) + removeListener(playerListener) release() } } @@ -108,7 +107,7 @@ class PlaybackService : MediaLibraryService(), MediaSessionService.Listener { * background. */ override fun onForegroundServiceStartNotAllowedException() { - toast(R.string.unknown_error_occurred) + showErrorToast(getString(R.string.unknown_error_occurred)) // todo: show a notification instead. } diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/player/Player.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/player/Player.kt index 2ad24a89..f719e44b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/player/Player.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/player/Player.kt @@ -27,13 +27,13 @@ internal fun PlaybackService.initializeSessionAndPlayer(handleAudioFocus: Boolea // all player operations are handled on a separate thread to avoid slowing down the main thread. playerThread = HandlerThread(PLAYER_THREAD, Process.THREAD_PRIORITY_AUDIO).also { it.start() } player = initializePlayer(handleAudioFocus, handleAudioBecomingNoisy, skipSilence) - listener = PlayerListener(context = this) + playerListener = getPlayerListener() mediaSession = MediaLibraryService.MediaLibrarySession.Builder(this, player, getMediaSessionCallback()) .setSessionActivity(getSessionActivityIntent()) .build() withPlayer { - addListener(listener!!) + addListener(playerListener) updateRepeatMode() setPlaybackSpeed(config.playbackSpeed) shuffleModeEnabled = config.isShuffleEnabled diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/player/PlayerListener.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/player/PlayerListener.kt index 3df21bdb..21360044 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/player/PlayerListener.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/player/PlayerListener.kt @@ -11,9 +11,9 @@ import com.simplemobiletools.musicplayer.playback.PlaybackService import com.simplemobiletools.musicplayer.playback.PlaybackService.Companion.updatePlaybackInfo @UnstableApi -class PlayerListener(private val context: PlaybackService) : Player.Listener { +internal fun PlaybackService.getPlayerListener() = object : Player.Listener { - override fun onPlayerError(error: PlaybackException) = context.toast(R.string.unknown_error_occurred, Toast.LENGTH_LONG) + override fun onPlayerError(error: PlaybackException) = toast(R.string.unknown_error_occurred, Toast.LENGTH_LONG) override fun onEvents(player: Player, events: Player.Events) { if ( @@ -28,8 +28,8 @@ class PlayerListener(private val context: PlaybackService) : Player.Listener { ) ) { updatePlaybackInfo(player) - context.saveCurrentPlaybackInfo() - context.broadcastUpdateWidgetState() + saveCurrentPlaybackInfo() + broadcastUpdateWidgetState() } } }