Added support for video events

This commit is contained in:
Fabio Manganiello 2018-04-19 22:42:28 +02:00
parent 84ab37e44e
commit 2fdb8c50da
2 changed files with 52 additions and 0 deletions

View file

@ -0,0 +1,32 @@
from platypush.message.event import Event
class VideoEvent(Event):
""" Base class for music events """
def __init__(self, status, track, *args, **kwargs):
super().__init__(*args, status=status, video=video, **kwargs)
class VideoPlayEvent(VideoEvent):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class VideoStopEvent(VideoEvent):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class VideoPauseEvent(VideoEvent):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class NewPlayingVideoEvent(VideoEvent):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# vim:sw=4:ts=4:et:

View file

@ -11,6 +11,8 @@ from dbus.exceptions import DBusException
from omxplayer import OMXPlayer
from platypush.message.response import Response
from platypush.message.event.video import VideoPlayEvent, VideoPauseEvent, \
VideoStop, NewPlayingVideoEvent
from .. import Plugin
@ -18,9 +20,11 @@ class VideoOmxplayerPlugin(Plugin):
def __init__(self, args=[], *argv, **kwargs):
self.args = args
self.player = None
self.cur_video = None
self.videos_queue = []
def play(self, resource):
self.cur_video = resource
if resource.startswith('youtube:') \
or resource.startswith('https://www.youtube.com/watch?v='):
resource = self._get_youtube_content(resource)
@ -29,6 +33,7 @@ class VideoOmxplayerPlugin(Plugin):
try:
self.player = OMXPlayer(resource, args=self.args)
self._init_player_handlers()
except DBusException as e:
logging.warning('DBus connection failed: you will probably not ' +
'be able to control the media')
@ -44,6 +49,8 @@ class VideoOmxplayerPlugin(Plugin):
self.player.stop()
self.player.quit()
self.player = None
self.cur_video = None
return self.status()
def voldown(self):
@ -90,6 +97,19 @@ class VideoOmxplayerPlugin(Plugin):
'status': 'Not initialized'
}))
def _init_player_handlers(self):
if not self.player:
return
self.player.playEvent += lambda _: \
self.bus.post(VideoPlayEvent(video=self.cur_video))
self.player.pauseEvent += lambda _: \
self.bus.post(VideoPauseEvent(video=self.cur_video))
self.player.stopEvent += lambda _: \
self.bus.post(VideoStopEvent())
def youtube_search_and_play(self, query):
self.videos_queue = self.youtube_search(query)
url = self.videos_queue.pop(0)