Simple event listener

I’m using mpd on a Raspberry Pi as a living room jukebox. It’s connected to a speaker by a headphone cable, but the speaker is also capable of Bluetooth. I would like to listen to events that happen to mpd and trigger actions - such as connecting to the speaker with Bluetooth when somebody presses play. This would allow me to site the Pi anywhere in the room, which is useful (as well as being quite cool :smile: I haven’t been able to find good documentation on doing this with mpd, but mopidy looks more promising because of the events support in the Python API.

Does anybody have a minimal example of a mopidy event listener in Python I can use as a starting point?

Have a look in my github account. You will find Mopidy extensions under development for managing Bluetooth devices which also export events over HTTP web socket.

There is also the option of automatically connecting to a speaker once it is discovered.

Alternatively, without using any extensions, just use GStreamer sbcenc and a2dpsink plugins directly in your audio output configuration setting - this will thenconnect to your BT speaker whenever you play.

Hi Liam, can you please post an example of the code Peter was after? Peter did you have any luck on this?

I’m interested in achieving this also to build radio functionality into the Spotmop frontend.

The extensions were implemented on a mopidy fork I made 2 years ago:; this includes other changes as well to the audio backend for allowing dynamic sink addition/removal.

I can’t really remember much about it now but the main changes are in this commit:

The basic idea is that any extension that sub-classes service.Service is registered as a “service” and is able to propagate events and have its remote procedures called via websocket interface using jsonrpc.

An example extension that uses this is If you look at you can see various cases of events being sent using the service.ServiceListener.send() method. Otherwise, all the methods (not decorated with @private_method) are callable over JSON RPC.

I ended up using Bluetooth/ALSA integration. I don’t remember the exact details, but something like this:

This is actually independent of the player software.

I also moved from mpd to Squeezebox, largely because of the synchronisation/multi-device support but the apps are nice too - quite a few streaming services are available out of the box.