Fix Plex Companion device restarting playback when reconnecting to PKC

This commit is contained in:
croneter 2019-08-05 18:21:54 +02:00
parent 9139669f44
commit b1d59e65be
2 changed files with 53 additions and 6 deletions

View file

@ -129,6 +129,21 @@ class Playqueue_Object(object):
self.kodi_playlist_playback = False self.kodi_playlist_playback = False
LOG.debug('Playlist cleared: %s', self) LOG.debug('Playlist cleared: %s', self)
def position_from_plex_id(self, plex_id):
"""
Returns the position [int] for the very first item with plex_id [int]
(Plex seems uncapable of adding the same element multiple times to a
playqueue or playlist)
Raises KeyError if not found
"""
for position, item in enumerate(self.items):
if item.plex_id == plex_id:
break
else:
raise KeyError('Did not find plex_id %s in %s', plex_id, self)
return position
class Playlist_Item(object): class Playlist_Item(object):
""" """

View file

@ -55,7 +55,16 @@ def update_playqueue_from_PMS(playqueue,
except AttributeError: except AttributeError:
LOG.error('Could now download playqueue %s', playqueue_id) LOG.error('Could now download playqueue %s', playqueue_id)
return return
playqueue.clear() if playqueue.id == playqueue_id:
# This seems to be happening ONLY if a Plex Companion device
# reconnects and Kodi is already playing something - silly, really
# For all other cases, a new playqueue is generated by Plex
LOG.debug('Update for existing playqueue detected')
new = False
else:
new = True
playqueue.clear()
# Get new metadata for the playqueue first
try: try:
PL.get_playlist_details_from_xml(playqueue, xml) PL.get_playlist_details_from_xml(playqueue, xml)
except PL.PlaylistError: except PL.PlaylistError:
@ -63,10 +72,33 @@ def update_playqueue_from_PMS(playqueue,
return return
playqueue.repeat = 0 if not repeat else int(repeat) playqueue.repeat = 0 if not repeat else int(repeat)
playqueue.plex_transient_token = transient_token playqueue.plex_transient_token = transient_token
playback.play_xml(playqueue, if new:
xml, playback.play_xml(playqueue,
offset=offset, xml,
start_plex_id=start_plex_id) offset=offset,
start_plex_id=start_plex_id)
return
# Updates to playqueues could potentially become a bit more ugly...
if app.APP.is_playing:
try:
playerid = js.get_player_ids()[0]
except IndexError:
LOG.error('Unexpectately could not get Kodi player id')
return
if app.PLAYSTATE.player_states[playerid]['plex_id'] == start_plex_id:
# Nothing to do - let's not seek to avoid jumps in playback
return
pos = playqueue.position_from_plex_id(start_plex_id)
LOG.debug('Skipping to position %s for %s', pos, playqueue)
js.skipto(pos)
if offset:
js.seek_to(offset)
return
# Need to initiate playback again using our existing playqueue
app.APP.player.play(playqueue.kodi_pl,
None,
False,
playqueue.position_from_plex_id(start_plex_id))
class PlexCompanion(backgroundthread.KillableThread): class PlexCompanion(backgroundthread.KillableThread):
@ -165,7 +197,7 @@ class PlexCompanion(backgroundthread.KillableThread):
update_playqueue_from_PMS(playqueue, update_playqueue_from_PMS(playqueue,
playqueue_id=container_key, playqueue_id=container_key,
repeat=query.get('repeat'), repeat=query.get('repeat'),
offset=data.get('offset'), offset=utils.cast(int, data.get('offset')),
transient_token=data.get('token'), transient_token=data.get('token'),
start_plex_id=key) start_plex_id=key)