12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- # 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
- <https://github.com/LEW21/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:
- 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 confidence(self, text):
- """Return whether or not the command can be handled"""
- if text not in self.corpus_strings:
- return 0
- if self._mpris_proxy() is None:
- return 0
- return 1
-
- def handle(self, text):
- """Print and speak the phrase when it is heard"""
- # Is there a matching command?
- if self.confidence(text):
- self.commands[text]()
- return True
- else:
- return False
|