# This is part of Kaylee # -- this code is licensed GPLv3 # Copyright 2015-2017 Clayton G. Hobbs # Portions Copyright 2013 Jezra """MPRIS media player control plugin This plugin allows control of a media player supporting the Media Player Remote Interfacing Specification (MPRIS) D-Bus interface. To use this plugin, you must first install `pydbus `__ for Python 3. This plugin takes no configuration, so load it as follows:: ".mpris": {} Note: if your microphone can hear the audio produced by your speakers, it is recommended to load PulseAudio's module-echo-cancel when using this plugin. Recognition while music is playing may still be mediocre, but without module-echo-cancel it has almost no chance of working. """ from pydbus import SessionBus from .pluginbase import PluginBase class Plugin(PluginBase): """Main class for the MPRIS plugin""" def __init__(self, config, name): """Initialize the MPRIS plugin""" super().__init__(config, name) # Get the D-Bus proxy object self._bus = SessionBus() self._dbus_proxy = self._bus.get('.DBus') self.commands = { 'next song': self._next, 'next video': self._next, 'pause music': self._pause, 'pause video': self._pause, 'play music': self._play, 'play video': self._play, 'previous song': self._previous, 'previous video': self._previous } self.corpus_strings.update(self.commands) def _mpris_proxy(self): """Get a proxy for the first MPRIS media player, or None""" try: # Get the bus name of the first MPRIS media player bus_name = [name for name in self._dbus_proxy.ListNames() if name.startswith('org.mpris.MediaPlayer2')][0] # Get the proxy return self._bus.get(bus_name, '/org/mpris/MediaPlayer2') except IndexError: print('No media player found') return None def _next(self): p = self._mpris_proxy() if p is not None: p.Next() def _pause(self): p = self._mpris_proxy() if p is not None: p.Pause() def _play(self): p = self._mpris_proxy() if p is not None: p.Play() def _previous(self): p = self._mpris_proxy() if p is not None: p.Previous() def recognizer_finished(self, recognizer, text): """Print and speak the phrase when it is heard""" # Is there a matching command? if text in self.corpus_strings: self.emit('processed', text) self.commands[text]() return True else: return False