lukx / home-assistant-jukebox

Home-Assistant Radio Station and Stream Selector Card
65 stars 33 forks source link

Long buffering for specific radio stations #22

Open thehijacker opened 3 years ago

thehijacker commented 3 years ago

Hello,

For last few hours I am trying to debug long buffering on specific radio stations like this two:

https://scdn.nrjaudio.fm/adwz1/de/33047/mp3_128.mp3 http://live.1chi.si/vrabcek

It can take up to 20 seconds to start. If I press pause and play again as soon as station is selected it will start playing it instantly.

Same works with catt.

I am struggling with modifying function onStationSelect(e) to add some kind of delay or check if stream is in fact playing. If it would not then I would issue only play on it as suggested in pychromecast forums.

Can someone that knows a bit better JS and HA functions, give me/us a little help to solve this?

Thank you and best regards, Andrej

thehijacker commented 3 years ago

Best I could do without blocking entire browser.

I try to call service media_play every 0,5 seconds for 5 seconds in total after I switch to station. Now streams that would normally take 20 seconds to start, start in 1 to 4 seconds max.

    onStationSelect(e) {
        this.hass.callService('media_player', 'play_media', {
            entity_id: this._selectedSpeaker,
            media_content_id: e.currentTarget.stationUrl,
            media_content_type: 'music',
            extra: {
                metadata: {
                stream_type: "LIVE",
                metadataType: 3,
                title: e.currentTarget.stationName,
                artist: "Live Radio",
                images: [
                    { url: e.currentTarget.stationLogo }
                    ]
                }
            }
        });
        var that = this;
        var i = 0;
        for (i = 500; i <= 5000; i=i+500)
        {
            setTimeout(function() {
                that.hass.callService('media_player', 'media_play', {
                    entity_id: that._selectedSpeaker
                }); }, i);
        }
    }

I am open for improvement of this code. :)