如何连续轮询当前播放歌曲的 mpd 并将结果写入文件?

How to continuously poll mpd for currently played song and write result to a file?

我唯一需要从 mpd 中提取的是当前播放的 song/track。我必须确保这在输出文件中始终是最新的。

如果安装 mpc,则可以执行以下操作:

mpc idle player # block until the player changes songs
mpc current     # outputs "Artist Name - Song Name" onto stdout

循环执行这些,并将current的结果输出到文件中,你就完成了!

#!/bin/sh
while true
do
  mpc current > current_song.txt
  mpc idle player
done

您可以 idle 执行的操作的完整列表在 MPD 命令参考中:
http://www.musicpd.org/doc/protocol/command_reference.html#status_commands