2010-10-12 20 views
4

Je veux surveiller le changement de piste dans Rhythmbox en utilisant python. Je veux vérifier en permanence le changement de piste et exécuter un ensemble de fonctions si la piste est modifiée. J'ai écrit un morceau de code qui récupère les interfaces Rhythmbox du dbus et obtient les détails de la piste en cours. Mais ce programme doit être exécuté manuellement pour vérifier tout changement.Comment surveiller en continu rhythmbox pour le changement de piste en utilisant python

Je suis nouveau dans ce domaine et j'aimerais savoir comment nous pouvons créer un processus d'arrière-plan qui exécute et vérifie en continu Rhythmbox.

Je ne veux pas faire un plugin Rhythmbox (ce qui rendrait plutôt mon travail simple) car je vais étendre l'application pour écouter plusieurs lecteurs de musique.

S'il vous plaît me suggérer ce que je devrais faire exactement pour atteindre la fonctionnalité.

Répondre

-2

Quelque chose comme:

from time import sleep 

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

devrait fonctionner très bien. Si cela a été branché à quelque chose qui a écouté les signaux (import signal) de sorte que vous pouvez définir l'exécution de False lorsque quelqu'un ctrl-c est l'application, ce serait essentiellement ce que vous recherchez.

Dans le cas contraire, avoir une démonisation de Google (ce qui implique de forcer le processus plusieurs fois); de mémoire, il y a même une bibliothèque Python décente (qui, de mémoire, nécessite des instructions 2.5/2.6 with) qui faciliteraient ce côté des choses :).

+0

S'il vous plaît voir [Process Management] (http://mywiki.wooledge.org/ProcessManagement) et double fourche plus, mon fils ! –

1

Jetez un oeil à le script Conky ici:

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

qui utilise le dbus pour parler à Rhythmbox, comme ceci:

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') 

Vous pouvez appeler un certain nombre de fonctions sur iface_player à obtenir les informations requises. Il semble que vous deviez interroger cet exemple. Si vous voulez recevoir un message de dbus lors du changement de piste, vous devrez le faire différemment. Cela traite de pistes à explorer:

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

12

L'objet lecteur Rhythmbox (/org/gnome/Rhythmbox/Player) envoie un signal playingUriChanged chaque fois que le morceau change en cours. Connectez une fonction au signal pour qu'il s'exécute chaque fois que le signal est reçu. Voici un exemple qui imprime le titre de la chanson à chaque fois qu'une nouvelle chanson commence, en utilisant la boucle principale GLib pour traiter les messages DBus:

#! /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() 
+2

C'est génial. Un de ces jours, je vais trouver un bon usage pour cela. :) – sdolan

+0

Je l'ai essayé, mais j'ai eu une erreur - http://s020.radikal.ru/i723/1308/e5/2046183ed28e.png comment peut-il être résolu? – Tebe

1

J'utilise Ubuntu 14.04.1 et le script ci-dessus est dépréciée pour Rhythmbox 3. J'utilise ce script pour écrire la chanson en cours à ~/.now_playing pour BUTT à lire, mais vous pouvez le mettre à jour pour vos besoins. Rhythmbox utilise MPRIS maintenant et vous pouvez obtenir des informations ici:

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()