import "pkg:/components/Navigation/Navigation.bs" import "pkg:/components/Services/Innertube/Constants.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" import "pkg:/source/utils/DisposableObserve.bs" import "pkg:/source/utils/ErrorUtils.bs" import "pkg:/source/utils/FocusManagement.bs" import "pkg:/source/utils/Logging.bs" import "pkg:/source/utils/RemoteKeys.bs" import "pkg:/source/utils/StringUtils.bs" import "pkg:/source/utils/Types.bs" function Init() InitializePlayerUi() httpAgent = m.top.getHttpAgent() httpAgent.AddHeader("User-Agent", VIDEO_PLAYER_USER_AGENT) end function function OnNodeReady() InitializePitctureInPicture() DisposableObserveFieldScoped(m.videoQueue, "hasPrevious", FuncName(OnHasPreviousChange)) DisposableObserveFieldScoped(m.videoQueue, "hasNext", FuncName(OnHasNextChange)) OnHasPreviousChange() OnHasNextChange() DisposableObserveFieldScoped(m.previousButton, "buttonSelected", FuncName(OnPreviousButtonSelected)) DisposableObserveFieldScoped(m.playButton, "buttonSelected", FuncName(OnPlayButtonSelected)) DisposableObserveFieldScoped(m.nextButton, "buttonSelected", FuncName(OnNextButtonSelected)) DisposableObserveFieldScoped(m.minimizeButton, "buttonSelected", FuncName(OnMinimizeButtonSelected)) DisposableObserveField(m.top, "state", FuncName(OnPlayerStateChange)) DisposableObserveField(m.top, "bufferingStatus", FuncName(OnBufferingStatusChange)) DisposableObserveField(m.progressTimer, "fire", FuncName(OnProgress)) SetNavigation(m.previousButton, RemoteKeys.Right, m.playButton) SetNavigation(m.playButton, RemoteKeys.Right, m.nextButton) SetNavigation(m.nextButton, RemoteKeys.Right, m.minimizeButton) SetNavigation(m.playButton, RemoteKeys.Right, m.minimizeButton) SetNavigation(m.minimizeButton, RemoteKeys.Left, m.nextButton) SetNavigation(m.minimizeButton, RemoteKeys.Left, m.playButton) SetNavigation(m.nextButton, RemoteKeys.Left, m.playButton) SetNavigation(m.playButton, RemoteKeys.Left, m.previousButton) if m.top.hasFocus() and m.videoUi.visible and m.videoUi.opacity = 1 NodeSetFocus(m.playButton, true) end if end function function PlayWithContent(contentNode as object) videoContentNode = CreateObject("roSGNode", "ContentNode") videoId = contentNode.videoId title = contentNode.title author = contentNode._author thumbnail = contentNode.thumbnail if not StringUtils.IsNullOrEmpty(videoId) videoContentNode.AddFields({ videoId: videoId }) end if if not StringUtils.IsNullOrEmpty(title) videoContentNode.title = title end if ' NOTE: "_author" not "author". See PlaylistContentNode.xml for explanation. if not StringUtils.IsNullOrEmpty(author) videoContentNode.secondaryTitle = author end if if not StringUtils.IsNullOrEmpty(thumbnail) m.backgroundPosterSmall.uri = thumbnail end if StartVideoContentTask(videoContentNode) end function function StartVideoContentTask(videoContentNode as object) if m.videoContentTask <> invalid m.videoContentTask.cancel = true end if m.top.content = videoContentNode m.loadingLabel.text = Tr(Locale.VideoPlayer.Loading) m.loadingPercentLabel.text = "" m.loadingUi.visible = true m.loadingSpinner.control = "start" m.videoContentTask = AsyncTask.Start(Tasks.VideoContentTaskDev, { titleLabel: m.titleLabel secondaryTitleLabel: m.secondaryTitleLabel content: videoContentNode invidious: m.top.invidious preferences: m.top.preferences backgroundPoster: m.backgroundPoster }, OnVideoContentTaskResults) end function function OnVideoContentTaskResults(output as object) as void m.videoContentTask = invalid if output.cancelled return end if if not output.success or not output.result.success ' output.error for unhandled exception error = output.error if error = invalid ' output.result.error for network errors error = output.result.error end if error = ErrorUtils.Format(error) LogError(error) m.top.close = true return end if m.top.control = "play" end function function OnClose() m.top.previousPlayer = invalid if m.videoContentTask <> invalid m.videoContentTask.cancel = true m.videoContentTask = invalid end if parent = m.top.getParent() if parent <> invalid parent.RemoveChild(m.top) end if m.top.control = "stop" m.top.content = invalid m.top.dispose = true end function function OnKeyEvent(key as string, press as boolean) as boolean 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 key = RemoteKeys.Back m.videoQueue.closePlayer = true return true end if return false end function function OnHasPreviousChange() m.previousButton.disabled = not m.videoQueue.hasPrevious end function function OnHasNextChange() m.nextButton.disabled = not m.videoQueue.hasNext end function function OnPlayButtonSelected() state = m.top.state if state = "playing" m.top.control = "pause" else if state = "paused" m.top.control = "resume" end if end function function OnPreviousButtonSelected() m.videoQueue.previous = true end function function OnNextButtonSelected() m.videoQueue.next = true end function function OnMinimizeButtonSelected() VideoQueueUtils.ToggleVideoPictureInPicture(m.videoQueue) end function function OnPlayerStateChange() as void state = m.top.state if state = "stopping" or state = "stopped" m.videoUi.visible = false return end if if state = "playing" m.playButton.toggleState = false ' Pause icon m.playButton.disabled = false m.progressTimer.control = "start" else if state = "paused" m.playButton.toggleState = true ' Play icon m.playButton.disabled = false else m.playButton.disabled = true end if if state = "buffering" m.loadingLabel.text = Tr(Locale.VideoPlayer.Buffering) m.loadingSpinner.control = "start" m.loadingUi.visible = true m.backgroundPoster.visible = true m.backgroundPosterSmall.visible = true else m.loadingSpinner.control = "stop" m.loadingUi.visible = false m.backgroundPoster.visible = false m.backgroundPosterSmall.visible = false end if end function function OnBufferingStatusChange() as void bufferingStatus = m.top.bufferingStatus if bufferingStatus = invalid return end if m.loadingPercentLabel.text = `${bufferingStatus.percentage}%` end function function OnProgress() as void duration = m.top.duration if duration = 0 m.trickPlayBar.progress = 0 return end if m.trickPlayBar.progress = m.top.position / duration end function function OnDispose() ShutDownPitctureInPicture() ShutDownPlayerUi() DisposeObservers() end function