Last active
May 14, 2017 14:57
-
-
Save q-litzler/075183c981390722c57a972aabbe38f9 to your computer and use it in GitHub Desktop.
ExoPlayer Short Tip 4: Playback Progression
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 { | |
interface ProgressionListener { | |
val interval: Long | |
fun progression(position: Long, duration: Long) | |
} | |
private inner class RunnableProgression(val progressionListener: ProgressionListener) : Runnable { | |
override fun run() { | |
if (player.duration >= 0) { | |
progressionListener.progression(player.currentPosition.coerceAtMost(player.duration), player.duration) | |
} | |
handler.postDelayed(this, progressionListener.interval) | |
} | |
} | |
private val handler = Handler() | |
private val player: ExoPlayer = ExoPlayerFactory.newInstance(...) | |
private val runnableProgressions = mutableListOf<RunnableProgression>() | |
fun addProgressionListeners(vararg progressionListener: ProgressionListener) { | |
progressionListener.forEach { runnableProgressions.add(RunnableProgression(it)) } | |
} | |
fun postRunnableProgressions() { | |
runnableProgressions.forEach { handler.post(it) } | |
} | |
fun removeRunnableProgressions() { | |
runnableProgressions.forEach { handler.removeCallbacks(it) } | |
} | |
override fun start() { | |
... | |
postRunnableProgressions() | |
} | |
override fun pause() { | |
... | |
removeRunnableProgressions() | |
} | |
override fun resume() { | |
... | |
if (exoPlayer.playWhenReady) { | |
postRunnableProgressions() | |
} | |
} | |
override fun stop() { | |
... | |
removeRunnableProgressions() | |
} | |
override fun release() { | |
... | |
removeRunnableProgressions() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment