如何使用python连续监控Rhythmbox的音轨变化 [英] How to continuously monitor rhythmbox for track change using python

查看:113
本文介绍了如何使用python连续监控Rhythmbox的音轨变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用python监视Rhythmbox中音轨的变化.我想不断检查轨道的变化并在轨道变化的情况下执行一组功能.我编写了一段代码,该代码从dbus获取Rhythmbox接口,并获取当前的音轨详细信息.但是该程序必须手动运行以检查是否有任何更改.

I want to monitor the change of track in Rhythmbox using python. I want to continuously check for change of track and execute a set of functions if the track is changed. I have written a piece of code which gets hold of the Rhythmbox interfaces from the dbus and gets the current track details. But this program has to be run manually to check for any change.

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

I am new to this and I would like to know how we can create a background process which continuously runs and checks Rhythmbox.

我不想制作Rhythmbox插件(宁可简化我的工作),因为我将扩展该应用程序以收听多个音乐播放器.

I dont want to make a Rhythmbox plugin(which rather would make my work simple) as I will be extending the application to listen to multiple music players.

请向我建议要实现该功能到底需要做什么.

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

推荐答案

每当当前歌曲更改时,Rhythmbox播放器对象(/org/gnome/Rhythmbox/Player)都会发送playingUriChanged信号.将功能连接到信号以使它在接收到信号时立即运行.这是一个示例,该示例在每首新歌曲开始时使用GLib主循环处理DBus消息来打印歌曲的标题:

The Rhythmbox player object (/org/gnome/Rhythmbox/Player) sends a playingUriChanged signal whenever the current song changes. Connect a function to the signal to have it run whenever the signal is received. Here's an example that prints the title of the song whenever a new song starts, using the GLib main 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 ()

这篇关于如何使用python连续监控Rhythmbox的音轨变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆