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 committed Jan 4, 2023
1 parent 0326c9a commit a700acf
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 @@ -418,7 +418,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 @@ -427,6 +427,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 All @@ -447,7 +448,7 @@ def play_card(self, folder: str, recursive: bool = False):
self.second_swipe_action()
else:
logger.debug('Calling first swipe action')
self.play_folder(folder, recursive)
self.play_folder(folder, recursive, resume=resume)

@plugs.tag
def get_folder_content(self, folder: str):
Expand All @@ -463,7 +464,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 @@ -472,6 +474,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 @@ -491,11 +494,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 a700acf

Please sign in to comment.