Fixed Now playing viewpager lag

This commit is contained in:
Prathamesh More 2022-05-31 23:26:06 +05:30
parent 67d65a1532
commit ad04c4e2ed
2 changed files with 13 additions and 5 deletions

View file

@ -88,6 +88,7 @@ import com.bumptech.glide.request.target.Target
import com.bumptech.glide.request.transition.Transition
import com.google.android.gms.cast.framework.CastSession
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers.Default
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import org.koin.java.KoinJavaComponent.get
@ -792,6 +793,10 @@ class MusicService : MediaBrowserServiceCompat(),
}
fun playSongAt(position: Int) {
// Every chromecast method needs to run on main thread or you are greeted with IllegalStateException
// So it will use Main dispatcher
// And by using Default dispatcher for local playback we are reduce the burden of main thread
serviceScope.launch(if(playbackManager.isLocalPlayback) Default else Main) {
openTrackAndPrepareNextAt(position) { success ->
if (success) {
play()
@ -800,6 +805,7 @@ class MusicService : MediaBrowserServiceCompat(),
}
}
}
}
@Synchronized
fun prepareNextImpl() {

View file

@ -16,6 +16,8 @@ class PlaybackManager(val context: Context) {
var playback: Playback? = null
private var playbackLocation = PlaybackLocation.LOCAL
val isLocalPlayback get() = playbackLocation== PlaybackLocation.LOCAL
val audioSessionId: Int
get() = if (playback != null) {
playback!!.audioSessionId