6

我想做与此处描述的相同的事情,但使用 shell 脚本(最好在 bash 中)而不是 python。似乎这样的事情应该可以使用dbus-monitor,但我对 dbus 不是很熟悉,我不清楚如何将解决方案中描述的概念应用于 python 问题并将它们应用于 dbus-monitor 工具。

4

1 回答 1

17

这是我能找到的最简单的方法:

#!/bin/bash

interface=org.gnome.Rhythmbox.Player
member=playingUriChanged

# listen for playingUriChanged DBus events,
# each time we enter the loop, we just got an event
# so handle the event, e.g. by printing the artist and title
# see rhythmbox-client --print-playing-format for more output options

dbus-monitor --profile "interface='$interface',member='$member'" |
while read -r line; do
    printf "Now playing: "
    rhythmbox-client --print-playing
done

它产生如下输出:

Now playing: Daft Punk - Overture
Now playing: Daft Punk - The Grid

它还会在启动时打印当前播放的歌曲。如果这不是您想要的,请查看 的内容$line并查看它是否包含NameAcquiredor playingUriChanged。如果它包含NameAcquired,请跳过它。

Python版本和这个bash版本的主要区别在于Python版本使用DBus来获取播放歌曲信息。我找不到使用 bash 的好方法,但rhythmbox-client --print-playing似乎效果很好。

于 2011-03-17T21:26:47.583 回答