mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 06:10:31 +05:30
Fix issues with the video duration using SponsorBlock
This commit is contained in:
parent
0f72df6e70
commit
f1afd1d13a
@ -7,6 +7,7 @@ import android.media.session.PlaybackState
|
|||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.text.format.DateUtils
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.WindowManager
|
import android.view.WindowManager
|
||||||
import androidx.activity.viewModels
|
import androidx.activity.viewModels
|
||||||
@ -23,6 +24,7 @@ import com.github.libretube.util.DownloadHelper
|
|||||||
import com.github.libretube.util.PlayerHelper
|
import com.github.libretube.util.PlayerHelper
|
||||||
import com.google.android.exoplayer2.ExoPlayer
|
import com.google.android.exoplayer2.ExoPlayer
|
||||||
import com.google.android.exoplayer2.MediaItem
|
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.MergingMediaSource
|
||||||
import com.google.android.exoplayer2.source.ProgressiveMediaSource
|
import com.google.android.exoplayer2.source.ProgressiveMediaSource
|
||||||
import com.google.android.exoplayer2.ui.StyledPlayerView
|
import com.google.android.exoplayer2.ui.StyledPlayerView
|
||||||
@ -58,7 +60,17 @@ class OfflinePlayerActivity : BaseActivity() {
|
|||||||
private fun initializePlayer() {
|
private fun initializePlayer() {
|
||||||
player = ExoPlayer.Builder(this)
|
player = ExoPlayer.Builder(this)
|
||||||
.setHandleAudioBecomingNoisy(true)
|
.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
|
playerView = binding.player
|
||||||
|
|
||||||
|
@ -859,6 +859,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onEvents(player: Player, events: Player.Events) {
|
override fun onEvents(player: Player, events: Player.Events) {
|
||||||
|
updateDisplayedDuration()
|
||||||
super.onEvents(player, events)
|
super.onEvents(player, events)
|
||||||
if (events.containsAny(
|
if (events.containsAny(
|
||||||
Player.EVENT_PLAYBACK_STATE_CHANGED,
|
Player.EVENT_PLAYBACK_STATE_CHANGED,
|
||||||
@ -871,8 +872,6 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onPlaybackStateChanged(playbackState: Int) {
|
override fun onPlaybackStateChanged(playbackState: Int) {
|
||||||
updateDisplayedDuration()
|
|
||||||
|
|
||||||
exoPlayerView.keepScreenOn = !(
|
exoPlayerView.keepScreenOn = !(
|
||||||
playbackState == Player.STATE_IDLE ||
|
playbackState == Player.STATE_IDLE ||
|
||||||
playbackState == Player.STATE_ENDED
|
playbackState == Player.STATE_ENDED
|
||||||
@ -992,22 +991,21 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
|
|||||||
*/
|
*/
|
||||||
@SuppressLint("SetTextI18n")
|
@SuppressLint("SetTextI18n")
|
||||||
private fun updateDisplayedDuration() {
|
private fun updateDisplayedDuration() {
|
||||||
if (exoPlayer.duration == Long.MAX_VALUE || isLive) return
|
if (exoPlayer.duration < 0 || 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
playerBinding.duration.text = DateUtils.formatElapsedTime(
|
playerBinding.duration.text = DateUtils.formatElapsedTime(
|
||||||
exoPlayer.duration.div(1000)
|
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() {
|
private fun syncQueueButtons() {
|
||||||
|
@ -227,8 +227,8 @@
|
|||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/liveDiff"
|
android:id="@+id/liveDiff"
|
||||||
style="@style/TimeString"
|
style="@style/TimeString"
|
||||||
tools:text="05:10"
|
android:visibility="gone"
|
||||||
android:visibility="gone" />
|
tools:text="05:10" />
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/time_separator"
|
android:id="@+id/time_separator"
|
||||||
@ -239,7 +239,8 @@
|
|||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/duration"
|
android:id="@+id/duration"
|
||||||
style="@style/TimeString"
|
style="@style/TimeString"
|
||||||
tools:text="00:00" />
|
android:text="00:00"
|
||||||
|
tools:ignore="HardcodedText" />
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user