From b5ad6384494e3b870335357aef90a1695cb4b0f2 Mon Sep 17 00:00:00 2001 From: 1hitsong <3330318+1hitsong@users.noreply.github.com> Date: Thu, 4 Jan 2024 22:40:31 -0500 Subject: [PATCH] Update default subtitle logic to work with custom subtitle function --- components/ItemGrid/LoadVideoContentTask.bs | 5 ++- components/video/VideoPlayerView.bs | 47 ++++++++++++++++++--- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/components/ItemGrid/LoadVideoContentTask.bs b/components/ItemGrid/LoadVideoContentTask.bs index 833bb9f7..fa2a7d0e 100644 --- a/components/ItemGrid/LoadVideoContentTask.bs +++ b/components/ItemGrid/LoadVideoContentTask.bs @@ -151,13 +151,16 @@ sub LoadItems_AddVideoContent(video as object, mediaSourceId as dynamic, audio_s end if addSubtitlesToVideo(video, meta) + else + video.SelectedSubtitle = subtitle_idx end if + else + video.SelectedSubtitle = subtitle_idx end if video.videoId = video.id video.mediaSourceId = mediaSourceId video.audioIndex = audio_stream_idx - video.SelectedSubtitle = subtitle_idx video.PlaySessionId = m.playbackInfo.PlaySessionId diff --git a/components/video/VideoPlayerView.bs b/components/video/VideoPlayerView.bs index dfe4eb75..c2a89306 100644 --- a/components/video/VideoPlayerView.bs +++ b/components/video/VideoPlayerView.bs @@ -323,11 +323,6 @@ sub onVideoContentLoaded() m.top.transcodeParams = videoContent[0].transcodeparams m.chapters = videoContent[0].chapters - ' Allow default subtitles - m.top.unobserveField("selectedSubtitle") - m.top.selectedSubtitle = videoContent[0].selectedSubtitle - m.top.observeField("selectedSubtitle", "onSubtitleChange") - m.osd.itemTitleText = m.top.content.title populateChapterMenu() @@ -340,6 +335,29 @@ sub onVideoContentLoaded() m.top.allowCaptions = true end if + ' Allow default subtitles + m.top.unobserveField("selectedSubtitle") + + ' Set subtitleTrack property is subs are natively supported by Roku + selectedSubtitle = invalid + for each subtitle in m.top.fullSubtitleData + if subtitle.Index = videoContent[0].selectedSubtitle + selectedSubtitle = subtitle + exit for + end if + end for + + if isValid(selectedSubtitle) + availableSubtitleTrackIndex = availSubtitleTrackIdx(selectedSubtitle.Track.TrackName) + if availableSubtitleTrackIndex <> -1 + m.top.subtitleTrack = m.top.availableSubtitleTracks[availableSubtitleTrackIndex].TrackName + end if + end if + + m.top.selectedSubtitle = videoContent[0].selectedSubtitle + + m.top.observeField("selectedSubtitle", "onSubtitleChange") + if isValid(m.top.audioIndex) m.top.audioTrack = (m.top.audioIndex + 1).toStr() else @@ -587,6 +605,25 @@ function stateAllowsOSD() as boolean return inArray(validStates, m.top.state) end function + +' availSubtitleTrackIdx: Returns Roku's index for requested subtitle track +' +' @param {string} tracknameToFind - TrackName for subtitle we're looking to match +' @return {integer} indicating Roku's index for requested subtitle track. Returns -1 if not found +function availSubtitleTrackIdx(tracknameToFind as string) as integer + idx = 0 + for each availTrack in m.top.availableSubtitleTracks + ' The TrackName must contain the URL we supplied originally, though + ' Roku mangles the name a bit, so we check if the URL is a substring, rather + ' than strict equality + if Instr(1, availTrack.TrackName, tracknameToFind) + return idx + end if + idx = idx + 1 + end for + return -1 +end function + function onKeyEvent(key as string, press as boolean) as boolean ' Keypress handler while user is inside the chapter menu