Clean up running VLC instance on stop and new media play

This commit is contained in:
Fabio Manganiello 2019-02-21 19:24:43 +01:00
parent 401a538339
commit 749436c452

View file

@ -37,12 +37,10 @@ class MediaVlcPlugin(MediaPlugin):
:type volume: int :type volume: int
""" """
import vlc
super().__init__(*argv, **kwargs) super().__init__(*argv, **kwargs)
self._args = args or [] self._args = args or []
self._instance = vlc.Instance(*self._args) self._instance = None
self._player = None self._player = None
self._latest_seek = None self._latest_seek = None
self._default_fullscreen = fullscreen self._default_fullscreen = fullscreen
@ -65,19 +63,29 @@ class MediaVlcPlugin(MediaPlugin):
] if hasattr(vlc.EventType, evt)] ] if hasattr(vlc.EventType, evt)]
def _init_vlc(self, resource): def _init_vlc(self, resource):
if self._player: import vlc
self._player.stop() self._reset_state()
self._player = None
for k,v in self._env.items(): for k,v in self._env.items():
os.environ[k] = v os.environ[k] = v
self._instance = vlc.Instance(*self._args)
self._player = self._instance.media_player_new(resource) self._player = self._instance.media_player_new(resource)
for evt in self._watched_event_types(): for evt in self._watched_event_types():
self._player.event_manager().event_attach( self._player.event_manager().event_attach(
eventtype=evt, callback=self._event_callback()) eventtype=evt, callback=self._event_callback())
def _reset_state(self):
self._latest_seek = None
if self._player:
self._player.release()
self._player = None
if self._instance:
self._instance.release()
self._instance = None
def _event_callback(self): def _event_callback(self):
def callback(event): def callback(event):
from vlc import EventType from vlc import EventType
@ -90,11 +98,8 @@ class MediaVlcPlugin(MediaPlugin):
bus.post(MediaPauseEvent()) bus.post(MediaPauseEvent())
elif event.type == EventType.MediaPlayerStopped or \ elif event.type == EventType.MediaPlayerStopped or \
event.type == EventType.MediaPlayerEndReached: event.type == EventType.MediaPlayerEndReached:
self._reset_state()
bus.post(MediaStopEvent()) bus.post(MediaStopEvent())
if self._player:
self._player.release()
self._player = None
self._latest_seek = None
for callback in self._on_stop_callbacks: for callback in self._on_stop_callbacks:
callback() callback()
elif event.type == EventType.MediaPlayerTitleChanged: elif event.type == EventType.MediaPlayerTitleChanged: