Play audio (local MP3 file) to a media_player

Does anyone know if it is possible to play a MP3 (say stored locally on HASS.IO) to a media_player?

2 Likes

I’ve been battling this as well. I recently switched from Home Assistant to Hass.IO, which wasn’t easy. I’ve lost my custom UI and audio notifications. I’ve been attempting to get Mopidy working but I can’t find much for support. I get as far as Mopidy attempting to play the MP3 but then I get connection errors.

So it is possible to do it?

I have moved from Raspian to Hassio and Yes it’s still working. I have both local file and tts generated mp3 working.

I had mine on /config/www/audio folder. Folder permission 755

Make sure you can reach it from http://yourhaip:8123/local/audio/youraudiofilename.mp3 or what ever audio file extension you use.

https://yourdomain/local/audio/youraudiofilename.mp3 - make sure you forward 443 to 8123 on your firewall…

For tts cache folder
I have it in /config/tts didn’t make a chabge on the folder permission.

I tried /tmp/tts but the tts mp3 does not get written.

4 Likes

Hi Vin,

Sorry for the late reply - thank you for the info, so it is possible, YAY! I am trying to play a file when a motion is triggered or a button pressed, but I am starting with a basics, getting the file to play.

I have a media player (Yamaha wireless speaker called media_player.kitchen in HASS.IO) and I was wondering how did you get to play an audio file to it, or even better, have some google voice/preset text “Doorbell was pressed” sent to the media_player?

Is this even possible or am I expecting way too much from HASS.IO?

EDIT: I think what I am trying to achieve can be done via this:https://home-assistant.io/components/tts/

Question is, how do I ‘trigger’ this to happen?

here is a script I use (part of it) to wake me up. the part I’m attaching says the temperature outside and the weather conditions (raining, partly cloudy etc).

it works great. you might want to set the volume to an appropriate level before sending this command…

as you can see, i have a sequence. i have a few things that it says, and i put pauses between them. it’s good to note that the pauses have to include the time it takes to say each bit.

morning_brief:
  alias: "morning brief"
  sequence:
    - service: tts.google_say
      entity_id: media_player.attic_speaker
      data_template:
        message: 'Its {{ states.sensor.time.state }} and {{ states.sensor.pws_weather.state }} '
        cache: false
    - delay:
        seconds: 12
      entity_id: media_player.attic_speaker
      data_template:
        message: ' next bit etc '
        cache: false

Next you can write an automation that runs this script when triggered ie.

- id: motion_kitchen
  alias: motion_kitchen
  trigger:
    platform: state
    entity_id: sensor.kitchen_sensor_burglar
    from: '0'
    to: '8'
  action:
    service: homeassistant.turn_on
    entity_id: script.morning_brief
1 Like

To add to the question. Couldn’t you play a LOCALLY hosted MP3 without worrying about any port forwarding etc? with tts it’s sometimes nice to have a little chime (alert) that plays before the message and identifies the type of message you are about to hear (motion, weather etc)

guys, if you want to play an mp3 on hassio using the local 3.5 jack on the rpi, just follow my instructions here:

It works pretty well.
If you want to use the Google TTS on any other media player you can have a look at my nabaztag project where I use the mp3s generated by the google tts and send them to a “virtual” media player here:

If i can help in any way just ask :slight_smile:

2 Likes

This work on HA with VLC, not sure on hass.io

- service: media_player.play_media
  data:
    entity_id: media_player.speaker
    media_content_id: /home/pi/Downloads/play.mp3
    media_content_type: 'music'
4 Likes

Grat news, will try this automation when I get home, tts can’t exactly sing, so I miss out on the music with notifications

hi,

I was able to play local mp3 files by using the complete path but tking into account that www file is the local file so the complete domain was like this:

“media_content_id”: “http://192.168.2.111:8123/local/musica/1.mp3”

Is there any way to play all the files into a folder like a sort of playlist?

Thanks

1 Like

If your media player supports media_content_type: ‘playlist’ in service media_player.play_media
just build your own playlist by adding items in VLC player on your desktop PC.
In my case, I have an instance of mpd addon running, added several files (or urls) to a new playlist on my desktop. Then saved the m3u8 file to the designated location.
Just beware that these playlist items are accessible the same way as on desktop PC (or change the m3u8 file e.g. by notepad++ to fit the location locally).
In hass.io the location is mostly in your “share” share folder (when using Samba addon).
Another approach could be, again using mpd media player, using service media_player.select_source.

greetz Bob

Hi, thanks for your help,

I’m using chromecast to strem the audio, but unfortunately It’s not working with the playlist approach. this is my json text:

{
“entity_id”: “media_player.juans_room”,
“media_content_id”: “https://127.0.0.1:8123/local/musica/1231.m3u8”,
“media_content_type”: “playlist”
}

Thanks

Unfortunately, google chromecast is a special kind of media player. IMHO it’s designed to play media mirrored by android apps and does not support playing “local” stuff out of the box. My mpd media player can access playlists located somewhere and plays items from it.
I had a chromecast myself, but it was too restricted for my needs at all.
I am not sure, but https://github.com/balloob/pychromecast/blob/master/pychromecast/controllers/media.py or https://github.com/balloob/pychromecast/tree/master/examples gives some information. But there are only google things like youtube or single mp3s as input.
The hard way to go could be via plex which can cast to chromecast as well. Plex could be automated via hass again. :roll_eyes:

1 Like

I have my music copied to my NAS in folders by album. I send all the files in the folder to the chromecast using castnow. (https://github.com/xat/castnow) Works great.

1 Like

I run my HA on an ODROID-XU4 using Ubuntu 18.04 for the operating system. There’s probably an easier way to do this, but here’s my method.

I copied my music to my NAS using the directory format /artist/album/song.flac or m4a. I mount the music directory from my NAS to the Odroid using NFS at the mount point homeassistant configuration directory/pub/Music. In my case the configuration directory is /home/homeassistant/.homeassistant/.

I made a shell script that I called makemusicyaml that will read the directory tree and make a yaml file for the input_select. Here it is:

find  /home/homeassistant/.homeassistant/pub/Music/* -type d -print | sed 's/\/home\/homeassistant\/.homeassistant\/pub\/Music\///g' | sed 's/\x27/\\\x27/g' | sort | sed 's/^/ - /' | grep "/" 

To use the yaml file redirect the output to music.yaml, like this:
./makemusicyaml > music.yaml

I created a shell script named “cast” for passing the music to castnow. I only have one Chromecast and it is always assigned the same IP address. Here’s the script:

/usr/bin/find  /home/homeassistant/.homeassistant/pub/Music/$1/*.m4a

if [ $? -eq 0 ]
then
  /usr/local/bin/castnow  /home/homeassistant/.homeassistant/pub/Music/$1/*.m4a --address 192.168.1.3 --quiet 
else

  /usr/bin/find  /home/homeassistant/.homeassistant/pub/Music/$1/*.flac
  if [ $? -eq 0 ]
  then
      /usr/local/bin/castnow  /home/homeassistant/.homeassistant/pub/Music/$1/*.flac --address 192.168.1.3 --quiet
  fi
fi

In my configuration.yaml I added

input_select:
  music:
    name: Music
    options:
      !include music.yaml
  

shell_command:
  send2cast:   bash /home/homeassistant/.homeassistant/cast {{states('input_select.music')}}
  

Here’s a screen shot with the input select pulled down.

1 Like

@Pantomax

Hello, the subject is old, but I would appreciate if you answer.
I get this error.

[s6-init] making user provided files available at /var/run/s6/etc…exited 0.
[s6-init] ensuring user provided files have correct perms…exited 0.
[fix-attrs.d] applying ownership & permissions fixes…
[fix-attrs.d] done.
[cont-init.d] executing container initialization scripts…
[cont-init.d] done.
[services.d] starting services
[services.d] done.
starting!
[Info] Read alias: http://192.168.6.200:8123/local/1.mp3
ALSA lib dlmisc.c:283:(snd_dlobj_cache_get0) Cannot open shared library /usr/lib/alsa-lib/libasound_module_pcm_pulse.so ((null): Error loading shared library /usr/lib/alsa-lib/libasound_module_pcm_pulse.so: No such file or directory)
ERROR: Failed to load plugin /usr/lib/ao/plugins-4/libalsa.so => dlopen() failed
play FAIL ao: Could not find a default ao driver
play FAIL sox: Sorry, there is no default audio device configured
[Error] Playing failed ->

Hi. I didn’t found what I was looking for in this topic, but it would be nice to give my cents to whoever gets here later.

Use this https://www.home-assistant.io/integrations/media_extractor

I could finally play audio from my HA folder and also youtube video/songs.

2 Likes

what media player are you using?

Hi everybody,
I’m new with HA and somehow struggling with the playback of a local mp3-file. I’m using the service media_player.play_media and the file is located on my HA-machine within the media-folder (subfolder audio):

service: media_player.play_media
data:
  media_content_id: 'http:/192.168.178.121:8123/media/audio/Achtung-Im-Bad-oder-WC-Fenster.mp3'
  media_content_type: music
target:
  entity_id: media_player.wohnzimmer_sonos

Unfortunately I can’t get any playback. The logfile says:

ERROR (SyncWorker_0) [homeassistant.components.sonos.media_player] Error on play_media with UPnP Error 714 received: Illegal MIME-Type from 192.168.178.54

I would be grateful for any help

1 Like