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 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: https://github.com/liamw9534/mopidy/commits/liamw_mopidy_dev; 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: https://github.com/liamw9534/mopidy/commit/278eb62d50b68cd0376041d726e9b6d7087f6755#diff-f3138e0ab17ebf1710d15e3e68a6ee65
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 https://github.com/liamw9534/mopidy-btmanager/blob/master/mopidy_btmanager. If you look at actor.py 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.