loop videos after ending if desired

This commit is contained in:
tibbi 2019-01-02 18:32:03 +01:00
parent 677e68332c
commit 7d758178d5

View file

@ -242,7 +242,7 @@ open class VideoPlayerActivity : SimpleActivity(), SeekBar.OnSeekBarChangeListen
return return
} }
val wasEnded = videoEnded() val wasEnded = didVideoEnd()
if (wasEnded) { if (wasEnded) {
setPosition(0) setPosition(0)
} }
@ -259,7 +259,7 @@ open class VideoPlayerActivity : SimpleActivity(), SeekBar.OnSeekBarChangeListen
} }
mIsPlaying = false mIsPlaying = false
if (!videoEnded()) { if (!didVideoEnd()) {
mExoPlayer?.playWhenReady = false mExoPlayer?.playWhenReady = false
} }
@ -288,17 +288,29 @@ open class VideoPlayerActivity : SimpleActivity(), SeekBar.OnSeekBarChangeListen
} }
private fun videoCompleted() { private fun videoCompleted() {
if (mExoPlayer == null) {
return
} }
private fun videoEnded(): Boolean { clearLastVideoSavedProgress()
mCurrTime = (mExoPlayer!!.duration / 1000).toInt()
if (config.loopVideos) {
playVideo()
} else {
video_seekbar.progress = video_seekbar.max
video_curr_time.text = mDuration.getFormattedDuration()
pauseVideo()
}
}
private fun didVideoEnd(): Boolean {
val currentPos = mExoPlayer?.currentPosition ?: 0 val currentPos = mExoPlayer?.currentPosition ?: 0
val duration = mExoPlayer?.duration ?: 0 val duration = mExoPlayer?.duration ?: 0
return currentPos != 0L && currentPos >= duration return currentPos != 0L && currentPos >= duration
} }
private fun saveVideoProgress() { private fun saveVideoProgress() {
if (!videoEnded()) { if (!didVideoEnd()) {
config.apply { config.apply {
lastVideoPosition = mExoPlayer!!.currentPosition.toInt() / 1000 lastVideoPosition = mExoPlayer!!.currentPosition.toInt() / 1000
lastVideoPath = mUri.toString() lastVideoPath = mUri.toString()
@ -306,6 +318,13 @@ open class VideoPlayerActivity : SimpleActivity(), SeekBar.OnSeekBarChangeListen
} }
} }
private fun clearLastVideoSavedProgress() {
config.apply {
lastVideoPosition = 0
lastVideoPath = ""
}
}
private fun setVideoSize() { private fun setVideoSize() {
val videoProportion = mVideoSize.x.toFloat() / mVideoSize.y.toFloat() val videoProportion = mVideoSize.x.toFloat() / mVideoSize.y.toFloat()
val display = windowManager.defaultDisplay val display = windowManager.defaultDisplay