Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import androidx.core.os.bundleOf
import androidx.core.os.postDelayed
import androidx.media3.common.C
import androidx.media3.common.ForwardingPlayer
import androidx.media3.common.MediaItem
import androidx.media3.common.MediaMetadata
import androidx.media3.common.PlaybackException
import androidx.media3.common.Player
Expand Down Expand Up @@ -54,7 +53,6 @@ abstract class AbstractPlayerService : MediaLibraryService(), MediaLibrarySessio
var trackSelector: DefaultTrackSelector? = null

lateinit var videoId: String
private set

var isTransitioning = false
private set
Expand Down Expand Up @@ -199,28 +197,26 @@ abstract class AbstractPlayerService : MediaLibraryService(), MediaLibrarySessio
}
}

private fun navigateVideo(videoId: String) {
/**
* Clear the currently playing video and start playing the new provided [videoId]
*
* If overriding, clearing old data must be done BEFORE calling [super.navigateVideo()]!
*/
@CallSuper
open fun navigateVideo(videoId: String) {
updatePlaylistMetadata {
setExtras(bundleOf(IntentData.videoId to videoId))
}

exoPlayer?.clearMediaItems()

setVideoId(videoId)
this.videoId = videoId

CoroutineScope(Dispatchers.IO).launch {
startPlayback()
}
}

/**
* Update the [videoId] to the new videoId and change the playlist metadata
* to reflect that videoId change
*/
protected open fun setVideoId(videoId: String) {
this.videoId = videoId

updatePlaylistMetadata {
setExtras(bundleOf(IntentData.videoId to videoId))
}
}

protected fun updatePlaylistMetadata(updateAction: MediaMetadata.Builder.() -> Unit) {
handler.post {
exoPlayer?.playlistMetadata = MediaMetadata.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,13 @@ open class OfflinePlayerService : AbstractPlayerService() {

PlayingQueue.clear()

val videoId = if (shuffle) {
this.videoId = if (shuffle) {
runBlocking(Dispatchers.IO) {
Database.downloadDao().getAll().filterByTab(downloadTab).randomOrNull()
}?.download?.videoId
} else {
args.getString(IntentData.videoId)
} ?: return
setVideoId(videoId)

exoPlayer?.addListener(playerListener)
trackSelector?.updateParameters {
Expand Down Expand Up @@ -208,11 +207,7 @@ open class OfflinePlayerService : AbstractPlayerService() {
exoPlayer?.seekTo(0)
} else if (PlayerHelper.isAutoPlayEnabled()) {
val nextId = videoId ?: PlayingQueue.getNext() ?: return
setVideoId(nextId)

scope.launch {
startPlayback()
}
navigateVideo(nextId)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ open class OnlinePlayerService : AbstractPlayerService() {
isAudioOnlyPlayer = args.getBoolean(IntentData.audioOnly)

// get the intent arguments
setVideoId(playerData.videoId)
this.videoId = playerData.videoId
playlistId = playerData.playlistId
channelId = playerData.channelId
startTimestampSeconds = playerData.timestamp
Expand Down Expand Up @@ -197,11 +197,7 @@ open class OnlinePlayerService : AbstractPlayerService() {
val nextVideo = nextId ?: PlayingQueue.getNext() ?: return

// play new video on background
setVideoId(nextVideo)

scope.launch {
startPlayback()
}
navigateVideo(nextVideo)
}

/**
Expand Down Expand Up @@ -259,11 +255,11 @@ open class OnlinePlayerService : AbstractPlayerService() {
}
}

override fun setVideoId(videoId: String) {
super.setVideoId(videoId)

override fun navigateVideo(videoId: String) {
this.streams = null
this.sponsorBlockSegments = emptyList()

super.navigateVideo(videoId)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import androidx.core.graphics.drawable.toDrawable
import androidx.core.net.toUri
import androidx.core.os.bundleOf
import androidx.core.os.postDelayed
import androidx.core.view.SoftwareKeyboardControllerCompat
import androidx.core.view.WindowCompat
import androidx.core.view.isGone
import androidx.core.view.isInvisible
Expand All @@ -49,7 +48,6 @@ import androidx.media3.common.MediaItem
import androidx.media3.common.MediaMetadata
import androidx.media3.common.PlaybackException
import androidx.media3.common.Player
import androidx.media3.common.Tracks
import androidx.media3.session.MediaController
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.R
Expand Down Expand Up @@ -82,7 +80,6 @@ import com.github.libretube.helpers.NavBarHelper
import com.github.libretube.helpers.NavigationHelper
import com.github.libretube.helpers.PlayerHelper
import com.github.libretube.helpers.PlayerHelper.getCurrentSegment
import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.helpers.ProxyHelper
import com.github.libretube.helpers.ThemeHelper
import com.github.libretube.helpers.WindowHelper
Expand Down Expand Up @@ -280,7 +277,10 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions {
// check if video has ended, next video is available and autoplay is enabled/the video is part of a played playlist.
if (playbackState == Player.STATE_ENDED) {
playerBackgroundBinding.sbSkipBtn.isGone = true
if (PlayerHelper.isAutoPlayEnabled(playlistId != null) && autoPlayCountdownEnabled) {

// if the current tracks are empty, the player is transitioning at the moment
val isTransitioning = playerController.currentTracks.isEmpty
if (PlayerHelper.isAutoPlayEnabled(playlistId != null) && autoPlayCountdownEnabled && !isTransitioning) {
showAutoPlayCountdown()
} else {
binding.player.showControllerPermanently()
Expand Down
Loading