added comments

This commit is contained in:
Bnyro 2022-06-29 09:29:23 +02:00
parent d5be2600b0
commit 787ed2acc4

View File

@ -482,21 +482,25 @@ class PlayerFragment : Fragment() {
run() run()
} }
// the function is working recursively
private fun initAutoPlay() { private fun initAutoPlay() {
// save related streams for autoplay // save related streams for autoplay
if (autoplay) { if (autoplay) {
// if it's a playlist use the next video // if it's a playlist use the next video
if (playlistId != null) { if (playlistId != null) {
lateinit var playlist: Playlist // var for saving the list in lateinit var playlist: Playlist // var for saving the list in
// runs only the first time when starting a video from a playlist
if (playlistStreamIds.isEmpty()) { if (playlistStreamIds.isEmpty()) {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
// fetch the playlists videos // fetch the playlists videos
playlist = RetrofitInstance.api.getPlaylist(playlistId!!) playlist = RetrofitInstance.api.getPlaylist(playlistId!!)
// save the playlist urls in the array
playlist.relatedStreams?.forEach { video -> playlist.relatedStreams?.forEach { video ->
playlistStreamIds += video.url?.replace("/watch?v=", "")!! playlistStreamIds += video.url?.replace("/watch?v=", "")!!
} }
// restart the function after videos are loaded // save playlistNextPage for usage if video is not contained
playlistNextPage = playlist.nextpage playlistNextPage = playlist.nextpage
// restart the function after videos are loaded
initAutoPlay() initAutoPlay()
} }
} }
@ -510,16 +514,19 @@ class PlayerFragment : Fragment() {
// fetch the next page of the playlist if the video isn't contained // fetch the next page of the playlist if the video isn't contained
} else if (playlistNextPage != null) { } else if (playlistNextPage != null) {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
Log.e(TAG, "fetching next autoplay page list")
RetrofitInstance.api.getPlaylistNextPage(playlistId!!, playlistNextPage!!) RetrofitInstance.api.getPlaylistNextPage(playlistId!!, playlistNextPage!!)
// append all the playlist item urls to the array
playlist.relatedStreams?.forEach { video -> playlist.relatedStreams?.forEach { video ->
playlistStreamIds += video.url?.replace("/watch?v=", "")!! playlistStreamIds += video.url?.replace("/watch?v=", "")!!
} }
// restart the function after videos are loaded // save playlistNextPage for usage if video is not contained
playlistNextPage = playlist.nextpage playlistNextPage = playlist.nextpage
// restart the function after videos are loaded
initAutoPlay() initAutoPlay()
} }
} }
// else: the video must be the last video of the playlist so nothing happens
// if it's not a playlist then use the next related video // if it's not a playlist then use the next related video
} else if (relatedStreams != null && relatedStreams!!.isNotEmpty()) { } else if (relatedStreams != null && relatedStreams!!.isNotEmpty()) {
// save next video from related streams for autoplay // save next video from related streams for autoplay
@ -528,11 +535,16 @@ class PlayerFragment : Fragment() {
} }
} }
// used for autoplay and skipping to next video
private fun playNextVideo() { private fun playNextVideo() {
// check whether there is a new video in the queue
// by making sure that the next and the current video aren't the same
if (videoId != nextStreamId) {
// save the id of the next stream as videoId and load the next video // save the id of the next stream as videoId and load the next video
videoId = nextStreamId videoId = nextStreamId
fetchJsonAndInitPlayer(view!!) fetchJsonAndInitPlayer(view!!)
} }
}
private fun setSponsorBlockPrefs() { private fun setSponsorBlockPrefs() {
sponsorBlockPrefs.sponsorBlockEnabled = sponsorBlockPrefs.sponsorBlockEnabled =