如何使用重新访问的python持续监控节奏盒以进行音轨变化

Posted

技术标签:

【中文标题】如何使用重新访问的python持续监控节奏盒以进行音轨变化【英文标题】:How to continuously monitor rhythmbox for track change using python revisited 【发布时间】:2014-07-09 09:42:21 【问题描述】:

从this SO answer 运行以下 Python 代码

#!/usr/bin/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 ()

在 Ubuntu 12.04 和 14.04 上都会出现以下错误:

Traceback (most recent call last):
  File "./test.py", line 20, in <module>
    proxy = bus.get_object ("org.gnome.Rhythmbox", "/org/gnome/Rhythmbox/Player")
  File "/usr/lib/python2.7/dist-packages/dbus/bus.py", line 241, in get_object
    follow_name_owner_changes=follow_name_owner_changes)
  File "/usr/lib/python2.7/dist-packages/dbus/proxies.py", line 248, in __init__
    self._named_service = conn.activate_name_owner(bus_name)
  File "/usr/lib/python2.7/dist-packages/dbus/bus.py", line 180, in activate_name_owner
    self.start_service_by_name(bus_name)
  File "/usr/lib/python2.7/dist-packages/dbus/bus.py", line 278, in start_service_by_name
    'su', (bus_name, flags)))
  File "/usr/lib/python2.7/dist-packages/dbus/connection.py", line 651, in call_blocking
    message, timeout)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Rhythmbox was not provided by any .service files

我做错了什么?

或者,如果有另一种在 Python 上收听 DBUS 信号的方法也会有所帮助。

【问题讨论】:

您确定 Rhythmbox 已配置为在您的系统上使用 DBUS 吗?该错误表明没有提供org.gnome.Rhythmbox 服务。 我 enabled all plugins 但也许我只是没有安装正确的插件。我 90% 确定代码可以正常工作,只是我缺少一些包或 Rhythmbox 存在一些配置问题。 从 Ubuntu 软件中心获取 d-feet,连接到会话总线,看看是否可以在列表中找到节奏盒。这至少会缩小一些范围 【参考方案1】:

这适用于 Rhythmbox 3。我将其更改为将当前歌曲写入文件 ( ~/.now_playing ),但您可以根据需要对其进行更新:

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

【讨论】:

【参考方案2】:

结果我被所有的首字母缩写词弄糊涂了,找错了树。

在 Rhythmbox 3 之前,Rhythmbox 是通过 DBUS 协议上自己的特殊 API 控制的。

在 Rhythmbox 3 之后,Rhythmbox(通过 MPRIS2 DBUS 插件)通过 DBUS 协议上的通用 MPRIS2 API 进行控制。

所以谷歌搜索“Rhythmbox DBUS”让我大吃一惊。

对于任何试图完成与我相同的事情的人:只需谷歌“MPRIS2 $DESIRED LANGUAGE$”即可获得控制几乎所有 Linux 音乐播放器的通用解决方案。在python的情况下,有:

https://github.com/hugosenari/mpris2

https://github.com/emesene/emesene-supported-plugins/blob/master/music/handler_mpris2.py

【讨论】:

以上是关于如何使用重新访问的python持续监控节奏盒以进行音轨变化的主要内容,如果未能解决你的问题,请参考以下文章

如何从 HealthKit 跑步锻炼中获取海拔增益、节奏和跑步速度?

如何从 HealthKit 跑步锻炼中获取海拔增益、节奏和跑步速度?

尽管有另一种货币的可用资金,但 PayPal 沙盒以一种货币重复付款失败

如何从应用程序本身重新启动 python 应用程序

使用python进行服务器监控

k8s+docker+Jenkins实现可持续集成容器服务监控平滑增加容器等功能