From 27fc34338dd67bcca9c9a7964c326d1748baf019 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Fri, 5 Jul 2024 21:38:45 -0400 Subject: [PATCH] Basic state machine for dev player --- .../src/components/VideoPlayerDev/PlayerUi.bs | 147 ++++++++++++++++++ .../VideoPlayerDev/VideoPlayerDev.bs | 128 ++++----------- .../VideoPlayerDev/VideoPlayerDev.xml | 13 +- 3 files changed, 188 insertions(+), 100 deletions(-) create mode 100644 playlet-lib/src/components/VideoPlayerDev/PlayerUi.bs diff --git a/playlet-lib/src/components/VideoPlayerDev/PlayerUi.bs b/playlet-lib/src/components/VideoPlayerDev/PlayerUi.bs new file mode 100644 index 00000000..6ced1e3b --- /dev/null +++ b/playlet-lib/src/components/VideoPlayerDev/PlayerUi.bs @@ -0,0 +1,147 @@ +enum PlayerUiState + None ' None is not a valid state, but it is the default value pre-initialization + FadingIn + Visible + FadingOut + Hidden +end enum + +function InitializePlayerUi() + m.top.enableUI = false + m.top.enableTrickPlay = false + + FindUiNodes() + SetupLoadingSpinner() + + DisposableObserveFieldScoped(m.uiFadeAnimation, "state", FuncName(OnUiFadeAnimationStateChange)) + DisposableObserveField(m.hideVideoUiTimer, "fire", FuncName(OnHideVideoUiTimer)) + + DisposableObserveField(m.top, "playerUiState", FuncName(OnPlayerUiState)) + + m.top.playerUiState = PlayerUiState.Visible +end function + +function FindUiNodes() + m.clock = m.top.findNode("Clock") + m.titleLabel = m.top.findNode("TitleLabel") + m.secondaryTitleLabel = m.top.findNode("SecondaryTitleLabel") + m.backgroundPoster = m.top.findNode("BackgroundPoster") + m.backgroundPosterSmall = m.top.findNode("BackgroundPosterSmall") + m.previousButton = m.top.findNode("PreviousButton") + m.playButton = m.top.findNode("PlayButton") + m.nextButton = m.top.findNode("NextButton") + m.minimizeButton = m.top.findNode("MinimizeButton") + m.trickPlayBar = m.top.findNode("TrickPlayBar") + m.videoUi = m.top.findNode("VideoUi") + m.progressTimer = m.top.findNode("ProgressTimer") + m.hideVideoUiTimer = m.top.findNode("HideVideoUiTimer") + m.uiFadeAnimation = m.top.findNode("UiFadeAnimation") + m.uiFadeOpacityInterpolator = m.top.findNode("UiFadeOpacityInterpolator") + m.loadingUi = m.top.findNode("LoadingUi") + m.loadingSpinnerBackground = m.loadingUi.findNode("LoadingSpinnerBackground") + m.loadingSpinner = m.loadingUi.findNode("LoadingSpinner") + m.loadingLabel = m.loadingSpinner.findNode("LoadingLabel") + m.loadingPercentLabel = m.loadingSpinner.findNode("LoadingPercentLabel") +end function + +function SetupLoadingSpinner() + poster = m.loadingSpinner.poster + + size = 150 + poster.width = size + poster.height = size + centerx = (1280 - size) / 2 + centery = (720 - size) / 2 + m.loadingSpinner.translation = [centerx, centery] + poster.opacity = 0.8 + poster.uri = "pkg:/images/spinner.png" + + size = 80 + centerx = (1280 - size) / 2 + centery = (720 - size) / 2 + m.loadingSpinnerBackground.translation = [centerx, centery] +end function + +function OnPlayerUiState() + state = m.top.playerUiState + + if state = PlayerUiState.FadingIn + OnPlayerUiFadingIn() + else if state = PlayerUiState.Visible + OnPlayerUiVisible() + else if state = PlayerUiState.FadingOut + OnPlayerUiFadingOut() + else if state = PlayerUiState.Hidden + OnPlayerUiHidden() + end if +end function + +function OnPlayerUiFadingIn() + if m.top.hasFocus() + NodeSetFocus(m.playButton, true) + end if + + m.uiFadeOpacityInterpolator.reverse = false + m.uiFadeAnimation.control = "start" +end function + +function OnPlayerUiVisible() + if m.top.hasFocus() + NodeSetFocus(m.playButton, true) + end if + + StartHideVideoUiTimer() +end function + +function OnPlayerUiFadingOut() + m.uiFadeOpacityInterpolator.reverse = true + m.uiFadeAnimation.control = "start" +end function + +function OnPlayerUiHidden() + m.videoUi.opacity = 0 + + if m.top.isInFocusChain() and not m.top.hasFocus() + NodeSetFocus(m.top, false) + NodeSetFocus(m.top, true) + end if +end function + +function StartHideVideoUiTimer() + m.hideVideoUiTimer.control = "stop" + m.hideVideoUiTimer.control = "start" +end function + +function StopHideVideoUiTimer() + m.hideVideoUiTimer.control = "stop" +end function + +function OnHideVideoUiTimer() as void + if m.top.playerUiState = PlayerUiState.Hidden + return + end if + + m.top.playerUiState = PlayerUiState.FadingOut +end function + +function OnUiFadeAnimationStateChange() as void + ' "stopped" Indicates that the animation has either run to completion or has been explicitly stopped. + ' Since we don't stop the animation explicitly, we can assume that the animation has run to completion. + ' This assumption becomes invalid if we set m.uiFadeAnimation.control = "stop" + if m.uiFadeAnimation.state <> "stopped" + return + end if + + state = m.top.playerUiState + if state = PlayerUiState.FadingIn + m.top.playerUiState = PlayerUiState.Visible + else if state = PlayerUiState.FadingOut + m.top.playerUiState = PlayerUiState.Hidden + end if +end function + +function ShutDownPlayerUi() + m.hideVideoUiTimer.control = "stop" + m.loadingSpinner.control = "stop" + m.clock.dispose = true +end function diff --git a/playlet-lib/src/components/VideoPlayerDev/VideoPlayerDev.bs b/playlet-lib/src/components/VideoPlayerDev/VideoPlayerDev.bs index e2d0d7a2..087b146c 100644 --- a/playlet-lib/src/components/VideoPlayerDev/VideoPlayerDev.bs +++ b/playlet-lib/src/components/VideoPlayerDev/VideoPlayerDev.bs @@ -1,5 +1,6 @@ import "pkg:/components/Navigation/Navigation.bs" import "pkg:/components/VideoPlayerDev/PictureInPicture.bs" +import "pkg:/components/VideoPlayerDev/PlayerUi.bs" import "pkg:/components/VideoQueue/VideoQueueUtils.bs" import "pkg:/source/AsyncTask/AsyncTask.bs" import "pkg:/source/AsyncTask/Tasks.bs" @@ -12,29 +13,11 @@ import "pkg:/source/utils/StringUtils.bs" import "pkg:/source/utils/Types.bs" function Init() - m.top.enableUI = false - m.top.enableTrickPlay = false + InitializePlayerUi() + httpAgent = m.top.getHttpAgent() httpAgent.AddHeader("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0") - - m.clock = m.top.findNode("Clock") - m.titleLabel = m.top.findNode("TitleLabel") - m.secondaryTitleLabel = m.top.findNode("SecondaryTitleLabel") - m.backgroundPoster = m.top.findNode("BackgroundPoster") - m.backgroundPosterSmall = m.top.findNode("BackgroundPosterSmall") - m.previousButton = m.top.findNode("PreviousButton") - m.playButton = m.top.findNode("PlayButton") - m.nextButton = m.top.findNode("NextButton") - m.minimizeButton = m.top.findNode("MinimizeButton") - m.trickPlayBar = m.top.findNode("TrickPlayBar") - m.videoUi = m.top.findNode("VideoUI") - m.progressTimer = m.top.findNode("ProgressTimer") - m.hideVideoUiTimer = m.top.findNode("HideVideoUiTimer") - m.fadeAnimation = m.top.findNode("FadeAnimation") - m.fadeOpacityInterpolator = m.top.findNode("FadeOpacityInterpolator") - - SetupLoadingSpinner() end function function OnNodeReady() @@ -54,7 +37,6 @@ function OnNodeReady() DisposableObserveField(m.top, "bufferingStatus", FuncName(OnBufferingStatusChange)) DisposableObserveField(m.progressTimer, "fire", FuncName(OnProgress)) - DisposableObserveField(m.hideVideoUiTimer, "fire", FuncName(OnHideVideoUiTimer)) SetNavigation(m.previousButton, RemoteKeys.Right, m.playButton) SetNavigation(m.playButton, RemoteKeys.Right, m.nextButton) @@ -160,20 +142,42 @@ function OnClose() end function function OnKeyEvent(key as string, press as boolean) as boolean - m.hideVideoUiTimer.control = "stop" - m.hideVideoUiTimer.control = "start" - if ShowVideoUi() - return true + if press = false + return false + end if + + state = m.top.state + uiState = m.top.playerUiState + + if key = RemoteKeys.Play + if state = "paused" and uiState = PlayerUiState.Hidden + ' If we pressed play and the state is paused, the player is about to resume. + ' Return false to let the default behavior resume video. + return false + end if + end if + + if key = RemoteKeys.Play or key = RemoteKeys.PlayOnly + if state = "playing" and uiState = PlayerUiState.Hidden + ' If we pressed play and the state is playing, the player is about to pause. + ' Return false to let the default behavior pause video. + m.top.playerUiState = PlayerUiState.FadingIn + return false + end if + end if + + if uiState = PlayerUiState.Visible + StartHideVideoUiTimer() + end if + + if uiState = PlayerUiState.Hidden or uiState = PlayerUiState.FadingOut + m.top.playerUiState = PlayerUiState.FadingIn end if if NavigationKeyHandler(key, press).handled return true end if - if press = false - return false - end if - if key = RemoteKeys.Back m.videoQueue.closePlayer = true return true @@ -264,72 +268,8 @@ function OnProgress() as void m.trickPlayBar.progress = m.top.position / duration end function -function OnHideVideoUiTimer() - HideVideoUi() -end function - function OnDispose() ShutDownPitctureInPicture() - m.fadeAnimation.control = "stop" - m.hideVideoUiTimer.control = "stop" - m.loadingSpinner.control = "stop" - m.clock.dispose = true + ShutDownPlayerUi() DisposeObservers() end function - -function SetupLoadingSpinner() - m.loadingUi = m.top.findNode("LoadingUI") - m.loadingSpinnerBackground = m.loadingUi.findNode("LoadingSpinnerBackground") - m.loadingSpinner = m.loadingUi.findNode("LoadingSpinner") - poster = m.loadingSpinner.poster - - size = 150 - poster.width = size - poster.height = size - centerx = (1280 - size) / 2 - centery = (720 - size) / 2 - m.loadingSpinner.translation = [centerx, centery] - poster.opacity = 0.8 - poster.uri = "pkg:/images/spinner.png" - - size = 80 - centerx = (1280 - size) / 2 - centery = (720 - size) / 2 - m.loadingSpinnerBackground.translation = [centerx, centery] - - m.loadingLabel = m.loadingSpinner.findNode("LoadingLabel") - m.loadingPercentLabel = m.loadingSpinner.findNode("LoadingPercentLabel") -end function - -function ShowVideoUi() as boolean - if m.fadeAnimation.state = "stopped" - if m.videoUi.opacity = 1 - return false - end if - end if - - m.fadeAnimation.control = "stop" - m.fadeOpacityInterpolator.reverse = false - m.fadeAnimation.control = "start" - - if m.top.hasFocus() - NodeSetFocus(m.playButton, true) - end if - - return true -end function - -function HideVideoUi() as void - if m.fadeAnimation.state = "stopped" - if m.videoUi.opacity = 0 - return - end if - end if - m.fadeAnimation.control = "stop" - m.fadeOpacityInterpolator.reverse = true - m.fadeAnimation.control = "start" - - if m.top.isInFocusChain() - NodeSetFocus(m.top, true) - end if -end function diff --git a/playlet-lib/src/components/VideoPlayerDev/VideoPlayerDev.xml b/playlet-lib/src/components/VideoPlayerDev/VideoPlayerDev.xml index b6a7c1d1..a0ae9356 100644 --- a/playlet-lib/src/components/VideoPlayerDev/VideoPlayerDev.xml +++ b/playlet-lib/src/components/VideoPlayerDev/VideoPlayerDev.xml @@ -15,6 +15,7 @@ + @@ -25,7 +26,7 @@ horizOrigin="right" translation="[1280,0]" /> - + - + - - + + - +