How to play mp3 from media browser folder?

Did you whitelist the media folder in config like this?

homeassistant:
  allowlist_external_dirs:
    - /media

This didn’t solve the issue. I found out that chromecast can’t play media directly stored in Home Assistant.

So what I did is I moved the file to my Nextcloud server, shared a link of that audio and used that link to play the audio to google speaker.

I’m not sure this is right… I have the same issue. I can’t figure out how to get a mp3-file to play. But I know if you do it manually you CAN play it. So no reason why it should not work automatically? Right?

I had the same problem too. But the easiest fix I did was to move the songs to my Nextcloud. I also couldn’t figure why I can’t play the songs in the media folder.

Try storing and playin the mp3 files from an online storage like Dropbox. Make sure the files are shared and accessible online.

That’s really disappointing. I’m running into this same issue. It looks like the /media/ folder gives a 401:Unauthorized error if you try to access its contents directly which, presumably, would be necessary for most devices.

When you access the same contents via the “Media Browser” tab they append an ID that allows access. You’d think they’d build a method to invoke a piece of content via a content_id that would call the public URL with an authorization code. Otherwise why have a media folder at all if it only allows for web playback?

Okay , I found a work around.

  1. Open Home Assistant Media browser.
  2. Choose Player → Select Web Browser.
  3. Opened Dev Console on the browser, go to Network tab.
  4. Select the media you want to play. Once it starts playing Check the Network tab. Copy the request URL and use it as the “media_content_id”

This is how mine looks.

service: media_player.play_media
data:
  media_content_id: http://10.10.10.190:8123/media/local/private/WhiteNoise.mp4?authSig=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJjNTMxNDE2Y2IxYmE0M2ViYmE4N2YxNDAxZmY0ZTY0MCIsInBhdGgiOiIvbWVkaWEvbG9jYWwvcHJpdmF0ZS9XaGl0ZU5vaXNlLm1wNCIsImlhdCI6MTYyODYzMTEyNSwiZXhwIjoxNjI4NzE3NTI1fQ5SYn1_FC4BZzZtvyWPlG0Kx_QxNe4
  media_content_type: music
  entity_id: media_player.googlehome7641

EDIT ------ The copied Authkey expires.
I found a NodeRed solution to request authkey everytime.

[{"id":"970707c9e290e4c5","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"8a9451a1489823ee","type":"switch","z":"970707c9e290e4c5","name":"Short Press","property":"payload.event.args[0]","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"str"},{"t":"eq","v":"1","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":170,"y":100,"wires":[["f95cdc2547c3b160"],[]]},{"id":"f95cdc2547c3b160","type":"ha-api","z":"970707c9e290e4c5","name":"Get AuthSign","server":"76b07c6c.a5b7a4","debugenabled":false,"protocol":"websocket","method":"get","path":"","data":"{   \"type\": \"auth/sign_path\",   \"path\": \"/media/local/private/WhiteNoise.mp4\",   \"expires\": 300 }","dataType":"jsonata","location":"payload","locationType":"msg","responseType":"json","x":390,"y":100,"wires":[["d9f6be0e73519a55"]]},{"id":"d9f6be0e73519a55","type":"function","z":"970707c9e290e4c5","name":"Make Payload","func":"varPath = \"http://192.168.1.190:8123\" + msg.payload.path;\nvarGoogleHome = \"music\";\nmsg.payload.data = {\"media_content_id\" : varPath, \"media_content_type\" : varGoogleHome, }\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":100,"wires":[["c407630b2c25e128"]]},{"id":"c407630b2c25e128","type":"api-call-service","z":"970707c9e290e4c5","name":"Play White Noise","server":"76b07c6c.a5b7a4","version":1,"debugenabled":false,"service_domain":"media_player","service":"play_media","entityId":"media_player.googlehome7491","data":"payload.data","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":750,"y":100,"wires":[[]]},{"id":"76b07c6c.a5b7a4","type":"server","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

1 Like

I’ll try this. Thanks!

Worked perfectly. Thanks!

Hello!
antimage’s response is perfect! I just wanted to add:

Google devices like the Nest Hub or Nest Mini can’t resolve local hostnames. It took a while for me to figure that out…

So make sure to use the IP address, like he suggested:
http://10.10.10.190:8123/media/local/private/WhiteNoise.mp4?authSig=(...)

and NOT the local hostname
http://homeassistant.local:8123/(...)

you can also generate 10 years valid token in your /profile (bottom of the page), should be enough for some time :wink:

I tried with the token, It didnt work. I dont think its the same as Authkey. I get this error with the 10 year token.
image

After have tried lots of configuration, nothing has worked. Play web hosted mp3 files is fine but for local files it’s not working.

Workaround : simply set up an apache2 on your hass device (pi, vm, desktop…) or on another device and put your mp3 in /var/www/html/ after a chmod 777 on the folder. This is nasty but no more issue :slight_smile:

I tried this, but it gives me a 401 unauthorized error. How do you configure this?

How do you even use a long lived token? There’s not much documentation how to use it? I used it and replaced everything after authSig= and it gave me a 401 error.

Hi Chris,
Long lived tokens do not work for this case. You need a signed path to access the content, AFAIK you cant create a signed path which never expires. So I created a node red flow which requests a signed path every time I need to access the media. It works perfectly.

[{"id":"970707c9e290e4c5","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"8a9451a1489823ee","type":"switch","z":"970707c9e290e4c5","name":"Short Press","property":"payload.event.args[0]","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"str"},{"t":"eq","v":"1","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":170,"y":100,"wires":[["f95cdc2547c3b160"],[]]},{"id":"f95cdc2547c3b160","type":"ha-api","z":"970707c9e290e4c5","name":"Get AuthSign","server":"76b07c6c.a5b7a4","debugenabled":false,"protocol":"websocket","method":"get","path":"","data":"{   \"type\": \"auth/sign_path\",   \"path\": \"/media/local/private/WhiteNoise.mp4\",   \"expires\": 300 }","dataType":"jsonata","location":"payload","locationType":"msg","responseType":"json","x":390,"y":100,"wires":[["d9f6be0e73519a55"]]},{"id":"d9f6be0e73519a55","type":"function","z":"970707c9e290e4c5","name":"Make Payload","func":"varPath = \"http://192.168.1.190:8123\" + msg.payload.path;\nvarGoogleHome = \"music\";\nmsg.payload.data = {\"media_content_id\" : varPath, \"media_content_type\" : varGoogleHome, }\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":100,"wires":[["c407630b2c25e128"]]},{"id":"c407630b2c25e128","type":"api-call-service","z":"970707c9e290e4c5","name":"Play White Noise","server":"76b07c6c.a5b7a4","version":1,"debugenabled":false,"service_domain":"media_player","service":"play_media","entityId":"media_player.googlehome7491","data":"payload.data","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":750,"y":100,"wires":[[]]},{"id":"76b07c6c.a5b7a4","type":"server","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

1 Like

I’d like to try this, but I have no idea how to use node red. I installed it, but I don’t know where to type the the script.

It looks like they’re using the websockets Authentication API | Home Assistant Developer Docs

They get a token then use that for the authSig query parameter.

Unfortunately, I don’t know how to directly/easily make a websocket call from within an automation and it seems the auth api is only available via websockets and not HTTP. Node-red on the other hand does seem to have native support for websockets.

Yes, I am using the Signed Paths - Authentication API | Home Assistant Developer Docs
I am not sure how to do this via home assistant automations. Node red makes it really easy.

It is explained in the docs: Media Source - Home Assistant

service: media_player.play_media
target:
  entity_id: media_player.living_room_tv
data:
  media_content_type: "video/mp4"
  media_content_id: "media-source://media_source/local/videos/favourites/Epic Sax Guy 10 Hours.mp4"

For me it worked like this:

service: media_player.play_media
target:
      entity_id: media_player.google_nest_mini
 data:
    media_content_id: "media-source://media_source/local/file.mp3"
    media_content_type: music
6 Likes

This is the script I have and it works perfectly. I didn’t need to whitelist anything.

  - service: media_player.play_media
    target:
      entity_id: media_player.speaker
    data:
      media_content_id: '/media/local/file.mp3'
      media_content_type: audio/mp3

I had “music” as the media_content_type at first, but it wasn’t working with my setup. I found out using audio/mp3 has no issue.

7 Likes