mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 07:50:31 +05:30
final touches
This commit is contained in:
parent
103376eb6b
commit
ffb3dd6743
@ -716,7 +716,7 @@ class PlayerFragment : BaseFragment() {
|
||||
}
|
||||
|
||||
private fun playVideo() {
|
||||
fun run() {
|
||||
Globals.playingQueue += videoId!!
|
||||
lifecycleScope.launchWhenCreated {
|
||||
streams = try {
|
||||
RetrofitInstance.api.getStreams(videoId!!)
|
||||
@ -746,15 +746,10 @@ class PlayerFragment : BaseFragment() {
|
||||
if (!relatedStreamsEnabled) toggleComments()
|
||||
// prepare for autoplay
|
||||
if (autoplayEnabled) setNextStream()
|
||||
if (watchHistoryEnabled) {
|
||||
PreferenceHelper.addToWatchHistory(videoId!!, streams)
|
||||
if (watchHistoryEnabled) PreferenceHelper.addToWatchHistory(videoId!!, streams)
|
||||
}
|
||||
}
|
||||
}
|
||||
Globals.playingQueue += videoId!!
|
||||
}
|
||||
run()
|
||||
}
|
||||
|
||||
/**
|
||||
* set the videoId of the next stream for autoplay
|
||||
@ -828,6 +823,8 @@ class PlayerFragment : BaseFragment() {
|
||||
private fun playNextVideo() {
|
||||
if (nextStreamId == null) return
|
||||
// check whether there is a new video in the queue
|
||||
val nextQueueVideo = autoPlayHelper.getNextPlayingQueueVideoId(videoId!!)
|
||||
if (nextQueueVideo != null) nextStreamId = nextQueueVideo
|
||||
// by making sure that the next and the current video aren't the same
|
||||
saveWatchPosition()
|
||||
// forces the comments to reload for the new video
|
||||
|
@ -30,7 +30,6 @@ import com.google.android.exoplayer2.ExoPlayer
|
||||
import com.google.android.exoplayer2.MediaItem
|
||||
import com.google.android.exoplayer2.Player
|
||||
import com.google.android.exoplayer2.audio.AudioAttributes
|
||||
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
@ -87,7 +86,12 @@ class BackgroundMode : Service() {
|
||||
private lateinit var autoPlayHelper: AutoPlayHelper
|
||||
|
||||
/**
|
||||
* Setting the required [notification] for running as a foreground service
|
||||
* Autoplay Preference
|
||||
*/
|
||||
private val autoplay = PreferenceHelper.getBoolean(PreferenceKeys.AUTO_PLAY, true)
|
||||
|
||||
/**
|
||||
* Setting the required [Notification] for running as a foreground service
|
||||
*/
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
@ -174,7 +178,7 @@ class BackgroundMode : Service() {
|
||||
|
||||
fetchSponsorBlockSegments()
|
||||
|
||||
setNextStream()
|
||||
if (autoplay) setNextStream()
|
||||
}
|
||||
}
|
||||
|
||||
@ -200,7 +204,6 @@ class BackgroundMode : Service() {
|
||||
override fun onPlaybackStateChanged(@Player.State state: Int) {
|
||||
when (state) {
|
||||
Player.STATE_ENDED -> {
|
||||
val autoplay = PreferenceHelper.getBoolean(PreferenceKeys.AUTO_PLAY, true)
|
||||
if (autoplay) playNextVideo()
|
||||
}
|
||||
Player.STATE_IDLE -> {
|
||||
@ -232,6 +235,8 @@ class BackgroundMode : Service() {
|
||||
*/
|
||||
private fun playNextVideo() {
|
||||
if (nextStreamId == null || nextStreamId == videoId) return
|
||||
val nextQueueVideo = autoPlayHelper.getNextPlayingQueueVideoId(videoId)
|
||||
if (nextQueueVideo != null) nextStreamId = nextQueueVideo
|
||||
|
||||
// play new video on background
|
||||
this.videoId = nextStreamId!!
|
||||
@ -240,8 +245,7 @@ class BackgroundMode : Service() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the [MediaItem] with the [streams] into the [player]. Also creates a [MediaSessionConnector]
|
||||
* with the [mediaSession] and attach it to the [player].
|
||||
* Sets the [MediaItem] with the [streams] into the [player]
|
||||
*/
|
||||
private fun setMediaItem() {
|
||||
streams?.let {
|
||||
|
@ -17,18 +17,16 @@ class AutoPlayHelper(
|
||||
currentVideoId: String,
|
||||
relatedStreams: List<StreamItem>
|
||||
): String? {
|
||||
return if (Globals.playingQueue.last() != currentVideoId) {
|
||||
val currentVideoIndex = Globals.playingQueue.indexOf(currentVideoId)
|
||||
return if (Globals.playingQueue.size > currentVideoIndex + 1) {
|
||||
Globals.playingQueue[currentVideoIndex + 1]
|
||||
} else if (playlistId == null) getNextTrendingVideoId(
|
||||
currentVideoId,
|
||||
relatedStreams
|
||||
) else {
|
||||
getNextPlaylistVideoId(
|
||||
) else getNextPlaylistVideoId(
|
||||
currentVideoId
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun getNextTrendingVideoId(videoId: String, relatedStreams: List<StreamItem>): String? {
|
||||
// don't play a video if it got played before already
|
||||
@ -77,4 +75,13 @@ class AutoPlayHelper(
|
||||
// return null when no nextPage is found
|
||||
return null
|
||||
}
|
||||
|
||||
fun getNextPlayingQueueVideoId(
|
||||
currentVideoId: String
|
||||
): String? {
|
||||
return if (Globals.playingQueue.last() != currentVideoId) {
|
||||
val currentVideoIndex = Globals.playingQueue.indexOf(currentVideoId)
|
||||
Globals.playingQueue[currentVideoIndex + 1]
|
||||
} else null
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user