2010-10-12 65 views
4

我想使用python监控Rhythmbox中的跟踪更改。如果轨道改变,我想持续检查轨道的变化并执行一组功能。我写了一段代码,可以从dbus获取Rhythmbox接口,并获取当前曲目的详细信息。但是这个程序必须手动运行以检查是否有任何变化。如何使用python连续监控rhythmbox以进行跟踪更改

我是新手,我想知道如何创建一个连续运行并检查Rhythmbox的后台进程。

我不想制作一个Rhythmbox插件(这会让我的工作变得简单),因为我将扩展应用程序以倾听多个音乐播放器。

请建议我到底需要做些什么来实现功能。

回答

-2

喜欢的东西:

from time import sleep 

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

应该只是罚款。如果这是连接到听信号的东西(import signal),以便您可以将某个ctrl-c的应用程序设置为False时,那基本上就是您要的。否则,让Google进行守护进程(涉及分几次);从内存来看,甚至还有一个像样的Python库(从内存中来说,需要2.5/2.6 with语句),这将有助于使事情变得更容易:)。

+0

请参见[流程管理(http://mywiki.wooledge.org/ProcessManagement)和双叉没有了,我的儿子! – 2011-03-17 20:11:21

1

看看Conky的脚本在这里:

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

使用DBUS说话的Rhythmbox,像这样:

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

12

的Rhythmbox的选手对象(/org/gnome/Rhythmbox/Player)发送一个信号playingUriChanged每当当前歌曲的改变。每当接收到信号时,将一个功能连接到信号使其运行。这里是每当一个新的歌曲开始,打印歌曲的标题为例,使用GLib的主循环处理的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

这太棒了。其中一天,我会弄清楚它的一个好用处。 :) – sdolan 2010-11-06 03:29:47

+0

我试过了,但是我得到一个错误 - http://s020.radikal.ru/i723/1308/e5/2046183ed28e.png它怎么解决? – Tebe 2013-08-27 16:06:08

1

我使用Ubuntu 14.04.1和上面的脚本已经废弃了的Rhythmbox 3。我正在使用此脚本将当前歌曲写入〜/ .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()