feat: persist play/pause state — auto-resume playback or pause-on-load on restart
This commit is contained in:
parent
06f6529cbe
commit
ec797b8074
@ -179,6 +179,8 @@ class ConfigLoader:
|
||||
self.config.set('Playback', 'last_position_ms', '0')
|
||||
if not self.config.has_option('Playback', 'last_volume'):
|
||||
self.config.set('Playback', 'last_volume', '80')
|
||||
if not self.config.has_option('Playback', 'last_playing'):
|
||||
self.config.set('Playback', 'last_playing', 'false')
|
||||
|
||||
def get(self, section: str, option: str, fallback: Any = None) -> Any:
|
||||
"""
|
||||
|
||||
11
src/main.py
11
src/main.py
@ -297,6 +297,7 @@ class MainWindow(QMainWindow):
|
||||
self._saved_volume: int = 80
|
||||
self._saved_track_path: str = ""
|
||||
self._saved_position_ms: int = 0
|
||||
self._saved_playing: bool = False
|
||||
self._pending_seek_ms: int = 0
|
||||
|
||||
self._setup_ui()
|
||||
@ -353,6 +354,7 @@ class MainWindow(QMainWindow):
|
||||
self._saved_volume = config.get_int("Playback", "last_volume", fallback=80)
|
||||
self._saved_track_path = config.get("Playback", "last_track_path", fallback="")
|
||||
self._saved_position_ms = config.get_int("Playback", "last_position_ms", fallback=0)
|
||||
self._saved_playing = config.get_boolean("Playback", "last_playing", fallback=False)
|
||||
|
||||
def _save_settings(self):
|
||||
config = self.config_loader
|
||||
@ -377,6 +379,7 @@ class MainWindow(QMainWindow):
|
||||
if duration > 0 and position > duration - 2000:
|
||||
position = 0
|
||||
config.set("Playback", "last_position_ms", str(position))
|
||||
config.set("Playback", "last_playing", str(self._is_playing).lower())
|
||||
else:
|
||||
config.set("Playback", "last_track_path", "")
|
||||
config.set("Playback", "last_position_ms", "0")
|
||||
@ -832,7 +835,7 @@ class MainWindow(QMainWindow):
|
||||
self.player.durationChanged.connect(self._on_media_duration_changed)
|
||||
self.player.mediaStatusChanged.connect(self._on_media_status_changed)
|
||||
|
||||
def _play_track_from_index(self, index: int, start_position_ms: int = 0):
|
||||
def _play_track_from_index(self, index: int, start_position_ms: int = 0, auto_play: bool = True):
|
||||
"""Start playing a track from the library table by row index."""
|
||||
if index < 0 or index >= self.library_table.rowCount():
|
||||
return
|
||||
@ -845,9 +848,13 @@ class MainWindow(QMainWindow):
|
||||
|
||||
self.current_playback_index = index
|
||||
self.player.setSource(QUrl.fromLocalFile(track_data["path"]))
|
||||
if auto_play:
|
||||
self.player.play()
|
||||
self._is_playing = True
|
||||
self.play_btn.setText("\u23F8")
|
||||
else:
|
||||
self._is_playing = False
|
||||
self.play_btn.setText("\u25B6")
|
||||
|
||||
self.now_playing_label.setText(f"{track_data['artist']} \u2014 {track_data['title']}")
|
||||
self.now_playing_label.setStyleSheet(
|
||||
@ -1079,7 +1086,7 @@ class MainWindow(QMainWindow):
|
||||
continue
|
||||
_is_ready, track_data = data_item.data(Qt.ItemDataRole.UserRole)
|
||||
if track_data and track_data.get("path", "") == self._saved_track_path:
|
||||
self._play_track_from_index(row, start_position_ms=self._saved_position_ms)
|
||||
self._play_track_from_index(row, start_position_ms=self._saved_position_ms, auto_play=self._saved_playing)
|
||||
return
|
||||
|
||||
def _extract_ready_track(self, fpath: str, fname: str, handler) -> Dict[str, Any]:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user