Minor code improvement

This commit is contained in:
Naveen 2023-08-19 14:40:57 +05:30
parent d4aef1c322
commit f268fa137c
No known key found for this signature in database
GPG key ID: 0E155DAD31671DA3
2 changed files with 17 additions and 14 deletions

View file

@ -23,7 +23,6 @@ import com.simplemobiletools.musicplayer.services.playback.player.initializeSess
class PlaybackService : MediaLibraryService() { class PlaybackService : MediaLibraryService() {
internal lateinit var player: SimpleMusicPlayer internal lateinit var player: SimpleMusicPlayer
internal lateinit var playerThread: HandlerThread internal lateinit var playerThread: HandlerThread
internal lateinit var handler: Handler
internal lateinit var mediaSession: MediaLibrarySession internal lateinit var mediaSession: MediaLibrarySession
internal lateinit var mediaItemProvider: MediaItemProvider internal lateinit var mediaItemProvider: MediaItemProvider
@ -33,9 +32,20 @@ class PlaybackService : MediaLibraryService() {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
initializeSessionAndPlayer(handleAudioFocus = true, handleAudioBecomingNoisy = true, skipSilence = config.gaplessPlayback) initializeSessionAndPlayer(handleAudioFocus = true, handleAudioBecomingNoisy = true, skipSilence = config.gaplessPlayback)
mediaItemProvider = MediaItemProvider(this) initializeLibrary()
}
// we may or may not have storage permission at this time override fun onGetSession(controllerInfo: MediaSession.ControllerInfo) = mediaSession
override fun onDestroy() {
super.onDestroy()
releaseMediaSession()
clearListener()
stopSleepTimer()
}
private fun initializeLibrary() {
mediaItemProvider = MediaItemProvider(this)
if (hasPermission(getPermissionToRequest())) { if (hasPermission(getPermissionToRequest())) {
mediaItemProvider.reload() mediaItemProvider.reload()
} else { } else {
@ -43,8 +53,6 @@ class PlaybackService : MediaLibraryService() {
} }
} }
override fun onGetSession(controllerInfo: MediaSession.ControllerInfo) = mediaSession
private fun releaseMediaSession() { private fun releaseMediaSession() {
mediaSession.release() mediaSession.release()
withPlayer { withPlayer {
@ -53,13 +61,6 @@ class PlaybackService : MediaLibraryService() {
} }
} }
override fun onDestroy() {
super.onDestroy()
releaseMediaSession()
clearListener()
stopSleepTimer()
}
fun stopService() { fun stopService() {
withPlayer { withPlayer {
player.pause() player.pause()
@ -70,7 +71,7 @@ class PlaybackService : MediaLibraryService() {
} }
internal fun withPlayer(callback: Player.() -> Unit) { internal fun withPlayer(callback: Player.() -> Unit) {
handler.post { Handler(player.applicationLooper).post {
callback(player) callback(player)
} }
} }
@ -90,8 +91,11 @@ class PlaybackService : MediaLibraryService() {
companion object { companion object {
// Initializing a media controller might take a noticeable amount of time thus we expose current playback info here to keep things as quick as possible. // Initializing a media controller might take a noticeable amount of time thus we expose current playback info here to keep things as quick as possible.
var isPlaying: Boolean = false var isPlaying: Boolean = false
private set
var currentMediaItem: MediaItem? = null var currentMediaItem: MediaItem? = null
private set
var nextMediaItem: MediaItem? = null var nextMediaItem: MediaItem? = null
private set
fun updatePlaybackInfo(player: Player) { fun updatePlaybackInfo(player: Player) {
currentMediaItem = player.currentMediaItem currentMediaItem = player.currentMediaItem

View file

@ -27,7 +27,6 @@ internal fun PlaybackService.initializeSessionAndPlayer(handleAudioFocus: Boolea
// all player operations are handled on a separate thread to avoid slowing down the main thread. // 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() } playerThread = HandlerThread(PLAYER_THREAD, Process.THREAD_PRIORITY_AUDIO).also { it.start() }
player = initializePlayer(handleAudioFocus, handleAudioBecomingNoisy, skipSilence) player = initializePlayer(handleAudioFocus, handleAudioBecomingNoisy, skipSilence)
handler = Handler(player.applicationLooper)
listener = PlayerListener(context = this) listener = PlayerListener(context = this)
mediaSession = MediaLibraryService.MediaLibrarySession.Builder(this, player, getMediaSessionCallback()) mediaSession = MediaLibraryService.MediaLibrarySession.Builder(this, player, getMediaSessionCallback())
.setSessionActivity(getSessionActivityIntent()) .setSessionActivity(getSessionActivityIntent())