Skip to content

Commit

Permalink
Add resume to play_card
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
votti authored and Vito Zanotelli committed Aug 2, 2024
1 parent f2a1730 commit 75d41c4
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/jukebox/components/playermpd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
#
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand All @@ -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):
Expand Down

0 comments on commit 75d41c4

Please sign in to comment.