feat: Update play tracking to trigger at 20% completion instead of start

This commit is contained in:
JSC
2025-07-12 16:13:13 +02:00
parent 6bbf3dce66
commit 4e96c3538c

View File

@@ -23,6 +23,7 @@ TRACK_START_THRESHOLD_MS = 500 # 500 milliseconds - threshold for considering a
STATE_CHANGE_THRESHOLD_MS = (
1000 # 1 second threshold for state change detection
)
PLAY_COMPLETION_THRESHOLD = 0.20 # 20% completion threshold to count as a play
class MusicPlayerService:
@@ -655,18 +656,23 @@ class MusicPlayerService:
elif self.is_playing and not old_playing:
self._track_ending_handled = False
# Track play event when song starts playing from beginning (but only once per track load)
# Only track if we're playing, haven't tracked yet, and current time is near the start (< 5 seconds)
# Track play event when song reaches 20% completion (but only once per track load)
# Only track if playing, haven't tracked yet, have valid duration, and reached threshold
if (
self.is_playing
and not self._track_play_tracked
and self.current_time >= 0
and self.current_time < TRACK_START_THRESHOLD_MS
and self.duration > 0
and self.current_time > 0 # Ensure we have valid playback time
and self.current_time >= (self.duration * PLAY_COMPLETION_THRESHOLD)
):
current_track = self.get_current_track()
if current_track:
self._track_sound_play(current_track["id"])
self._track_play_tracked = True
logger.info(
f"Tracked play for '{current_track['title']}' at {self.current_time}ms "
f"({(self.current_time/self.duration)*100:.1f}% completion)"
)
# Emit updates if state changed significantly or periodically
state_changed = (