如何使用 bash 持续监控节奏盒的曲目变化

发布于 2024-10-23 18:12:39 字数 282 浏览 8 评论 0原文

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

I'd like to do the same thing as is described here, but using shell scripting (preferably in bash) instead of python. It seems like such a thing should be possible using dbus-monitor, but I'm not very familiar with dbus, and it's not clear to me how to take the concepts described in the solution to the python question and apply them to the dbus-monitor tool.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久夏青 2024-10-30 18:12:39

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

#!/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 的内容,看看它是否包含 NameAcquiredplayingUriChanged。如果它包含 NameAcquired,请跳过它。

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

Here's the simplest way I could find:

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

It produces output like this:

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

It also prints the currently playing song when started up. If that is not what you want, look at the contents of $line and see if it contains NameAcquired or playingUriChanged. If it contains NameAcquired, skip it.

The main difference between the Python version and this bash version is that the Python version uses DBus to get the playing song information. I couldn't find a good way to do that using bash, but rhythmbox-client --print-playing seems to work well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文