fix crashing out of range after last lyric

This commit is contained in:
Drew Weymouth
2024-05-18 17:42:08 -07:00
parent 4946b01726
commit b4b079478a
+11 -3
View File
@@ -76,7 +76,7 @@ func (l *LyricsViewer) UpdatePlayPos(timeSecs float64) {
// advance if needed // advance if needed
if l.lyrics.Lines[l.nextLyricLine].Start <= timeSecs { if l.lyrics.Lines[l.nextLyricLine].Start <= timeSecs {
l.viewer.NextLine() l.viewer.NextLine()
if l.nextLyricLine < len(l.lyrics.Lines) { if l.nextLyricLine < len(l.lyrics.Lines)-1 {
l.nextLyricLine++ l.nextLyricLine++
} }
} }
@@ -88,14 +88,22 @@ func (l *LyricsViewer) OnSeeked(timeSecs float64) {
return return
} }
nextLine := 0 // first line that starts after timeSecs // find first line that starts after timeSecs
nextLine := -1
for i, l := range l.lyrics.Lines { for i, l := range l.lyrics.Lines {
if l.Start > timeSecs { if l.Start > timeSecs {
nextLine = i nextLine = i
break break
} }
} }
l.nextLyricLine = nextLine
if nextLine == -1 {
// last lyric
nextLine = len(l.lyrics.Lines)
l.nextLyricLine = nextLine - 1
} else {
l.nextLyricLine = nextLine
}
l.viewer.SetCurrentLine(nextLine /*one-indexed*/) l.viewer.SetCurrentLine(nextLine /*one-indexed*/)
} }