From f8315438d86fb974a871fee18557ca7a97119428 Mon Sep 17 00:00:00 2001 From: ForgottenUmbrella Date: Mon, 27 Jan 2020 01:41:38 +1100 Subject: [PATCH 1/3] Let ExoPlayer handle video looping seamlessly Now that videos are replacing the inefficient GIF format, gapless loops are important. ExoPlayer's built-in mechanism prebuffers the video to enable this, whereas the current implementation of seeking to the start presents a short but noticeable delay between each loop. Note that this change introduces an incompatibility with current behaviour: due to google/ExoPlayer#6459, certain videos with broken audio tracks that played fine before will no longer start. Disabling the audio track is a workaround to re-enable looping playback, but ExoPlayer does not appear to expose a way to check if the audio track is short enough to produce the bug. --- .../gallery/pro/activities/VideoPlayerActivity.kt | 3 +++ .../simplemobiletools/gallery/pro/fragments/VideoFragment.kt | 3 +++ 2 files changed, 6 insertions(+) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/VideoPlayerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/VideoPlayerActivity.kt index bed9bc018..6d0f4f245 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/VideoPlayerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/VideoPlayerActivity.kt @@ -214,6 +214,9 @@ open class VideoPlayerActivity : SimpleActivity(), SeekBar.OnSeekBarChangeListen mExoPlayer = ExoPlayerFactory.newSimpleInstance(applicationContext).apply { seekParameters = SeekParameters.CLOSEST_SYNC audioStreamType = C.STREAM_TYPE_MUSIC + if (config.loopVideos) { + repeatMode = Player.REPEAT_MODE_ONE + } prepare(audioSource) } initExoPlayerListeners() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/VideoFragment.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/VideoFragment.kt index 92b98f653..162f98c86 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/VideoFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/VideoFragment.kt @@ -317,6 +317,9 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S mExoPlayer = ExoPlayerFactory.newSimpleInstance(context) mExoPlayer!!.seekParameters = SeekParameters.CLOSEST_SYNC + if (mConfig.loopVideos) { + mExoPlayer?.repeatMode = Player.REPEAT_MODE_ONE + } val isContentUri = mMedium.path.startsWith("content://") val uri = if (isContentUri) Uri.parse(mMedium.path) else Uri.fromFile(File(mMedium.path)) From e83db406a9a0f3cc0ea19fe1de447cfacdd7fddb Mon Sep 17 00:00:00 2001 From: ForgottenUmbrella Date: Mon, 27 Jan 2020 02:12:40 +1100 Subject: [PATCH 2/3] Reset progress views on video loop The previous loop method of seeking to the beginning at video completion ensured that the seekbar and current time text were immediately reset. Using ExoPlayer's own implementation of video looping means that `Player.STATE_ENDED` is no longer reached, thus the UI no longer accurately tracks progress. Restore the old behaviour by resetting the views on position discontinuity. --- .../gallery/pro/activities/VideoPlayerActivity.kt | 8 +++++++- .../gallery/pro/fragments/VideoFragment.kt | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/VideoPlayerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/VideoPlayerActivity.kt index 6d0f4f245..6b306ba4c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/VideoPlayerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/VideoPlayerActivity.kt @@ -234,7 +234,13 @@ open class VideoPlayerActivity : SimpleActivity(), SeekBar.OnSeekBarChangeListen override fun onLoadingChanged(isLoading: Boolean) {} - override fun onPositionDiscontinuity(reason: Int) {} + override fun onPositionDiscontinuity(reason: Int) { + // Reset progress views when video loops. + if (reason == Player.DISCONTINUITY_REASON_PERIOD_TRANSITION) { + video_seekbar.progress = 0 + video_curr_time.text = 0.getFormattedDuration() + } + } override fun onRepeatModeChanged(repeatMode: Int) {} diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/VideoFragment.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/VideoFragment.kt index 162f98c86..56deff9a1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/VideoFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/VideoFragment.kt @@ -353,7 +353,13 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S override fun onLoadingChanged(isLoading: Boolean) {} - override fun onPositionDiscontinuity(reason: Int) {} + override fun onPositionDiscontinuity(reason: Int) { + // Reset progress views when video loops. + if (reason == Player.DISCONTINUITY_REASON_PERIOD_TRANSITION) { + mSeekBar.progress = 0 + mCurrTimeView.text = 0.getFormattedDuration() + } + } override fun onRepeatModeChanged(repeatMode: Int) {} From 9877cbaf2cde09f190f4ee15a9f51f1973d091d9 Mon Sep 17 00:00:00 2001 From: ForgottenUmbrella Date: Mon, 27 Jan 2020 02:13:25 +1100 Subject: [PATCH 3/3] Remove unreachable branches in video completion handling It is never the case that `loopVideos == true` in the `videoCompleted` handler, since enabling that preference sets `repeatMode`, which in turn prevents the player from reaching `STATE_ENDED`. Thus, the branches are unreachable. --- .../gallery/pro/activities/VideoPlayerActivity.kt | 10 +++------- .../gallery/pro/fragments/VideoFragment.kt | 10 +++------- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/VideoPlayerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/VideoPlayerActivity.kt index 6b306ba4c..27839af11 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/VideoPlayerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/VideoPlayerActivity.kt @@ -347,13 +347,9 @@ open class VideoPlayerActivity : SimpleActivity(), SeekBar.OnSeekBarChangeListen clearLastVideoSavedProgress() mCurrTime = (mExoPlayer!!.duration / 1000).toInt() - if (config.loopVideos) { - resumeVideo() - } else { - video_seekbar.progress = video_seekbar.max - video_curr_time.text = mDuration.getFormattedDuration() - pauseVideo() - } + video_seekbar.progress = video_seekbar.max + video_curr_time.text = mDuration.getFormattedDuration() + pauseVideo() } private fun didVideoEnd(): Boolean { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/VideoFragment.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/VideoFragment.kt index 56deff9a1..bc7a69b56 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/VideoFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/VideoFragment.kt @@ -681,13 +681,9 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S } mCurrTime = (mExoPlayer!!.duration / 1000).toInt() - if (listener?.videoEnded() == false && mConfig.loopVideos) { - playVideo() - } else { - mSeekBar.progress = mSeekBar.max - mCurrTimeView.text = mDuration.getFormattedDuration() - pauseVideo() - } + mSeekBar.progress = mSeekBar.max + mCurrTimeView.text = mDuration.getFormattedDuration() + pauseVideo() } private fun cleanup() {