Last active
September 28, 2021 05:51
-
-
Save q-litzler/5ace47b533c9b6c1c0b5256a96af9ec6 to your computer and use it in GitHub Desktop.
ExoPlayer Short Tip 3: The Android Lifecycle
This file contains 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
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