update slider once per second always

This commit is contained in:
Drew Weymouth
2022-12-23 14:55:47 -08:00
parent af24ec56a8
commit 024557d49e
4 changed files with 13 additions and 25 deletions
+5 -19
View File
@@ -32,9 +32,7 @@ func NewPlaybackManager(ctx context.Context, cli *subsonic.Client, p *player.Pla
for _, cb := range pm.onSongChange { for _, cb := range pm.onSongChange {
cb(pm.NowPlaying()) cb(pm.NowPlaying())
} }
if pm.pollingTick != nil { pm.doUpdateTimePos()
pm.pollingTick.Reset(pm.getPollSpeed())
}
}) })
p.OnSeek(func() { p.OnSeek(func() {
pm.doUpdateTimePos() pm.doUpdateTimePos()
@@ -106,25 +104,10 @@ func (p *PlaybackManager) PlayAlbum(albumID string) error {
return p.player.PlayFromBeginning() return p.player.PlayFromBeginning()
} }
// depending on the length of the current track, we need to poll
// faster or less fast to make track position scroll bar look smooth
func (p *PlaybackManager) getPollSpeed() time.Duration {
t := p.player.GetStatus().Duration
if t < 30 {
return 100 * time.Millisecond
} else if t < 90 {
return 150 * time.Millisecond
} else if t < 120 {
return 250 * time.Millisecond
} else {
return 333 * time.Millisecond
}
}
func (p *PlaybackManager) startPollTimePos() { func (p *PlaybackManager) startPollTimePos() {
ctx, cancel := context.WithCancel(p.ctx) ctx, cancel := context.WithCancel(p.ctx)
p.cancelPollPos = cancel p.cancelPollPos = cancel
p.pollingTick = time.NewTicker(p.getPollSpeed()) p.pollingTick = time.NewTicker(250 * time.Millisecond)
// TODO: fix occasional nil pointer dereference on app quit // TODO: fix occasional nil pointer dereference on app quit
go func() { go func() {
@@ -153,4 +136,7 @@ func (p *PlaybackManager) stopPollTimePos() {
p.cancelPollPos() p.cancelPollPos()
p.cancelPollPos = nil p.cancelPollPos = nil
} }
if p.pollingTick != nil {
p.pollingTick.Stop()
}
} }
+1 -1
View File
@@ -375,7 +375,7 @@ func (p *Player) eventHandler(ctx context.Context) {
case <-ctx.Done(): case <-ctx.Done():
return return
default: default:
e := p.mpv.WaitEvent(0.2 /*timeout seconds*/) e := p.mpv.WaitEvent(1 /*timeout seconds*/)
if e.Event_Id != mpv.EVENT_NONE { if e.Event_Id != mpv.EVENT_NONE {
//log.Printf("mpv event: %+v\n", e) //log.Printf("mpv event: %+v\n", e)
} }
+2 -3
View File
@@ -148,9 +148,8 @@ func (pc *PlayerControls) doPlayTimeUpdate(curTime, totalTime float64) {
pc.curTimeLabel.SetText(ct) pc.curTimeLabel.SetText(ct)
updated = true updated = true
} }
if totalTime < 210 || updated { if updated {
// if current track is long, we only need to redraw the slider // Only update slider once a second when time label changes
// when the time label updates, to reduce screen redraws.
pc.slider.SetValue(v) pc.slider.SetValue(v)
} }
} }
+5 -2
View File
@@ -1,13 +1,16 @@
package ui package ui
import "fmt" import (
"fmt"
"math"
)
func SecondsToTimeString(s float64) string { func SecondsToTimeString(s float64) string {
if s < 0 { if s < 0 {
s = 0 s = 0
} }
min := int(s / 60) min := int(s / 60)
sec := int(s - float64(min*60)) sec := int(math.Round(s - float64(min*60)))
return fmt.Sprintf("%2d:%02d", min, sec) return fmt.Sprintf("%2d:%02d", min, sec)
} }