How to constantly control the rhythmbox to change a track using python

I want to track track changes in Rhythmbox using python. I want to constantly check for track changes and perform a set of functions if the track is changed. I wrote a code snippet that captures the Rhythmbox interfaces from dbus and gets the current track information. But you need to run this program manually to check for any changes.

I am new to this, and I would like to know how we can create a background process that constantly runs and validates Rhythmbox.

I don’t want to create the Rhythmbox plugin (which will make my work simpler), as I will expand the application to listen to several music players.

Please suggest me exactly what I would have to do to achieve functionality.

+4
source share
4 answers

The Rhythmbox ( /org/gnome/Rhythmbox/Player) player object sends a signal playingUriChangedwhenever the current song changes. Connect the function to the signal so that it starts whenever the signal is received. Here's an example that prints the name of a song whenever a new song starts, using the main GLib loop to process DBus messages:

#! /usr/bin/env python

import dbus
import dbus.mainloop.glib
import glib

# This gets called whenever Rhythmbox sends the playingUriChanged signal
def playing_song_changed (uri):
    global shell
    if uri != "":
        song = shell.getSongProperties (uri)
        print "Now playing: {0}".format (song["title"])
    else:
        print "Not playing anything"

dbus.mainloop.glib.DBusGMainLoop (set_as_default = True)

bus = dbus.SessionBus ()

proxy = bus.get_object ("org.gnome.Rhythmbox", "/org/gnome/Rhythmbox/Player")
player = dbus.Interface (proxy, "org.gnome.Rhythmbox.Player")
player.connect_to_signal ("playingUriChanged", playing_song_changed)

proxy = bus.get_object ("org.gnome.Rhythmbox", "/org/gnome/Rhythmbox/Shell")
shell = dbus.Interface (proxy, "org.gnome.Rhythmbox.Shell")

# Run the GLib event loop to process DBus signals as they arrive
mainloop = glib.MainLoop ()
mainloop.run ()
+12
source

Take a look at the Conky script here:

https://launchpad.net/~conkyhardcore/+archive/ppa/+files/conkyrhythmbox_2.12.tar.gz

This uses dbus to communicate with rhythmbox, for example:

bus = dbus.SessionBus()
remote_object_shell = bus.get_object('org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Shell')
iface_shell = dbus.Interface(remote_object_shell, 'org.gnome.Rhythmbox.Shell')
remote_object_player = bus.get_object('org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Player')
iface_player = dbus.Interface(remote_object_player, 'org.gnome.Rhythmbox.Player')

iface_player, . , . dbus , -. :

http://ubuntuforums.org/showthread.php?t=156706

+1

Ubuntu 14.04.1, script Rhythmbox 3. script ~/.now_playing BUTT, . Rhythmbox MPRIS, :

http://specifications.freedesktop.org/mpris-spec/latest/index.html

#!/usr/bin/python

import dbus
import dbus.mainloop.glib
import glib

# This gets called whenever Rhythmbox sends the playingUriChanged signal
def playing_song_changed (Player,two,three):
    global iface
    global track
    global home
    track2 = iface.Get(Player,"Metadata").get(dbus.String(u'xesam:artist'))[0] + " - "+ iface.Get(Player,"Metadata").get(dbus.String(u'xesam:title'))

    if track != track2:
        track = iface.Get(Player,"Metadata").get(dbus.String(u'xesam:artist'))[0] + " - "+ iface.Get(Player,"Metadata").get(dbus.String(u'xesam:title'))
        f = open( home + '/.now_playing', 'w' )
        f.write( track + '\n' )
        f.close()


dbus.mainloop.glib.DBusGMainLoop (set_as_default = True)

bus = dbus.SessionBus ()
from os.path import expanduser
home = expanduser("~")
player = bus.get_object ("org.mpris.MediaPlayer2.rhythmbox", "/org/mpris/MediaPlayer2")
iface = dbus.Interface (player, "org.freedesktop.DBus.Properties")

track = iface.Get("org.mpris.MediaPlayer2.Player","Metadata").get(dbus.String(u'xesam:artist'))[0] + " - "+ iface.Get("org.mpris.MediaPlayer2.Player","Metadata").get(dbus.Strin$
f = open( home + "/.now_playing", 'w' )
f.write( track + '\n' )
f.close()

iface.connect_to_signal ("PropertiesChanged", playing_song_changed)

# Run the GLib event loop to process DBus signals as they arrive
mainloop = glib.MainLoop ()
mainloop.run ()
+1

- :

from time import sleep

execute = True
while execute:
    your_function_call()
    sleep(30) # in seconds; prevent busy polling

It should work fine. If it was connected to something that listened for signals ( import signal) so that you could set the execution to False when someone ctrl-c the application, this would be basically what you need.

Otherwise, you have Google to demonize (which includes the sailing process a couple of times); from memory there is even a decent Python library (which requires 2.5 / 2.6 from memory with), which would facilitate this side of things :).

-2
source

All Articles