From 75d41c4b2cf3a8b46ad0ad4199c27d14d233a3a9 Mon Sep 17 00:00:00 2001 From: votti Date: Fri, 30 Dec 2022 17:15:57 +0100 Subject: [PATCH 1/2] Add resume to play_card Adds a resume flag to play_card to resume from position in case there is already playback information for a folder. This would be important for audiobooks. In case the resume fails (eg if the folder changed), a normal playback is done and the error logged. --- src/jukebox/components/playermpd/__init__.py | 25 ++++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/jukebox/components/playermpd/__init__.py b/src/jukebox/components/playermpd/__init__.py index 86dbc60ab..f726c15c3 100644 --- a/src/jukebox/components/playermpd/__init__.py +++ b/src/jukebox/components/playermpd/__init__.py @@ -538,7 +538,7 @@ def resume(self): self.mpd_client.play() @plugs.tag - def play_card(self, folder: str, recursive: bool = False): + def play_card(self, folder: str, recursive: bool = False, resume: bool = False): """ Main entry point for trigger music playing from RFID reader. Decodes second swipe options before playing folder content @@ -547,6 +547,7 @@ def play_card(self, folder: str, recursive: bool = False): :param folder: Folder path relative to music library path :param recursive: Add folder recursively + :param resume: Try to resume from last position? """ # Developers notes: # @@ -575,7 +576,7 @@ def play_card(self, folder: str, recursive: bool = False): # run callbacks before play_folder is invoked play_card_callbacks.run_callbacks(folder, PlayCardState.firstSwipe) - self.play_folder(folder, recursive) + self.play_folder(folder, recursive, resume=resume) @plugs.tag def get_single_coverart(self, song_url): @@ -612,7 +613,8 @@ def get_folder_content(self, folder: str): return plc.playlist @plugs.tag - def play_folder(self, folder: str, recursive: bool = False) -> None: + def play_folder(self, folder: str, recursive: bool = False, + resume: bool = False) -> None: """ Playback a music folder. @@ -621,6 +623,7 @@ def play_folder(self, folder: str, recursive: bool = False) -> None: :param folder: Folder path relative to music library path :param recursive: Add folder recursively + :param resume: Try to resume from previous state? """ # TODO: This changes the current state -> Need to save last state with self.mpd_lock: @@ -640,11 +643,23 @@ def play_folder(self, folder: str, recursive: bool = False) -> None: self.music_player_status['player_status']['last_played_folder'] = folder + # Here a reference to the folder dict is used. + # Thus any update to the current_folder_status dict will + # be reflected in the dict of the corresponding folder self.current_folder_status = self.music_player_status['audio_folder_status'].get(folder) if self.current_folder_status is None: self.current_folder_status = self.music_player_status['audio_folder_status'][folder] = {} - - self.mpd_client.play() + # Dont attempt to resume, if this is a new folder + self.mpd_client.play() + else: + if resume: + try: + self.resume() + except mpd.base.CommandError as e: + logger.exception("Failed to resume folder: %s", folder) + self.mpd_client.play() + else: + self.mpd_client.play() @plugs.tag def play_album(self, albumartist: str, album: str): From b8939b41bb40bae3d88e9690ea3d2edf3f1ce631 Mon Sep 17 00:00:00 2001 From: s-martin Date: Fri, 3 May 2024 22:06:39 +0200 Subject: [PATCH 2/2] handle exception in logger --- src/jukebox/components/playermpd/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jukebox/components/playermpd/__init__.py b/src/jukebox/components/playermpd/__init__.py index f726c15c3..67ebdbc28 100644 --- a/src/jukebox/components/playermpd/__init__.py +++ b/src/jukebox/components/playermpd/__init__.py @@ -656,7 +656,7 @@ def play_folder(self, folder: str, recursive: bool = False, try: self.resume() except mpd.base.CommandError as e: - logger.exception("Failed to resume folder: %s", folder) + logger.exception("Failed to resume folder: %s", folder, exc_info=e) self.mpd_client.play() else: self.mpd_client.play()