Use omxplayer or any media player on your pi to play TTS

I had trouble finding a suitable media player for my pi through the audio jack to an amp. I found omxplayer to work but it wasn’t a HA media player yet. I noticed that my TTS mp3’s were stored in the TTS directory so I setup a python script to copy the TTS cached mp3 and then delete the original. In HA, when I use the TTS service, I then call the python script to copy the cache mp3 and then use a switch to play on omxplayer. It work great so far.

The copy python script

import os
import shutil
import glob
newest = max(glob.iglob('/home/homeassistant/.homeassistant/tts/*.[Mm][Pp]3'), key=os.path.getctime)
shutil.copy(newest, '/home/pi/mp3/play.mp3')
os.remove(newest)

The switch to play on a media player

  play:
    command_on: 'omxplayer /home/pi/mp3/play.mp3'
    command_off: 'omxplayer /home/pi/mp3/play.mp3'
1 Like

Nice script to find latest files created in a folder. Thanks. Will use it for other stuff :slight_smile: