Merge pull request #196 from adamantike/fix/scrobble-track-when-removed-from-queue

Scrobble playing track when removed from queue
This commit is contained in:
Drew Weymouth
2023-06-15 20:39:40 -07:00
committed by GitHub
+14 -9
View File
@@ -60,8 +60,7 @@ func NewPlaybackManager(
if tracknum >= int64(len(pm.playQueue)) { if tracknum >= int64(len(pm.playQueue)) {
return return
} }
pm.checkScrobble(pm.playTimeStopwatch.Elapsed()) pm.checkScrobble()
pm.playTimeStopwatch.Reset()
if pm.player.GetStatus().State == player.Playing { if pm.player.GetStatus().State == player.Playing {
pm.playTimeStopwatch.Start() pm.playTimeStopwatch.Start()
} }
@@ -76,8 +75,7 @@ func NewPlaybackManager(
}) })
p.OnStopped(func() { p.OnStopped(func() {
pm.playTimeStopwatch.Stop() pm.playTimeStopwatch.Stop()
pm.checkScrobble(pm.playTimeStopwatch.Elapsed()) pm.checkScrobble()
pm.playTimeStopwatch.Reset()
pm.stopPollTimePos() pm.stopPollTimePos()
pm.doUpdateTimePos() pm.doUpdateTimePos()
pm.invokeOnSongChangeCallbacks() pm.invokeOnSongChangeCallbacks()
@@ -248,11 +246,15 @@ func (p *PlaybackManager) RemoveTracksFromQueue(trackIDs []string) {
newQueue := make([]*mediaprovider.Track, 0, len(p.playQueue)-len(trackIDs)) newQueue := make([]*mediaprovider.Track, 0, len(p.playQueue)-len(trackIDs))
rmCount := 0 rmCount := 0
idSet := sharedutil.ToSet(trackIDs) idSet := sharedutil.ToSet(trackIDs)
isPlayingTrackRemoved := false
for i, tr := range p.playQueue { for i, tr := range p.playQueue {
if _, ok := idSet[tr.ID]; ok { if _, ok := idSet[tr.ID]; ok {
// removing this track // removing this track
// TODO: if we are removing the currently playing track, if i == p.NowPlayingIndex() {
// we need to scrobble it if it played for more than the scrobble threshold isPlayingTrackRemoved = true
// If we are removing the currently playing track, we need to scrobble it
p.checkScrobble()
}
if err := p.player.RemoveTrackAt(i - rmCount); err == nil { if err := p.player.RemoveTrackAt(i - rmCount); err == nil {
rmCount++ rmCount++
} else { } else {
@@ -268,8 +270,9 @@ func (p *PlaybackManager) RemoveTracksFromQueue(trackIDs []string) {
p.playQueue = newQueue p.playQueue = newQueue
p.nowPlayingIdx = p.player.GetStatus().PlaylistPos p.nowPlayingIdx = p.player.GetStatus().PlaylistPos
// fire on song change callbacks in case the playing track was removed // fire on song change callbacks in case the playing track was removed
// TODO: only call this if the playing track actually was removed if isPlayingTrackRemoved {
p.invokeOnSongChangeCallbacks() p.invokeOnSongChangeCallbacks()
}
} }
// Stop playback and clear the play queue. // Stop playback and clear the play queue.
@@ -289,10 +292,11 @@ func (p *PlaybackManager) SetReplayGainOptions(config ReplayGainConfig) {
} }
// call BEFORE updating p.nowPlayingIdx // 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 { if !p.scrobbleCfg.Enabled || len(p.playQueue) == 0 || p.nowPlayingIdx < 0 {
return return
} }
playDur := p.playTimeStopwatch.Elapsed()
if playDur.Seconds() < 0.1 || p.curTrackTime < 0.1 { if playDur.Seconds() < 0.1 || p.curTrackTime < 0.1 {
return return
} }
@@ -306,6 +310,7 @@ func (p *PlaybackManager) checkScrobble(playDur time.Duration) {
p.lastScrobbled = song p.lastScrobbled = song
go p.sm.Server.Scrobble(song.ID, true) go p.sm.Server.Scrobble(song.ID, true)
} }
p.playTimeStopwatch.Reset()
} }
func (p *PlaybackManager) sendNowPlayingScrobble() { func (p *PlaybackManager) sendNowPlayingScrobble() {