mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-27 23:40:33 +05:30
final touches
This commit is contained in:
parent
103376eb6b
commit
ffb3dd6743
@ -716,44 +716,39 @@ class PlayerFragment : BaseFragment() {
|
||||
}
|
||||
|
||||
private fun playVideo() {
|
||||
fun run() {
|
||||
lifecycleScope.launchWhenCreated {
|
||||
streams = try {
|
||||
RetrofitInstance.api.getStreams(videoId!!)
|
||||
} catch (e: IOException) {
|
||||
println(e)
|
||||
Log.e(TAG, "IOException, you might not have internet connection")
|
||||
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
|
||||
return@launchWhenCreated
|
||||
} catch (e: HttpException) {
|
||||
Log.e(TAG, "HttpException, unexpected response")
|
||||
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
|
||||
return@launchWhenCreated
|
||||
}
|
||||
|
||||
runOnUiThread {
|
||||
// set media sources for the player
|
||||
setResolutionAndSubtitles(streams)
|
||||
prepareExoPlayerView()
|
||||
initializePlayerView(streams)
|
||||
if (!isLive) seekToWatchPosition()
|
||||
exoPlayer.prepare()
|
||||
exoPlayer.play()
|
||||
exoPlayerView.useController = true
|
||||
initializePlayerNotification()
|
||||
if (sponsorBlockEnabled) fetchSponsorBlockSegments()
|
||||
// show comments if related streams disabled
|
||||
if (!relatedStreamsEnabled) toggleComments()
|
||||
// prepare for autoplay
|
||||
if (autoplayEnabled) setNextStream()
|
||||
if (watchHistoryEnabled) {
|
||||
PreferenceHelper.addToWatchHistory(videoId!!, streams)
|
||||
}
|
||||
}
|
||||
Globals.playingQueue += videoId!!
|
||||
lifecycleScope.launchWhenCreated {
|
||||
streams = try {
|
||||
RetrofitInstance.api.getStreams(videoId!!)
|
||||
} catch (e: IOException) {
|
||||
println(e)
|
||||
Log.e(TAG, "IOException, you might not have internet connection")
|
||||
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
|
||||
return@launchWhenCreated
|
||||
} catch (e: HttpException) {
|
||||
Log.e(TAG, "HttpException, unexpected response")
|
||||
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
|
||||
return@launchWhenCreated
|
||||
}
|
||||
|
||||
runOnUiThread {
|
||||
// set media sources for the player
|
||||
setResolutionAndSubtitles(streams)
|
||||
prepareExoPlayerView()
|
||||
initializePlayerView(streams)
|
||||
if (!isLive) seekToWatchPosition()
|
||||
exoPlayer.prepare()
|
||||
exoPlayer.play()
|
||||
exoPlayerView.useController = true
|
||||
initializePlayerNotification()
|
||||
if (sponsorBlockEnabled) fetchSponsorBlockSegments()
|
||||
// show comments if related streams disabled
|
||||
if (!relatedStreamsEnabled) toggleComments()
|
||||
// prepare for autoplay
|
||||
if (autoplayEnabled) setNextStream()
|
||||
if (watchHistoryEnabled) PreferenceHelper.addToWatchHistory(videoId!!, streams)
|
||||
}
|
||||
Globals.playingQueue += videoId!!
|
||||
}
|
||||
run()
|
||||
}
|
||||
|
||||
/**
|
||||
@ -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,17 +17,15 @@ class AutoPlayHelper(
|
||||
currentVideoId: String,
|
||||
relatedStreams: List<StreamItem>
|
||||
): String? {
|
||||
val currentVideoIndex = Globals.playingQueue.indexOf(currentVideoId)
|
||||
return if (Globals.playingQueue.size > currentVideoIndex + 1) {
|
||||
return if (Globals.playingQueue.last() != currentVideoId) {
|
||||
val currentVideoIndex = Globals.playingQueue.indexOf(currentVideoId)
|
||||
Globals.playingQueue[currentVideoIndex + 1]
|
||||
} else if (playlistId == null) getNextTrendingVideoId(
|
||||
currentVideoId,
|
||||
relatedStreams
|
||||
) else {
|
||||
getNextPlaylistVideoId(
|
||||
currentVideoId
|
||||
)
|
||||
}
|
||||
) else getNextPlaylistVideoId(
|
||||
currentVideoId
|
||||
)
|
||||
}
|
||||
|
||||
private fun getNextTrendingVideoId(videoId: String, relatedStreams: List<StreamItem>): String? {
|
||||
@ -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