5

I want to know how to receive the actual status of the mpd player with a linux bash script. I know how to start and stop the player...

#!/bin/bash
mpc play
mpc volume +1
mpc stop

...but i need to know if mpd is playing a song or not. Also the current volume setting is interesting.

I tried to receive it with mpcstatus=cat /var/tmp/mpd_status or actvol=cat /var/tmp/mpd_volume but the files do not exist. I'm working with Volumio/Debian on a RaspberryPi.

WBK
  • 81
  • 1
  • 5
  • `mpc status` does not work, it gives just `volume:100% repeat: off random: off single: off consume: off` ,no matter whether it's playing or not. Only the volume can be extracted from that string. – WBK May 19 '15 at 14:53
  • `mpc status` giving those results in all cases sounds like a bug. Have you looked at the `mpc`'s developer site and or bug list. Maybe it fixed in a beta? Maybe they would take a bug report. Good luck! – shellter May 19 '15 at 16:02
  • I have to apologise! `mpc status` is displaying the following: `Atomic Rooster - Time Take My Life [playing] #1/18 0:00/5:59 (0%) volume: 75% repeat: off random: off single: off consume: off`. Seems to bee that I just checked the output when the player was stopped! Now it should be easy to filter the needed values with the grep-command. – WBK May 19 '15 at 17:34
  • Not really a complete answer to this question, but mpc does have a `current` command. As far as I can tell, if `mpc current` produces no output, then mpd is not playing. This helps with the "if mpd is playing a song or not" part. It does nothing to help with "current volume setting". – Dale Mar 09 '20 at 23:56

1 Answers1

3

I've got it!

Play:

if mpc status | grep playing >/dev/nul      # If mpd is playing
then
 command... 
fi

Volume:

ACTVOL=`mpc status | sed -n '/volume/p' | cut -c8-10 | sed 's/^[ \t]*//'`
WBK
  • 81
  • 1
  • 5
  • 2
    This won't work if any of those keywords is located in currently playing song title. You should parse only the line containing your information: `if mpc status | awk 'NR==2' | grep playing; then [...]` – pedroapero Jul 17 '16 at 12:47