forked from platypush/platypush
[media.mpv] Made the event API compatible with all python-mpv versions.
The format of the `MpvEvent*` classes, the data passed to the event callback and the available event type enum fields have all changed between python-mpv < 1.0.0 and >= 1.0.0. This change makes things work with all mpv + python-mpv versions.
This commit is contained in:
parent
47728d5bbd
commit
83ced6a320
1 changed files with 25 additions and 15 deletions
|
@ -139,29 +139,39 @@ class MediaMpvPlugin(MediaPlugin):
|
||||||
|
|
||||||
self.logger.info('Received mpv event: %s', event)
|
self.logger.info('Received mpv event: %s', event)
|
||||||
|
|
||||||
|
# For python-mpv >= 1.0.0
|
||||||
if isinstance(event, MpvEvent):
|
if isinstance(event, MpvEvent):
|
||||||
event = event.as_dict()
|
event_id = event.event_id.value
|
||||||
if not isinstance(event, dict):
|
# For python-mpv < 1.0.0
|
||||||
|
elif isinstance(event, dict):
|
||||||
|
event_id = event.get('event_id')
|
||||||
|
else:
|
||||||
return
|
return
|
||||||
|
|
||||||
evt_type = event.get('event', b'').decode()
|
if event_id == 6: # START_FILE
|
||||||
if not evt_type:
|
|
||||||
return
|
|
||||||
|
|
||||||
if evt_type == 'start-file':
|
|
||||||
self._post_event(NewPlayingMediaEvent)
|
self._post_event(NewPlayingMediaEvent)
|
||||||
elif evt_type == 'playback-restart':
|
elif event_id == 21: # PLAYBACK_RESTART
|
||||||
self._post_event(MediaPlayEvent)
|
self._post_event(MediaPlayEvent)
|
||||||
elif evt_type in ('shutdown', 'idle', 'end-file'):
|
elif event_id in {1, 7, 11}: # SHUTDOWN, EOF, IDLE
|
||||||
if self._state != PlayerState.PLAY:
|
self._post_event(MediaStopEvent)
|
||||||
self._post_event(MediaStopEvent)
|
if self._player:
|
||||||
|
try:
|
||||||
if evt_type == 'shutdown' and self._player:
|
self._player.terminate()
|
||||||
self._player = None
|
except Exception as e:
|
||||||
elif evt_type == 'seek' and self._cur_player:
|
self.logger.debug(
|
||||||
|
'Error terminating mpv instance: %s', e, exc_info=True
|
||||||
|
)
|
||||||
|
self._player = None
|
||||||
|
elif event_id == 20 and self._cur_player: # SEEK
|
||||||
self._post_event(
|
self._post_event(
|
||||||
MediaSeekEvent, position=self._cur_player.playback_time
|
MediaSeekEvent, position=self._cur_player.playback_time
|
||||||
)
|
)
|
||||||
|
elif event_id == 12: # PAUSE
|
||||||
|
self._latest_state = PlayerState.PAUSE
|
||||||
|
self._post_event(MediaPauseEvent)
|
||||||
|
elif event_id == 13: # UNPAUSE
|
||||||
|
self._latest_state = PlayerState.PLAY
|
||||||
|
self._post_event(MediaResumeEvent)
|
||||||
|
|
||||||
self._latest_state = self._state
|
self._latest_state = self._state
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue