Fix SponsorBlock segment loop at end of video

This commit is contained in:
Bnyro 2023-02-12 13:27:35 +01:00
parent 9746d5a5fd
commit e4627fd156
3 changed files with 16 additions and 6 deletions

View File

@ -26,6 +26,7 @@ import com.google.android.exoplayer2.PlaybackParameters
import com.google.android.exoplayer2.audio.AudioAttributes import com.google.android.exoplayer2.audio.AudioAttributes
import com.google.android.exoplayer2.ui.CaptionStyleCompat import com.google.android.exoplayer2.ui.CaptionStyleCompat
import com.google.android.exoplayer2.video.VideoSize import com.google.android.exoplayer2.video.VideoSize
import kotlin.math.absoluteValue
import kotlin.math.roundToInt import kotlin.math.roundToInt
object PlayerHelper { object PlayerHelper {
@ -485,10 +486,18 @@ object PlayerHelper {
* @param skipManually Whether the event gets handled by the function caller * @param skipManually Whether the event gets handled by the function caller
* @return If segment found and [skipManually] is true, the end position of the segment in ms, otherwise null * @return If segment found and [skipManually] is true, the end position of the segment in ms, otherwise null
*/ */
fun ExoPlayer.checkForSegments(context: Context, segments: List<Segment>, skipManually: Boolean = false): Long? { fun ExoPlayer.checkForSegments(
segments.forEach { segment -> context: Context,
segments: List<Segment>,
skipManually: Boolean = false
): Long? {
for (segment in segments) {
val segmentStart = (segment.segment[0] * 1000f).toLong() val segmentStart = (segment.segment[0] * 1000f).toLong()
val segmentEnd = (segment.segment[1] * 1000f).toLong() val segmentEnd = (segment.segment[1] * 1000f).toLong()
// avoid seeking to the same segment multiple times, e.g. when the SB segment is at the end of the video
if ((duration - currentPosition).absoluteValue < 500) continue
if (currentPosition in segmentStart until segmentEnd) { if (currentPosition in segmentStart until segmentEnd) {
if (!skipManually) { if (!skipManually) {
if (sponsorBlockNotifications) { if (sponsorBlockNotifications) {

View File

@ -112,6 +112,7 @@ class BackgroundMode : LifecycleService() {
val notification: Notification = Notification.Builder(this, BACKGROUND_CHANNEL_ID) val notification: Notification = Notification.Builder(this, BACKGROUND_CHANNEL_ID)
.setContentTitle(getString(R.string.app_name)) .setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.playingOnBackground)) .setContentText(getString(R.string.playingOnBackground))
.setSmallIcon(R.drawable.ic_launcher_lockscreen)
.build() .build()
startForeground(PLAYER_NOTIFICATION_ID, notification) startForeground(PLAYER_NOTIFICATION_ID, notification)

View File

@ -114,6 +114,10 @@ import com.google.android.exoplayer2.ui.StyledPlayerView
import com.google.android.exoplayer2.upstream.DefaultDataSource import com.google.android.exoplayer2.upstream.DefaultDataSource
import com.google.android.exoplayer2.util.MimeTypes import com.google.android.exoplayer2.util.MimeTypes
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import java.io.IOException
import java.util.*
import java.util.concurrent.Executors
import kotlin.math.abs
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -121,10 +125,6 @@ import kotlinx.coroutines.withContext
import kotlinx.datetime.LocalDate import kotlinx.datetime.LocalDate
import kotlinx.serialization.encodeToString import kotlinx.serialization.encodeToString
import retrofit2.HttpException import retrofit2.HttpException
import java.io.IOException
import java.util.*
import java.util.concurrent.Executors
import kotlin.math.abs
class PlayerFragment : BaseFragment(), OnlinePlayerOptions { class PlayerFragment : BaseFragment(), OnlinePlayerOptions {