Page 1 of 1

Can mpd execute a script when play begins and ends?

Posted: July 24th, 2020, 9:07 am
by delooper
I'm building a little GPIO unit that will be able to power up (and down) my amplifiers via USB.

In an ideal world, I'd have mpd call a script to trigger the GPIO unit, when a song (or playlist) begins, and call a script to power-down the amplifiers when the playlist ends.

Is there a native way to do this with mpd, i.e. can we configure mpd to call a script just before a playlist starts, and similarly call another script after play has ended?

Failing that, is there a smarter way to do this, other than having a script that monitors the output of "mpc current"? For startup, perhaps observing the timestamp on mpd.log would be the least-effort approach.

Any advice would be welcome.

Re: Can mpd execute a script when play begins and ends?

Posted: July 24th, 2020, 10:59 am
by max
No, but you can write a simple client which does this. Not mpc, no manual polling - your client should listen to MPD's "idle" events and each time one is generated, check the playback status. That way, your client doesn't need to wake up unless something really happens.

Re: Can mpd execute a script when play begins and ends?

Posted: July 24th, 2020, 6:14 pm
by delooper
Thanks. I did a little digging around. Will try implementing it via python-mpd2 at first, to see how it goes.

Re: Can mpd execute a script when play begins and ends?

Posted: July 24th, 2020, 6:45 pm
by max
That's a good idea. If you want it even lighter than that, write something in C with libmpdclient.

Re: Can mpd execute a script when play begins and ends?

Posted: July 24th, 2020, 6:46 pm
by max
This might be a different approach: https://alip.github.io/mpdcron/hooks/

Re: Can mpd execute a script when play begins and ends?

Posted: July 24th, 2020, 11:51 pm
by delooper
Thanks, mpdcron looks like a smart generalization of what I was thinking about. I'll try that first.