diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2793b0b3..3e8722b3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Moved most of Playlet logic to Playlet Lib
- Support for multiple caption languages
+#### Fixed
+- Issue where auto-play keeps playing the same videos in a loop
+
## [0.6.0] - 2023-01-10
### Added
- Ability to clear search history from web app
diff --git a/playlet-lib/src/components/VideoPlayer/VideoContainer.bs b/playlet-lib/src/components/VideoPlayer/VideoContainer.bs
new file mode 100644
index 00000000..b46212ba
--- /dev/null
+++ b/playlet-lib/src/components/VideoPlayer/VideoContainer.bs
@@ -0,0 +1,20 @@
+import "pkg:/source/utils/RingBuffer.bs"
+
+function Init()
+ 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
+
+' watchedVideosClear needs at least one arg https://github.com/rokucommunity/brighterscript/blob/master/docs/callfunc-operator.md#callfunc-evaluation-with-no-arguments
+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 f6b05c46..1087b684 100644
--- a/playlet-lib/src/components/VideoPlayer/VideoContainer.xml
+++ b/playlet-lib/src/components/VideoPlayer/VideoContainer.xml
@@ -5,5 +5,8 @@
+
+
+
\ 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 1c33822e..979a6e73 100644
--- a/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs
+++ b/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs
@@ -38,6 +38,7 @@ function OnkeyEvent(key as string, press as boolean) as boolean
end if
end if
if key = "back"
+ m.videoContainer@.watchedVideosClear()
CloseVideo()
return true
end if
@@ -156,6 +157,8 @@ function OnVideoDetailsTaskResults(output as object) as void
m.busySpinner.control = "stop"
m.top.content = contentNode
m.top.control = "play"
+
+ m.videoContainer@.watchedVideosAdd(videoId)
end function
function FindChildNodeOfType(node as object, nodeType as string) as object
@@ -277,11 +280,8 @@ function OnVideoPlayerStateChange() as void
if state = "finished"
metadata = m.top.content?.metadata
- metadata = metadata?.recommendedVideos?.GetEntry(0)
- videoId = metadata?.videoId
- if videoId <> invalid
- PlayVideo(videoId, { metadata: metadata })
- else
+
+ if not PlayNextRecommendedVideo(metadata?.recommendedVideos)
CloseVideo()
end if
end if
@@ -297,6 +297,25 @@ function OnVideoPlayerStateChange() as void
end if
end function
+function PlayNextRecommendedVideo(recommendedVideos as object) as boolean
+ if recommendedVideos = invalid or recommendedVideos.Count() = 0
+ return false
+ end if
+
+ for each metadata in recommendedVideos
+ videoId = metadata?.videoId
+ if videoId = invalid
+ continue for
+ end if
+ if not m.videoContainer@.watchedVideosContain(videoId)
+ PlayVideo(videoId, { metadata: metadata })
+ return true
+ end if
+ end for
+
+ return false
+end function
+
function SetupAnimation()
m.widthAnimation = m.top.findNode("widthAnimation")
m.widthAnimationInterpolator = m.top.findNode("widthAnimationInterpolator")
diff --git a/playlet-lib/src/source/utils/General.bs b/playlet-lib/src/source/utils/General.bs
index 5a2f4073..6d26cc02 100644
--- a/playlet-lib/src/source/utils/General.bs
+++ b/playlet-lib/src/source/utils/General.bs
@@ -1,3 +1,5 @@
+import "pkg:/source/utils/StringUtils.bs"
+
function GetLocalIpAddress() as string
if m.ipAddress = invalid or m.ipAddress = ""
di = CreateObject("roDeviceInfo")
diff --git a/playlet-lib/src/source/utils/RingBuffer.bs b/playlet-lib/src/source/utils/RingBuffer.bs
new file mode 100644
index 00000000..572362ac
--- /dev/null
+++ b/playlet-lib/src/source/utils/RingBuffer.bs
@@ -0,0 +1,22 @@
+import "pkg:/source/utils/General.bs"
+
+class RingBuffer
+ private array
+ private size
+ private index
+
+ function new(size as integer)
+ m.array = CreateObject("roArray", size, false)
+ m.size = size
+ m.index = 0
+ end function
+
+ function Push(tvalue As Dynamic)
+ m.array[m.index] = tvalue
+ m.index = (m.index + 1) MOD m.size
+ end function
+
+ function Contains(tvalue As Dynamic) as boolean
+ return ArrayIndexOf(m.array, tvalue) <> -1
+ end function
+end class