Automation to play a jellyfin audio file on an attached speaker dedvice?

I have Jellyfin installed and configured, and have the Jellyfin plugin in HASIO. In Home Assistant, if I go to Media > Jellyfin, all of my media is there. I can browse down to an audio file, and, using the options at the bottom right of the screen, select a device to play it to, press the power button, and the play button, and hear the audio on that device.

I am trying to automate that in Node Red on Home Assistant without any success. I have tried grabbing the stream URL from Jellyfin itself, and the URL inside HA as I play the file as detailed above, and placed that in a “DefaultMediaReceiver” payload, and nothing seems to work. That DefaultMediaReceiver payload works fine if I specify a URL to an MP3 file that is on the internet.

I use a function node with this code:

var msg =
{
    "payload": {
        "app": "DefaultMediaReceiver",
        "type": "MEDIA",
        "media": {
            "url": "<jellyfin or HA URL to mp3 here...>",
            "contentType": "audio/mp3",
            "streamType": "BUFFERED"
        }
    }
};
return msg;

And that is sent to a castv2-sender node. Everything is OK, except the URL will never play> I also send a volume set to that device, and that works, and simply changing the URL to an online mp3 works fine.

Any help much appreciated. Happy to hear about a better way to do this in Node Red if I have taken a wrong path. I am trying to keep it in Node Red just because I have a bunch of other automation in there, but also happy to hear about other options that may have a better outcome too.

Cheers

I seem to have gotten it working. I am fairly certain my issue was that I sent the volume_set command, and the play_media command far too quickly, effectively at the same time. Putting a 1 second delay on the play has resolved the issue.

When it comes to the URL that works, here is how I got it in case it helps others:

Open Jellyfin
Browse to the particular media file you want to play
Click the veritcal …
Click “Copy Stream URL”
Use that URL as the url in the payload

It was not working, but making sure you have a decent delay between any volume set and play commands seems to have fixed it for me.