Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
namlu
Visitor

Recognising overlapping chapter times

I need to handle cases when the chapter times overlap one another so that I can display the correct text for that chapter. For example, I have the following chapter array:

"chapters": Array[3][
    {
      "name": "Recap & Intro",
      "start": 0,
      "end": 145000,
      "skippable": true,
      "canSkipOnAutoplay": true
    },
    {
      "name": "Intro",
      "start": 53000,
      "end": 145000,
      "skippable": true
    },
    {
      "name": "Credits",
      "start": 3498000,
      "skippable": true,
      "canSkipOnAutoplay": true,
      "showUpNext": true
    }
  ],


So I've done this to check whether or not I'm in the current chapter:

function isInChapter(chapter as Object) as Boolean
    return m.top.videoPosition >= chapter.start and m.top.videoPosition < chapter.end
end function


 
Unfortunately, this doesn't account for the above case where the Recap & Intro overlap time with the Intro.

I've been trying something like this:
function chapterForCurrentVideoPosition(unused = invalid) as Object
    videoNode = m.top.videoContentNode
    if videoNode = invalid
        return invalid
    end if
    
    for i = 0 to (videoNode.chapters.count() - 1)
        chapter = videoNode.chapters[i]

        if isInChapter(chapter)
            if m.top.videoPosition >= videoNode.chapters[i + 1].start
                exit for
            end if
            return chapter
        end if
    end for
    return invalid
end function

But I end up with a strange loop case where the chapters become out of whack around position = 53, which is the start of Intro. 

The debug logs look something like this (Note: I removed the debug statements from the above code for simplification):
]
VideoSkipChapterButton:   ---for loop count down: 0
VideoSkipChapterButton: ===chapter.start: 0
VideoSkipChapterButton: ===chapter.end: 145
VideoSkipChapterButton:   Is in chapter, returning Recap & Intro.
VideoSkipChapterButton: ===chapter.end: 145
.start: 53
VideoPlayerScreen: updateSkipChapterShouldHideIfChapterChanged
VideoPlayerScreen: updateCurrentChapter
]
VideoSkipChapterButton:   ---for loop count down: 0
VideoSkipChapterButton: ===chapter.start: 0
VideoSkipChapterButton: ===chapter.end: 145
VideoSkipChapterButton:   Is in chapter, returning Recap & Intro.
VideoSkipChapterButton: ===chapter.end: 145
.start: 53
]
VideoSkipChapterButton:   ---for loop count down: 0
VideoSkipChapterButton: ===chapter.start: 0
VideoSkipChapterButton: ===chapter.end: 145
VideoSkipChapterButton:   Is in chapter, returning Recap & Intro.
VideoSkipChapterButton: ===chapter.end: 145
.start: 53
VideoPlayerScreen: onVideoPositionChanged: 53.09
]
VideoSkipChapterButton:   ---for loop count down: 0
VideoSkipChapterButton: ===chapter.start: 0
VideoSkipChapterButton: ===chapter.end: 145
VideoSkipChapterButton:   Is in chapter, returning Recap & Intro.
VideoSkipChapterButton: ===chapter.end: 145
.start: 53
VideoSkipChapterButton: ===i = 0
VideoSkipChapterButton:   Could not find a chapter.
VideoPlayerScreen: ---shouldShowSkipChapterButton: false
VideoPlayerScreen: ---m.settingsGroup.visible = false
VideoPlayerScreen: ---m.skipChapterShouldHide = true
]
VideoSkipChapterButton:   ---for loop count down: 0
VideoSkipChapterButton: ===chapter.start: 0
VideoSkipChapterButton: ===chapter.end: 145
VideoSkipChapterButton:   Is in chapter, returning Recap & Intro.
VideoSkipChapterButton: ===chapter.end: 145
.start: 53
VideoSkipChapterButton: ===i = 0
VideoSkipChapterButton:   Could not find a chapter.
VideoPlayerScreen: updateSkipChapterShouldHideIfChapterChanged
VideoPlayerScreen: updateCurrentChapter
]
VideoSkipChapterButton:   ---for loop count down: 0
VideoSkipChapterButton: ===chapter.start: 0
VideoSkipChapterButton: ===chapter.end: 145
VideoSkipChapterButton:   Is in chapter, returning Recap & Intro.
VideoSkipChapterButton: ===chapter.end: 145
.start: 53
VideoSkipChapterButton: ===i = 0
VideoSkipChapterButton:   Could not find a chapter.

Not sure what I'm doing wrong. Can anyone advise?
Cheers
0 Kudos
1 REPLY 1
namlu
Visitor

Re: Recognising overlapping chapter times

Was able to fix by checking for the chapters in descending order rather than ascending. This removed the complexity of having to get the start time for the upcoming and comparing it to the current video time.

function chapterForCurrentVideoPosition(unused = invalid) as Object

    videoNode = m.top.videoContentNode
    if videoNode = invalid
       return invalid
    end if
    
    descChapters = videoNode.chapters
    descChapters.sortBy("start", "r")

    for each chapter in descChapters
        if isInChapter(chapter)
            return chapter
        end if
    end for
    
    return invalid
end function
0 Kudos