MPV.Event.IDLE should also be treated as a player stop event

This commit is contained in:
Fabio Manganiello 2019-12-07 17:16:48 +01:00
parent d6f251989c
commit f378fa8832

View file

@ -38,6 +38,7 @@ class MediaMpvPlugin(MediaPlugin):
self.args = self._default_mpv_args self.args = self._default_mpv_args
if args: if args:
# noinspection PyTypeChecker
self.args.update(args) self.args.update(args)
self._player = None self._player = None
@ -55,7 +56,7 @@ class MediaMpvPlugin(MediaPlugin):
os.environ[k] = v os.environ[k] = v
self._player = mpv.MPV(**mpv_args) self._player = mpv.MPV(**mpv_args)
self._player.register_event_callback(self._event_callback()) self._player.event_callbacks += self._event_callback()
@staticmethod @staticmethod
def _post_event(evt_type, **evt): def _post_event(evt_type, **evt):
@ -83,7 +84,7 @@ class MediaMpvPlugin(MediaPlugin):
self._post_event(MediaPauseEvent, resource=self._get_current_resource(), title=self._player.filename) self._post_event(MediaPauseEvent, resource=self._get_current_resource(), title=self._player.filename)
elif evt == Event.UNPAUSE: elif evt == Event.UNPAUSE:
self._post_event(MediaPlayEvent, resource=self._get_current_resource(), title=self._player.filename) self._post_event(MediaPlayEvent, resource=self._get_current_resource(), title=self._player.filename)
elif evt == Event.SHUTDOWN or ( elif evt == Event.SHUTDOWN or evt == Event.IDLE or (
evt == Event.END_FILE and event.get('event', {}).get('reason') in evt == Event.END_FILE and event.get('event', {}).get('reason') in
[EndFile.EOF_OR_INIT_FAILURE, EndFile.ABORTED, EndFile.QUIT]): [EndFile.EOF_OR_INIT_FAILURE, EndFile.ABORTED, EndFile.QUIT]):
playback_rebounced = self._playback_rebounce_event.wait(timeout=0.5) playback_rebounced = self._playback_rebounce_event.wait(timeout=0.5)
@ -171,14 +172,14 @@ class MediaMpvPlugin(MediaPlugin):
""" Volume down by (default: 10)% """ """ Volume down by (default: 10)% """
if not self._player: if not self._player:
return None, 'No mpv instance is running' return None, 'No mpv instance is running'
return self.set_volume(self._player.volume-step) return self.set_volume(self._player.volume - step)
@action @action
def volup(self, step=10.0): def volup(self, step=10.0):
""" Volume up by (default: 10)% """ """ Volume up by (default: 10)% """
if not self._player: if not self._player:
return None, 'No mpv instance is running' return None, 'No mpv instance is running'
return self.set_volume(self._player.volume+step) return self.set_volume(self._player.volume + step)
@action @action
def set_volume(self, volume): def set_volume(self, volume):
@ -207,7 +208,7 @@ class MediaMpvPlugin(MediaPlugin):
return None, 'No mpv instance is running' return None, 'No mpv instance is running'
if not self._player.seekable: if not self._player.seekable:
return None, 'The resource is not seekable' return None, 'The resource is not seekable'
pos = min(self._player.time_pos+self._player.time_remaining, pos = min(self._player.time_pos + self._player.time_remaining,
max(0, position)) max(0, position))
self._player.time_pos = pos self._player.time_pos = pos
return self.status() return self.status()
@ -219,7 +220,7 @@ class MediaMpvPlugin(MediaPlugin):
return None, 'No mpv instance is running' return None, 'No mpv instance is running'
if not self._player.seekable: if not self._player.seekable:
return None, 'The resource is not seekable' return None, 'The resource is not seekable'
pos = max(0, self._player.time_pos-offset) pos = max(0, self._player.time_pos - offset)
return self.seek(pos) return self.seek(pos)
@action @action
@ -229,8 +230,8 @@ class MediaMpvPlugin(MediaPlugin):
return None, 'No mpv instance is running' return None, 'No mpv instance is running'
if not self._player.seekable: if not self._player.seekable:
return None, 'The resource is not seekable' return None, 'The resource is not seekable'
pos = min(self._player.time_pos+self._player.time_remaining, pos = min(self._player.time_pos + self._player.time_remaining,
self._player.time_pos+offset) self._player.time_pos + offset)
return self.seek(pos) return self.seek(pos)
@action @action
@ -267,6 +268,7 @@ class MediaMpvPlugin(MediaPlugin):
""" Toggle the fullscreen mode """ """ Toggle the fullscreen mode """
return self.toggle_property('fullscreen') return self.toggle_property('fullscreen')
# noinspection PyShadowingBuiltins
@action @action
def toggle_property(self, property): def toggle_property(self, property):
""" """
@ -285,6 +287,7 @@ class MediaMpvPlugin(MediaPlugin):
setattr(self._player, property, value) setattr(self._player, property, value)
return {property: value} return {property: value}
# noinspection PyShadowingBuiltins
@action @action
def get_property(self, property): def get_property(self, property):
""" """
@ -307,13 +310,14 @@ class MediaMpvPlugin(MediaPlugin):
if not self._player: if not self._player:
return None, 'No mpv instance is running' return None, 'No mpv instance is running'
for k,v in props.items(): for k, v in props.items():
setattr(self._player, k, v) setattr(self._player, k, v)
return props return props
@action @action
def set_subtitles(self, filename, *args, **kwargs): def set_subtitles(self, filename, *args, **kwargs):
""" Sets media subtitles from filename """ """ Sets media subtitles from filename """
# noinspection PyTypeChecker
return self.set_property(subfile=filename, sub_visibility=True) return self.set_property(subfile=filename, sub_visibility=True)
@action @action
@ -339,13 +343,13 @@ class MediaMpvPlugin(MediaPlugin):
""" """
if not self._player: if not self._player:
return self.play(resource, **args) return self.play(resource, **args)
return self._player.loadfile(resource, mode='append-play', **args) return self._player.loadfile(resource, mode='append-play')
@action @action
def mute(self): def mute(self):
""" Toggle mute state """ """ Toggle mute state """
if not self._player: if not self._player:
return (None, 'No mpv instance is running') return None, 'No mpv instance is running'
mute = not self._player.mute mute = not self._player.mute
self._player.mute = mute self._player.mute = mute
return {'muted': mute} return {'muted': mute}
@ -378,8 +382,7 @@ class MediaMpvPlugin(MediaPlugin):
'audio_channels': getattr(self._player, 'audio_channels'), 'audio_channels': getattr(self._player, 'audio_channels'),
'audio_codec': getattr(self._player, 'audio_codec_name'), 'audio_codec': getattr(self._player, 'audio_codec_name'),
'delay': getattr(self._player, 'delay'), 'delay': getattr(self._player, 'delay'),
'duration': getattr(self._player, 'playback_time', 0) + 'duration': getattr(self._player, 'playback_time', 0) + getattr(self._player, 'playtime_remaining', 0)
getattr(self._player, 'playtime_remaining', 0)
if getattr(self._player, 'playtime_remaining') else None, if getattr(self._player, 'playtime_remaining') else None,
'filename': getattr(self._player, 'filename'), 'filename': getattr(self._player, 'filename'),
'file_size': getattr(self._player, 'file_size'), 'file_size': getattr(self._player, 'file_size'),
@ -410,5 +413,4 @@ class MediaMpvPlugin(MediaPlugin):
return ('file://' if os.path.isfile(self._player.stream_path) return ('file://' if os.path.isfile(self._player.stream_path)
else '') + self._player.stream_path else '') + self._player.stream_path
# vim:sw=4:ts=4:et: # vim:sw=4:ts=4:et: