From 2ba2115da71a2d3c518404095bf25a5296edbe23 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Fri, 15 Dec 2023 21:27:41 -0500 Subject: [PATCH] (Regression) Fix issue where auto-play keeps playing the same videos in a loop --- CHANGELOG.md | 4 ++++ .../src/components/PlayQueue/PlayQueue.bs | 23 ++++++++++++++++-- .../src/components/PlayQueue/PlayQueue.xml | 2 ++ .../components/VideoPlayer/VideoContainer.bs | 24 ------------------- .../components/VideoPlayer/VideoContainer.xml | 3 --- .../src/components/VideoPlayer/VideoPlayer.bs | 10 ++++---- 6 files changed, 32 insertions(+), 34 deletions(-) delete mode 100644 playlet-lib/src/components/VideoPlayer/VideoContainer.bs diff --git a/CHANGELOG.md b/CHANGELOG.md index 18c23b19..8677b2db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- (Regression) Issue where auto-play keeps playing the same videos in a loop + ### Removed - Xmas theme logo - can't publish it before end of year... ([link](https://blog.roku.com/developer/channel-store-blackouts-2023)) diff --git a/playlet-lib/src/components/PlayQueue/PlayQueue.bs b/playlet-lib/src/components/PlayQueue/PlayQueue.bs index fef65c1d..fa489591 100644 --- a/playlet-lib/src/components/PlayQueue/PlayQueue.bs +++ b/playlet-lib/src/components/PlayQueue/PlayQueue.bs @@ -8,6 +8,7 @@ import "pkg:/source/asyncTask/asyncTask.bs" import "pkg:/source/utils/ArrayUtils.bs" import "pkg:/source/utils/ErrorUtils.bs" import "pkg:/source/utils/Logging.bs" +import "pkg:/source/utils/RingBuffer.bs" import "pkg:/source/utils/Types.bs" ' TODO:P1 prevent adding duplicates of the same video/playlist @@ -17,6 +18,8 @@ function Init() m.top.content = m.content m.content.addField("index", "integer", false) m.content.index = m.top.index + ' TODO:P2 use actual queue nodes instead of internal buffer + m.watchedVideos = new RingBuffer(100) end function function Play(node as object, playlistIndex as integer) @@ -138,7 +141,7 @@ function PlayItemAtCurrentIndex() as boolean return false end if if node.type = "video" - VideoUtils.PlayVideo(node) + PlayVideo(node) return true else if node.type = "playlist" if m.top.playlistIndex = -1 @@ -162,7 +165,7 @@ function PlayPlaylistAtCurrentIndex(playlist as object) as boolean if playlistIndex < playlist.getChildCount() child = playlist.getChild(playlistIndex) - VideoUtils.PlayVideo(child) + PlayVideo(child) return true end if @@ -170,6 +173,14 @@ function PlayPlaylistAtCurrentIndex(playlist as object) as boolean return true end function +function PlayVideo(node as object) + videoId = node.videoId + if not m.watchedVideos.Contains(videoId) + m.watchedVideos.Push(videoId) + end if + VideoUtils.PlayVideo(node) +end function + function LoadPlaylist(playlist as object) as void if playlist.loadState = FeedLoadState.Loading return @@ -219,3 +230,11 @@ function OnIndexChange() child.index = index end for end function + +function watchedVideosContain(videoId as string) as boolean + return m.watchedVideos.Contains(videoId) +end function + +function watchedVideosClear(unused as dynamic) + m.watchedVideos = new RingBuffer(100) +end function diff --git a/playlet-lib/src/components/PlayQueue/PlayQueue.xml b/playlet-lib/src/components/PlayQueue/PlayQueue.xml index dddeb5f8..1d3f7afd 100644 --- a/playlet-lib/src/components/PlayQueue/PlayQueue.xml +++ b/playlet-lib/src/components/PlayQueue/PlayQueue.xml @@ -12,6 +12,8 @@ + + diff --git a/playlet-lib/src/components/VideoPlayer/VideoContainer.bs b/playlet-lib/src/components/VideoPlayer/VideoContainer.bs deleted file mode 100644 index c2770c11..00000000 --- a/playlet-lib/src/components/VideoPlayer/VideoContainer.bs +++ /dev/null @@ -1,24 +0,0 @@ -import "pkg:/source/utils/RingBuffer.bs" - -function Init() - ' We keep in memory the last videos we watched, to avoid a loop - ' Sometimes recommended videos point to each other, and we're stuck watching - ' The same 2 videos over and over again - - ' TODO:P1 get rid of `watchedVideos` and use the PlayQueue instead - m.watchedVideos = new RingBuffer(100) -end function - -function watchedVideosContain(videoId as string) as boolean - return m.watchedVideos.Contains(videoId) -end function - -function watchedVideosAdd(videoId as string) - if not watchedVideosContain(videoId) - m.watchedVideos.Push(videoId) - end if -end function - -function watchedVideosClear(unused as dynamic) - m.watchedVideos = new RingBuffer(100) -end function diff --git a/playlet-lib/src/components/VideoPlayer/VideoContainer.xml b/playlet-lib/src/components/VideoPlayer/VideoContainer.xml index 43ba7990..2dc94baf 100644 --- a/playlet-lib/src/components/VideoPlayer/VideoContainer.xml +++ b/playlet-lib/src/components/VideoPlayer/VideoContainer.xml @@ -1,8 +1,5 @@ - - - \ No newline at end of file diff --git a/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs b/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs index 2c722619..2db5ca42 100644 --- a/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs +++ b/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs @@ -186,20 +186,20 @@ function OnVideoPlayerStateChange() as void if state = "finished" if content = invalid - m.container@.watchedVideosClear() + m.playQueue@.watchedVideosClear() VideoUtils.CloseVideo() return end if metadata = content.metadata if metadata = invalid - m.container@.watchedVideosClear() + m.playQueue@.watchedVideosClear() VideoUtils.CloseVideo() return end if if not PlayNext(metadata.recommendedVideos) - m.container@.watchedVideosClear() + m.playQueue@.watchedVideosClear() VideoUtils.CloseVideo() end if end if @@ -251,7 +251,7 @@ function PlayNextRecommendedVideo(recommendedVideos as object) as boolean if videoId = invalid continue for end if - if not m.container@.watchedVideosContain(videoId) + if not m.playQueue@.watchedVideosContain(videoId) LogInfo(`Playing next recommended video (${videoId})`) instance = m.invidious@.GetCurrentInstance() node = InvidiousContent.ToVideoContentNode(invalid, metadata, instance) @@ -277,7 +277,7 @@ function OnkeyEvent(key as string, press as boolean) as boolean end if if key = "back" - m.container@.watchedVideosClear() + m.playQueue@.watchedVideosClear() VideoUtils.CloseVideo() return true end if