Skip to content

Instantly share code, notes, and snippets.

@shadeslayer
Created April 17, 2011 16:44
Show Gist options
  • Save shadeslayer/924206 to your computer and use it in GitHub Desktop.
Save shadeslayer/924206 to your computer and use it in GitHub Desktop.
#!/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
#import dbus
import time
from gi.repository import Gio, GLib
class Player(players.base.Player):
def __init__(self):
self.BUS_NAME = "org.mpris.amarok"
self.PLAYER_OBJ_NAME = "/Player"
self.PLAYER_IFACE_NAME = "org.freedesktop.MediaPlayer"
self.player_proxy = self._get_proxy(obj_name=self.PLAYER_OBJ_NAME,
iface_name=self.PLAYER_IFACE_NAME)
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?"""
time.sleep(0.01)
status = self.player_proxy.GetStatus()
if status[3] == 1:
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.01)
if self.player_proxy.GetStatus()[0] == 0:
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.01)
msg = None
if self.is_playing():
msg = "[Playing]"
else:
msg = "[Paused]"
metadata = self.player_proxy.GetMetadata()
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_proxy.VolumeGet()))
def volume_up(self):
"""Increase volume by 10%"""
return self.player_proxy.VolumeUp(10)
def volume_down(self):
"""Decrease volume by 10%"""
return self.player_proxy.VolumeDown(10)
def volume_mute(self):
"""Mute volume"""
return self.player_proxy.Mute()
def volume_max(self):
"""Set volume to maximum"""
return self.player_proxy.VolumeSet(100)
def next(self):
"""Next song in playlist"""
return self.player_proxy.Next()
def previous(self):
"""Previous song in playlist"""
return self.player_proxy.Prev()
def play_pause(self):
"""Toggle play/pause on current song"""
return self.player_proxy.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