Simple MQTT media player#

About#

The idea is to implement a simple MQTT media player on Linux, which can be used to play TTS messages from Home Assistant. Home Assistant renders the TTS stream as an MP3 and makes it available on its HTTP server, so a corresponding command using mplayer to play the audio resource would look like this.

mplayer -volume 80 http://home.assistant.address:8123/api/tts_proxy/6a0efdf280bf8c79a.mp3

Configuration#

The solution for this will be implemented using mqttwarn’s execute service plugin, which can be used to invoke programs, and interpolate MQTT payload data.

# Simple MQTT media player using mqttwarn and mplayer.
# https://mqttwarn.readthedocs.io/en/latest/examples/mediaplayer/readme.html

[defaults]
launch = execute

[config:execute]
targets = {
    'mediaplayer-play': [ 'mplayer', '-volume', '80', '[TEXT]' ],
  }

[mediaplayer/play]
targets = execute:mediaplayer-play

Usage#

Using three terminal sessions, you can exercise the example interactively. First, let’s start the Mosquitto MQTT broker.

docker run --name=mosquitto -it --rm --publish=1883:1883 eclipse-mosquitto:2.0 mosquitto -c /mosquitto-no-auth.conf

Let’s acquire the mqttwarn-mplayer.ini configuration file, and start mqttwarn.

wget https://github.com/mqtt-tools/mqttwarn/raw/main/examples/mediaplayer/mqttwarn-mplayer.ini
mqttwarn --config-file=mqttwarn-mplayer.ini

Now, when publishing the URL to the audio resource on the designated MQTT topic, mqttwarn will invoke the mplayer command as instructed.

echo 'http://home.assistant.address:8123/api/tts_proxy/6a0efdf280bf8c79a.mp3' | \
  mosquitto_pub -t 'mediaplayer/play' -l