| Add ability to load previous track
All checks were successful
SonarQube Code Quality Scan / SonarQube Scan (push) Successful in 26s

This commit is contained in:
2026-06-01 21:29:53 +03:00
parent 31ceb040ed
commit 363ee732b4
2 changed files with 85 additions and 4 deletions

View File

@@ -355,6 +355,7 @@ class AudioPlayer(QObject):
"""
loading = Signal(str)
song_loaded = Signal(bool)
song_started = Signal(dict)
song_ended = Signal(str)
paused = Signal(dict)
@@ -383,6 +384,7 @@ class AudioPlayer(QObject):
# ── Queue & state ─────────────────────────────────────────────
self._queue: list[dict] = []
self._current: Optional[dict] = None
self._history: list[dict] = []
self._loop_mode = False
# ── Download worker (single persistent thread) ────────────────
@@ -447,20 +449,42 @@ class AudioPlayer(QObject):
def skip(self) -> None:
"""Skip to the next song."""
# Cancel any in-progress download (the skip is our new intent).
# Push current song to history before moving on.
if self._current is not None:
self._history.append(self._current)
self._cancel_active_downloads()
self._player.stop()
self._advance()
def previous(self) -> None:
"""Go back to the previous song, if available."""
if not self._history:
return
prev = self._history.pop()
self._cancel_active_downloads()
self._player.stop()
self._queue.insert(0, prev)
self._try_next_song()
def has_previous(self) -> bool:
"""Whether a previous song is available."""
return len(self._history) > 0
def is_loaded(self) -> bool:
"""Whether any song is currently loaded (playing or paused)."""
return self._current is not None
def stop_playback(self) -> None:
"""Soft stop: halt playback, clear queue, cancel downloads."""
self._cancel_active_downloads()
self._cleanup_temp_files()
self._queue.clear()
self._prefetch_cache.clear()
self._history.clear()
self._loop_mode = False
self._current = None
self._player.stop()
self.song_loaded.emit(False)
self.playback_state_changed.emit("stopped")
@Slot()
@@ -523,6 +547,9 @@ class AudioPlayer(QObject):
def get_loop(self) -> bool:
return self._loop_mode
def get_position(self) -> int:
return self._player.position()
def set_loop(self, enabled: bool) -> None:
self._loop_mode = enabled
@@ -658,6 +685,7 @@ class AudioPlayer(QObject):
self._player.setSource(QUrl.fromLocalFile(local_path))
self._player.play()
self.song_started.emit(song)
self.song_loaded.emit(True)
# Kick off prefetch for the next queued song.
self._start_prefetch()