use a separate scope to update playheads

viewModelScope will be cleard when the activity is stopped, but the playhead update should be done anyway

fixes #62
This commit is contained in:
Jannik 2022-07-10 13:50:53 +02:00
parent 522b893dc8
commit 8eb737a831
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
2 changed files with 3 additions and 7 deletions

View File

@ -85,8 +85,6 @@ class PlayerActivity : AppCompatActivity() {
hideBars() // Initial hide the bars
playerBinding = ActivityPlayerBinding.bind(findViewById(R.id.player_root))
println(findViewById(R.id.player_controls_root))
controlsBinding = PlayerControlsBinding.bind(findViewById(R.id.player_controls_root))
model.loadMediaAsync(

View File

@ -32,10 +32,7 @@ import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.*
import org.mosad.teapod.R
import org.mosad.teapod.parser.crunchyroll.*
import org.mosad.teapod.preferences.Preferences
@ -276,7 +273,8 @@ class PlayerViewModel(application: Application) : AndroidViewModel(application)
val playhead = (player.currentPosition / 1000)
if (playhead > 0 && Preferences.updatePlayhead) {
viewModelScope.launch { Crunchyroll.postPlayheads(currentEpisode.id, playhead.toInt()) }
// don't use viewModelScope here. This task may needs to finish, when ViewModel will be cleared
CoroutineScope(Dispatchers.IO).launch { Crunchyroll.postPlayheads(currentEpisode.id, playhead.toInt()) }
Log.i(javaClass.name, "Set playhead for episode ${currentEpisode.id} to $playhead sec.")
}