Fix #810: only enqueue autoplay tracks when 10 sec from end of play queue

This commit is contained in:
Drew Weymouth
2026-01-24 10:23:35 -08:00
parent 0bb5f969ff
commit 8e2e4c6d89
+13 -5
View File
@@ -42,6 +42,9 @@ type PlaybackManager struct {
lastPlayingID string lastPlayingID string
wfmUpdateImageCancel context.CancelFunc wfmUpdateImageCancel context.CancelFunc
wfmImageJobs [3]*WaveformImageJob wfmImageJobs [3]*WaveformImageJob
// whether autoplay tracks are currently being fetched/enqueued
pendingAutoplay bool
} }
type RemotePlaybackDevice struct { type RemotePlaybackDevice struct {
@@ -100,8 +103,14 @@ func (p *PlaybackManager) addOnTrackChangeHook() {
// On Windows, MPV sometimes fails to start playback when switching to a track // On Windows, MPV sometimes fails to start playback when switching to a track
// with a different sample rate than the previous. If this is detected, // with a different sample rate than the previous. If this is detected,
// send a command to the MPV player to force restart playback. // send a command to the MPV player to force restart playback.
p.OnPlayTimeUpdate(func(curTime, _ float64, _ bool) { p.OnPlayTimeUpdate(func(curTime, totalTime float64, _ bool) {
p.lastPlayTime = curTime p.lastPlayTime = curTime
// enqueue autoplay tracks if enabled and nearing end of queue
if p.cfg.Autoplay && !p.pendingAutoplay && totalTime-curTime < 10.0 &&
p.NowPlayingIndex() == len(p.engine.playQueue)-1 {
p.enqueueAutoplayTracks()
}
}) })
p.engine.onBeforeSongChange = append(p.engine.onBeforeSongChange, func(item mediaprovider.MediaItem) { p.engine.onBeforeSongChange = append(p.engine.onBeforeSongChange, func(item mediaprovider.MediaItem) {
@@ -116,10 +125,6 @@ func (p *PlaybackManager) addOnTrackChangeHook() {
}) })
p.OnSongChange(func(item mediaprovider.MediaItem, _ *mediaprovider.Track) { p.OnSongChange(func(item mediaprovider.MediaItem, _ *mediaprovider.Track) {
// Autoplay if enabled and we are on the last track
if p.cfg.Autoplay && p.NowPlayingIndex() == len(p.engine.playQueue)-1 {
p.enqueueAutoplayTracks()
}
p.handleWaveformImageSongChange(item) p.handleWaveformImageSongChange(item)
if runtime.GOOS != "windows" { if runtime.GOOS != "windows" {
@@ -729,7 +734,10 @@ func (p *PlaybackManager) enqueueAutoplayTracks() {
// since this func is invoked in a callback from the playback engine, // since this func is invoked in a callback from the playback engine,
// need to do the rest async as it may take time and block other callbacks // need to do the rest async as it may take time and block other callbacks
p.pendingAutoplay = true
go func() { go func() {
defer func() { p.pendingAutoplay = false }()
// first 2 strategies - similar by artist, and similar by genres - only work for tracks // first 2 strategies - similar by artist, and similar by genres - only work for tracks
if nowPlaying.Metadata().Type == mediaprovider.MediaItemTypeTrack { if nowPlaying.Metadata().Type == mediaprovider.MediaItemTypeTrack {
tr := nowPlaying.(*mediaprovider.Track) tr := nowPlaying.(*mediaprovider.Track)