Created
April 17, 2011 15:22
-
-
Save shadeslayer/924118 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
# vim: set sts=4 sw=4 et tw=0: | |
# | |
# Author(s): Rohan Garg <[email protected]> | |
# License: GPL (2011) | |
# | |
import players.base | |
from gi.repository import Gio, GLib | |
import time | |
class Player(players.base.Player): | |
def __init__(self): | |
#self.sessionBus = dbus.SessionBus() | |
#self.player = self.sessionBus.get_object("org.mpris.amarok", | |
# "/Player") | |
self.BUS_NAME = "org.mpris.MediaPlayer2.amarok" | |
self.PLAYER_OBJ_NAME = "/org/mpris/MediaPlayer2" | |
self.PLAYER_IFACE_NAME = "org.mpris.MediaPlayer2.Player" | |
self.VOL_IFACE_NAME = "org.kde.amarok.Mpris2Extensions.Player" | |
self.player = self._get_proxy(obj_name=self.PLAYER_OBJ_NAME, | |
iface_name=self.PLAYER_IFACE_NAME) | |
self.vol_proxy = self._get_proxy(obj_name=self.PLAYER_OBJ_NAME, | |
iface_name=self.VOL_IFACE_NAME) | |
print(self.player.PlaybackStatus()) | |
def _get_proxy(self, obj_name, iface_name): | |
flags = Gio.DBusProxyFlags.DO_NOT_AUTO_START | Gio.DBusProxyFlags.DO_NOT_LOAD_PROPERTIES | |
return Gio.DBusProxy.new_for_bus_sync(Gio.BusType.SESSION, flags, | |
Gio.DBusInterfaceInfo(), | |
self.BUS_NAME, obj_name, | |
iface_name, None) | |
def is_running(self): | |
"""Is the player running?""" | |
status = self.player.PlaybackStatus | |
if status == "Playing" or status == "Paused" or status == "Stopped" : | |
return True | |
return False | |
def is_playing(self): | |
"""Is the player playing""" | |
"""Introduce a time delay, DBus seems to lag with python""" | |
time.sleep(0.1) | |
if self.player.PlaybackStatus() == "Playing": | |
return True | |
else: | |
return False | |
def status(self): | |
"""Returns the current song""" | |
"""Introduce a time delay, DBus seems to lag with python""" | |
time.sleep(0.1) | |
msg = None | |
if self.is_playing(): | |
msg = "[Playing]" | |
else: | |
msg = "[Paused]" | |
metadata = self.player.Metadata() | |
return msg + ' "%s" by "%s" from "%s"' % (metadata['title'], | |
metadata['artist'], | |
metadata['album']) | |
def get_volume(self): | |
"""Get the current volume""" | |
return str(round(self.player.Volume())) | |
def volume_up(self): | |
"""Increase volume by 10%""" | |
return self.vol_proxy.AdjustVolume(0.10) | |
def volume_down(self): | |
"""Decrease volume by 10%""" | |
return self.vol_proxy.AdjustVolume(-0.10) | |
def volume_mute(self): | |
"""Mute volume""" | |
return self.vol_proxy.AdjustVolume(-1) | |
def volume_max(self): | |
"""Set volume to maximum""" | |
return self.vol_proxy.AdjustVolume(1) | |
def next(self): | |
"""Next song in playlist""" | |
return self.player.Next() | |
def previous(self): | |
"""Previous song in playlist""" | |
return self.player.Prev() | |
def play_pause(self): | |
"""Toggle play/pause on current song""" | |
return self.player.PlayPause() | |
def play(self): | |
"""Play current song""" | |
if not self.is_playing(): | |
return self.play_pause() | |
def pause(self): | |
"""Pause current song""" | |
if self.is_playing(): | |
return self.play_pause() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment