如何使用python连续监视rhythmbox的轨道变化

Man*_*ane 4 python dbus monitor rhythmbox

我想用python监视Rhythmbox中轨道的变化.我想连续检查曲目的变化并在曲目改变时执行一组功能.我编写了一段代码,可以从dbus中获取Rhythmbox接口并获取当前的跟踪详细信息.但是必须手动运行该程序以检查是否有任何变化.

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

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

请建议我到底要做些什么来实现这个功能.

Pau*_*icz 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 ()
Run Code Online (Sandbox Code Playgroud)

  • 这太棒了.有一天我会发现它很有用.:) (2认同)