From 50c383df954de8aa5faffd46791c89b7ab1ef9a7 Mon Sep 17 00:00:00 2001 From: Michael Manganiello Date: Thu, 15 Jun 2023 22:42:51 -0300 Subject: [PATCH 1/2] Scrobble playing track when removed from queue This change fixes two `TODO` comments, by scrobbling the currently playing track if it's removed from the queue, and only run OnSongChange callbacks when the playing track is affected. --- backend/playbackmanager.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/backend/playbackmanager.go b/backend/playbackmanager.go index 653fdbf..7808cef 100644 --- a/backend/playbackmanager.go +++ b/backend/playbackmanager.go @@ -248,11 +248,15 @@ func (p *PlaybackManager) RemoveTracksFromQueue(trackIDs []string) { newQueue := make([]*mediaprovider.Track, 0, len(p.playQueue)-len(trackIDs)) rmCount := 0 idSet := sharedutil.ToSet(trackIDs) + isPlayingTrackRemoved := false for i, tr := range p.playQueue { if _, ok := idSet[tr.ID]; ok { // removing this track - // TODO: if we are removing the currently playing track, - // we need to scrobble it if it played for more than the scrobble threshold + if i == p.NowPlayingIndex() { + isPlayingTrackRemoved = true + // If we are removing the currently playing track, we need to scrobble it + p.checkScrobble(p.playTimeStopwatch.Elapsed()) + } if err := p.player.RemoveTrackAt(i - rmCount); err == nil { rmCount++ } else { @@ -268,8 +272,9 @@ func (p *PlaybackManager) RemoveTracksFromQueue(trackIDs []string) { p.playQueue = newQueue p.nowPlayingIdx = p.player.GetStatus().PlaylistPos // fire on song change callbacks in case the playing track was removed - // TODO: only call this if the playing track actually was removed - p.invokeOnSongChangeCallbacks() + if isPlayingTrackRemoved { + p.invokeOnSongChangeCallbacks() + } } // Stop playback and clear the play queue. From 3063cec6006d816b0647ebfc1a60064df49e5341 Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Thu, 15 Jun 2023 20:23:49 -0700 Subject: [PATCH 2/2] Fix another bug where next track would get scrobbled additionally --- backend/playbackmanager.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/backend/playbackmanager.go b/backend/playbackmanager.go index 7808cef..51f64c5 100644 --- a/backend/playbackmanager.go +++ b/backend/playbackmanager.go @@ -60,8 +60,7 @@ func NewPlaybackManager( if tracknum >= int64(len(pm.playQueue)) { return } - pm.checkScrobble(pm.playTimeStopwatch.Elapsed()) - pm.playTimeStopwatch.Reset() + pm.checkScrobble() if pm.player.GetStatus().State == player.Playing { pm.playTimeStopwatch.Start() } @@ -76,8 +75,7 @@ func NewPlaybackManager( }) p.OnStopped(func() { pm.playTimeStopwatch.Stop() - pm.checkScrobble(pm.playTimeStopwatch.Elapsed()) - pm.playTimeStopwatch.Reset() + pm.checkScrobble() pm.stopPollTimePos() pm.doUpdateTimePos() pm.invokeOnSongChangeCallbacks() @@ -255,7 +253,7 @@ func (p *PlaybackManager) RemoveTracksFromQueue(trackIDs []string) { if i == p.NowPlayingIndex() { isPlayingTrackRemoved = true // If we are removing the currently playing track, we need to scrobble it - p.checkScrobble(p.playTimeStopwatch.Elapsed()) + p.checkScrobble() } if err := p.player.RemoveTrackAt(i - rmCount); err == nil { rmCount++ @@ -294,10 +292,11 @@ func (p *PlaybackManager) SetReplayGainOptions(config ReplayGainConfig) { } // call BEFORE updating p.nowPlayingIdx -func (p *PlaybackManager) checkScrobble(playDur time.Duration) { +func (p *PlaybackManager) checkScrobble() { if !p.scrobbleCfg.Enabled || len(p.playQueue) == 0 || p.nowPlayingIdx < 0 { return } + playDur := p.playTimeStopwatch.Elapsed() if playDur.Seconds() < 0.1 || p.curTrackTime < 0.1 { return } @@ -311,6 +310,7 @@ func (p *PlaybackManager) checkScrobble(playDur time.Duration) { p.lastScrobbled = song go p.sm.Server.Scrobble(song.ID, true) } + p.playTimeStopwatch.Reset() } func (p *PlaybackManager) sendNowPlayingScrobble() {