release player in PlayerVIewModel onCleared()

this fixes playback after the activity has been destroyed, while being in background
This commit is contained in:
Jannik 2020-12-27 20:31:18 +01:00
parent 7845770067
commit b3ce43c614
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
2 changed files with 10 additions and 3 deletions

View File

@ -118,6 +118,7 @@ class PlayerActivity : AppCompatActivity() {
initTimeUpdates()
}
// TODO if view model was not destroyed, don't start a new media
private fun initExoPlayer() {
controller = video_view.findViewById(R.id.exo_controller)
controller.isAnimationEnabled = false // disable controls (time-bar) animation
@ -222,10 +223,8 @@ class PlayerActivity : AppCompatActivity() {
playbackPosition = model.player.currentPosition
currentWindow = model.player.currentWindowIndex
playWhenReady = model.player.playWhenReady
model.player.release()
model.player.pause()
timerUpdates.cancel()
Log.d(javaClass.name, "Released player")
}
/**

View File

@ -2,6 +2,7 @@ package org.mosad.teapod.player
import android.app.Application
import android.net.Uri
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import com.google.android.exoplayer2.C
import com.google.android.exoplayer2.MediaItem
@ -42,6 +43,13 @@ class PlayerViewModel(application: Application) : AndroidViewModel(application)
var currentLanguage: Locale = Locale.ROOT
internal set
override fun onCleared() {
super.onCleared()
player.release()
Log.d(javaClass.name, "Released player")
}
fun loadMedia(mediaId: Int, episodeId: Int) {
runBlocking {
media = AoDParser.getMediaById(mediaId)