Skip to content

Commit a700acf

Browse files
committed
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.
1 parent 0326c9a commit a700acf

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/jukebox/components/playermpd/__init__.py

+20-5
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ def resume(self):
418418
self.mpd_client.play()
419419

420420
@plugs.tag
421-
def play_card(self, folder: str, recursive: bool = False):
421+
def play_card(self, folder: str, recursive: bool = False, resume: bool = False):
422422
"""
423423
Main entry point for trigger music playing from RFID reader. Decodes second swipe options before playing folder content
424424
@@ -427,6 +427,7 @@ def play_card(self, folder: str, recursive: bool = False):
427427
428428
:param folder: Folder path relative to music library path
429429
:param recursive: Add folder recursively
430+
:param resume: Try to resume from last position?
430431
"""
431432
# Developers notes:
432433
#
@@ -447,7 +448,7 @@ def play_card(self, folder: str, recursive: bool = False):
447448
self.second_swipe_action()
448449
else:
449450
logger.debug('Calling first swipe action')
450-
self.play_folder(folder, recursive)
451+
self.play_folder(folder, recursive, resume=resume)
451452

452453
@plugs.tag
453454
def get_folder_content(self, folder: str):
@@ -463,7 +464,8 @@ def get_folder_content(self, folder: str):
463464
return plc.playlist
464465

465466
@plugs.tag
466-
def play_folder(self, folder: str, recursive: bool = False) -> None:
467+
def play_folder(self, folder: str, recursive: bool = False,
468+
resume: bool = False) -> None:
467469
"""
468470
Playback a music folder.
469471
@@ -472,6 +474,7 @@ def play_folder(self, folder: str, recursive: bool = False) -> None:
472474
473475
:param folder: Folder path relative to music library path
474476
:param recursive: Add folder recursively
477+
:param resume: Try to resume from previous state?
475478
"""
476479
# TODO: This changes the current state -> Need to save last state
477480
with self.mpd_lock:
@@ -491,11 +494,23 @@ def play_folder(self, folder: str, recursive: bool = False) -> None:
491494

492495
self.music_player_status['player_status']['last_played_folder'] = folder
493496

497+
# Here a reference to the folder dict is used.
498+
# Thus any update to the current_folder_status dict will
499+
# be reflected in the dict of the corresponding folder
494500
self.current_folder_status = self.music_player_status['audio_folder_status'].get(folder)
495501
if self.current_folder_status is None:
496502
self.current_folder_status = self.music_player_status['audio_folder_status'][folder] = {}
497-
498-
self.mpd_client.play()
503+
# Dont attempt to resume, if this is a new folder
504+
self.mpd_client.play()
505+
else:
506+
if resume:
507+
try:
508+
self.resume()
509+
except mpd.base.CommandError as e:
510+
logger.exception("Failed to resume folder: %s", folder)
511+
self.mpd_client.play()
512+
else:
513+
self.mpd_client.play()
499514

500515
@plugs.tag
501516
def play_album(self, albumartist: str, album: str):

0 commit comments

Comments
 (0)