Fix issues with the video duration using SponsorBlock

This commit is contained in:
Bnyro 2022-12-27 19:14:25 +01:00
parent 0f72df6e70
commit f1afd1d13a
3 changed files with 30 additions and 19 deletions

View File

@ -7,6 +7,7 @@ import android.media.session.PlaybackState
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.text.format.DateUtils
import android.view.View
import android.view.WindowManager
import androidx.activity.viewModels
@ -23,6 +24,7 @@ import com.github.libretube.util.DownloadHelper
import com.github.libretube.util.PlayerHelper
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.source.MergingMediaSource
import com.google.android.exoplayer2.source.ProgressiveMediaSource
import com.google.android.exoplayer2.ui.StyledPlayerView
@ -58,7 +60,17 @@ class OfflinePlayerActivity : BaseActivity() {
private fun initializePlayer() {
player = ExoPlayer.Builder(this)
.setHandleAudioBecomingNoisy(true)
.build()
.build().apply {
addListener(object : Player.Listener {
override fun onEvents(player: Player, events: Player.Events) {
super.onEvents(player, events)
// update the displayed duration on changes
playerBinding.duration.text = DateUtils.formatElapsedTime(
player.duration / 1000
)
}
})
}
playerView = binding.player

View File

@ -859,6 +859,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
}
override fun onEvents(player: Player, events: Player.Events) {
updateDisplayedDuration()
super.onEvents(player, events)
if (events.containsAny(
Player.EVENT_PLAYBACK_STATE_CHANGED,
@ -871,8 +872,6 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
}
override fun onPlaybackStateChanged(playbackState: Int) {
updateDisplayedDuration()
exoPlayerView.keepScreenOn = !(
playbackState == Player.STATE_IDLE ||
playbackState == Player.STATE_ENDED
@ -992,22 +991,21 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
*/
@SuppressLint("SetTextI18n")
private fun updateDisplayedDuration() {
if (exoPlayer.duration == Long.MAX_VALUE || isLive) return
val durationWithSponsorBlock = if (
!this::segmentData.isInitialized || this.segmentData.segments.isEmpty()
) {
null
} else {
val difference = segmentData.segments.sumOf {
it.segment[1] - it.segment[0]
}.toInt()
DateUtils.formatElapsedTime(exoPlayer.duration.div(1000) - difference)
}
if (exoPlayer.duration < 0 || isLive) return
playerBinding.duration.text = DateUtils.formatElapsedTime(
exoPlayer.duration.div(1000)
) + if (durationWithSponsorBlock != null) " ($durationWithSponsorBlock)" else ""
)
if (!this::segmentData.isInitialized || this.segmentData.segments.isEmpty()) {
return
}
val durationWithSb = DateUtils.formatElapsedTime(
exoPlayer.duration.div(1000) - segmentData.segments.sumOf {
it.segment[1] - it.segment[0]
}.toInt()
)
playerBinding.duration.text = playerBinding.duration.text.toString() + " ($durationWithSb)"
}
private fun syncQueueButtons() {

View File

@ -227,8 +227,8 @@
<TextView
android:id="@+id/liveDiff"
style="@style/TimeString"
tools:text="05:10"
android:visibility="gone" />
android:visibility="gone"
tools:text="05:10" />
<TextView
android:id="@+id/time_separator"
@ -239,7 +239,8 @@
<TextView
android:id="@+id/duration"
style="@style/TimeString"
tools:text="00:00" />
android:text="00:00"
tools:ignore="HardcodedText" />
</LinearLayout>