seek to correct position when switching players

This commit is contained in:
Drew Weymouth
2025-03-29 15:57:34 -07:00
parent 084aa6cdba
commit f9c9f290bd
5 changed files with 63 additions and 30 deletions
+25 -10
View File
@@ -6,6 +6,7 @@ import (
"fmt"
"math"
"strconv"
"sync"
"github.com/dweymouth/supersonic/backend/mediaprovider"
"github.com/dweymouth/supersonic/backend/player"
@@ -68,6 +69,9 @@ type Player struct {
equalizer Equalizer
peaksEnabled bool
fileLoadedLock sync.Mutex
fileLoadedSig *sync.Cond
bgCancel context.CancelFunc
}
@@ -80,10 +84,12 @@ func New() *Player {
// Same as New, but sets the application name that mpv
// reports to the system audio API.
func NewWithClientName(c string) *Player {
return &Player{
p := &Player{
vol: -1, // use 100 in Init
clientName: c,
}
p.fileLoadedSig = sync.NewCond(&p.fileLoadedLock)
return p
}
// Initializes the Player and makes it ready for playback.
@@ -134,18 +140,27 @@ func (p *Player) Init(maxCacheMB int) error {
}
// Plays the specified file, clearing the previous play queue, if any.
func (p *Player) PlayFile(url string, _ mediaprovider.MediaItemMetadata) error {
func (p *Player) PlayFile(url string, _ mediaprovider.MediaItemMetadata, startTime float64) error {
if !p.initialized {
return ErrUnitialized
}
err := p.mpv.Command([]string{"loadfile", url, "replace"})
if err == nil {
p.lenPlaylist = 1
if p.status.State == player.Paused {
return p.Continue()
}
if err != nil {
return err
}
p.lenPlaylist = 1
if p.status.State == player.Paused {
err = p.Continue()
} else {
p.setState(player.Playing)
}
if startTime > 0 {
p.fileLoadedLock.Lock()
p.fileLoadedSig.Wait()
p.fileLoadedLock.Unlock()
p.SeekSeconds(startTime)
}
return err
}
@@ -481,10 +496,9 @@ func (p *Player) eventHandler(ctx context.Context) {
}
switch e.Event_Id {
case mpv.EVENT_PLAYBACK_RESTART:
if p.seeking {
p.seeking = false
}
p.seeking = false
case mpv.EVENT_SEEK:
p.seeking = false
p.InvokeOnSeek()
case mpv.EVENT_FILE_LOADED:
p.curPlaylistPos, _ = p.getInt64Property("playlist-pos")
@@ -494,6 +508,7 @@ func (p *Player) eventHandler(ctx context.Context) {
p.InvokeOnSeek()
}
p.InvokeOnTrackChange()
p.fileLoadedSig.Signal()
case mpv.EVENT_IDLE:
p.status.Duration = 0
p.status.TimePos = 0