Created
February 25, 2019 14:50
-
-
Save navi25/eba2ba6d90137c6e0000f8e80b4a6e46 to your computer and use it in GitHub Desktop.
Lifecycle handler for Exoplayer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun saveState(){ | |
if (player != null) { | |
playbackPosition = player?.currentPosition ?: 0L | |
currentWindowIndex = player?.currentWindowIndex ?: 0 | |
playWhenReady = player?.playWhenReady ?: true | |
} | |
} | |
private fun loadState(){ | |
player?.apply { | |
playWhenReady = playWhenReady | |
seekTo(currentWindowIndex, playbackPosition) | |
} | |
} | |
private fun releasePlayer() { | |
if (player != null) { | |
saveState() | |
player?.release() | |
player = null | |
} | |
} | |
//region Handle Lifecycle | |
fun start() { | |
player?.playWhenReady = true | |
player?.playbackState | |
currUri?: play(currUri) | |
currUriList?: play(currUriList) | |
} | |
fun resume(){ | |
player?.playWhenReady = true | |
currUri?: play(currUri) | |
currUriList?: play(currUriList) | |
loadState() | |
} | |
fun pause() { | |
player?.playWhenReady = false | |
player?.playbackState | |
saveState() | |
} | |
fun stop(){ | |
releasePlayer() | |
player?.stop(true) | |
} | |
//endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment