Skip to content

Instantly share code, notes, and snippets.

@q-litzler
Last active September 28, 2021 05:51
Show Gist options
  • Save q-litzler/5ace47b533c9b6c1c0b5256a96af9ec6 to your computer and use it in GitHub Desktop.
Save q-litzler/5ace47b533c9b6c1c0b5256a96af9ec6 to your computer and use it in GitHub Desktop.
ExoPlayer Short Tip 3: The Android Lifecycle
class Player {
val player: ExoPlayer = ExoPlayerFactory.newInstance(...)
var wasPlaying = player.playWhenReady
fun start() {
wasPlaying = player.playWhenReady
player.playWhenReady = true
}
fun pause() {
wasPlaying = player.playWhenReady
player.playWhenReady = false
}
fun resume() {
player.playWhenReady = wasPlaying
}
fun stop() {
player.stop()
}
fun release() {
player.stop()
player.release()
}
}
class PlayerActivity: AppCompatActivity() {
val player = Player()
override fun onCreate(savedInstanceState: Bundle?) {
player.start()
}
override fun onPause() {
player.pause()
}
override fun onResume() {
player.resume()
}
override fun onDestroy() {
player.release()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment