Convert player listener to an extension as well

This commit is contained in:
Naveen 2023-08-25 14:15:50 +05:30
parent 7f26701507
commit a6818e4084
No known key found for this signature in database
GPG key ID: 0E155DAD31671DA3
3 changed files with 10 additions and 11 deletions

View file

@ -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.
}

View file

@ -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

View file

@ -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()
}
}
}