implement SeekFwdBackN

This commit is contained in:
Drew Weymouth
2024-09-11 07:57:38 -07:00
parent faf067ce7e
commit 00ffb8b3a1
2 changed files with 31 additions and 1 deletions
+30
View File
@@ -183,6 +183,22 @@ func (p *playbackEngine) SeekBackOrPrevious() error {
return p.PlayTrackAt(p.nowPlayingIdx - 1)
}
func (p *playbackEngine) SeekFwdBackN(n int) error {
idx := p.nowPlayingIdx
if n < 0 && p.player.GetStatus().TimePos > 3 {
n += 1 // first seek back is just seek to beginning of current
}
if n == 0 || (idx == 0 && n < 0) {
return p.player.SeekSeconds(0) // seek back in current song
}
lastIdx := len(p.playQueue) - 1
if idx == lastIdx && n > 0 {
return nil // already on last track, nothing to seek next to
}
newIdx := minInt(len(p.playQueue)-1, maxInt(0, idx+n))
return p.PlayTrackAt(newIdx)
}
// Seek to given absolute position in the current track by seconds.
func (p *playbackEngine) SeekSeconds(sec float64) error {
if p.isRadio {
@@ -667,3 +683,17 @@ func (p *playbackEngine) doUpdateTimePos(seeked bool) {
cb(s.TimePos, duration, seeked)
}
}
func minInt(a, b int) int {
if a < b {
return a
}
return b
}
func maxInt(a, b int) int {
if a > b {
return a
}
return b
}