Can mpd execute a script when play begins and ends?

General Discussion about MPD – anything that doesn't fit in the other MPD forums.
Post Reply
delooper
Posts: 3
Joined: July 24th, 2020, 8:49 am

Can mpd execute a script when play begins and ends?

Post 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.
max
Forum team
Posts: 1139
Joined: January 15th, 2013, 3:43 pm

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

Post 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.
delooper
Posts: 3
Joined: July 24th, 2020, 8:49 am

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

Post by delooper »

Thanks. I did a little digging around. Will try implementing it via python-mpd2 at first, to see how it goes.
max
Forum team
Posts: 1139
Joined: January 15th, 2013, 3:43 pm

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

Post by max »

That's a good idea. If you want it even lighter than that, write something in C with libmpdclient.
max
Forum team
Posts: 1139
Joined: January 15th, 2013, 3:43 pm

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

Post by max »

This might be a different approach: https://alip.github.io/mpdcron/hooks/
delooper
Posts: 3
Joined: July 24th, 2020, 8:49 am

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

Post by delooper »

Thanks, mpdcron looks like a smart generalization of what I was thinking about. I'll try that first.
Post Reply