如何使用bash连续监控rhythmbox的音轨变化

jbe*_*rd4 6 bash dbus rhythmbox

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

Mik*_*kel 15

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

#!/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
Run Code Online (Sandbox Code Playgroud)

它产生如下输出:

Now playing: Daft Punk - Overture
Now playing: Daft Punk - The Grid
Run Code Online (Sandbox Code Playgroud)

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

Python版本和这个bash版本之间的主要区别在于Python版本使用DBus来获取播放歌曲信息.我找不到使用bash做到这一点的好方法,但rhythmbox-client --print-playing似乎运作良好.