Offer option to loop/repeat an audio file on the media_player.play_media service

I will try to be short and sweet, as I know the developers of this amazing project have a lot to keep up with.

I have several Google Home speakers dotted throughout the house. I created scripts which are assigned to buttons on Lovelace. When I press the button, whatever speaker it’s assigned to will play a locally hosted audio file with rain sounds. So for example if I press the “bedroom” button, it plays rain sounds on both Google Home Mini speakers in my bedroom.

But these audio files only play for an hour. Yes, I have tried joining them in Audacity but they become too large and therefore do not load well. Further to that, I believe having to do so is a waste of resources on storage, a waste of time, and inefficient since programming a looping function would achieve the same result but quicker and more efficiently (can use smaller files that just loop).

So my request is, can we have a “looping/repeat one” implemented on the media_player.play_media service, or have a brand new media_player.loop_media service created to cater for those of us who need it to play ambient sounds. This would be extremely useful not just to me but others as well, I know there are other people who would make use of this because a quick search for homeassistant media_player loop audio yields several results, and who knows how many more search results would come up by typing the query in different ways since you could describe it differently…

Thank you very much in advance!

I have a file with some snippets of code I’ve collected over the years from this forum. Below is a way of creating looped audio. I know it isn’t the service you are requesting but it should solve your issue in the interim. It’s mostly copy / pasted and from a long time ago so might need a few checks, I haven’t used it myself

script: 
  - alias: Fire Alarm
      sequence:
        - service: media_player.volume_set
          data:
            entity_id: media_player.kitchen_speaker, media_player.googlehome1023
            volume_level: '0.90'
        - service: tts.google_say
          data:
            entity_id: media_player.kitchen_speaker, media_player.googlehome1023
            message: 'Fire Alarm, Evacuate'
        - delay:
            seconds: 2
        - service: media_player.play_media
          data:
            entity_id: media_player.kitchen_speaker, media_player.googlehome1023
            media_content_id: https://xxxxxxxxxxxx/local/submarine_diving_alarm.mp3
            media_content_type: 'audio/mp3'
        - delay:
            seconds: 1   
        - service: script.turn_on
          data:
            entity_id: script.fire_alarm_loop
      
    
  - alias: Fire Alarm loop
      sequence:
        - delay:
            # time for light off
            seconds: 2
        - service: script.turn_on
          data:
            entity_id: script.fire_alarm
3 Likes

Thanks, I have found bits and pieces of code around the forum (now yours is another one to add to the list). As you can imagine, for a novice it would be a nightmare figuring out what to do… so I thought suggesting this feature would help more than just myself (as well as making life easier). Hoping someone can implement it, if I knew how to program I would be doing it right away! Stay safe.

Bump

I can’t believe I’m the only person who finds this a valuable addition crying

Bump - please vote? Thanks!

Bump please vote y’all :heart:

I’d like to know this too. There is media_player.repeat_set which from the docs looks like should be able to set to all or one to make a player repeat but this has not worked for me (when calling this on a playing media_player)

1 Like

I also found media_player.repeat_set but it doesn’t seem to work (for Google cast-based devices). I wonder if it may have been added for Sonos only? @amelchio (who implemented it) will know more.

Here’s my config, which plays the file once but fails to loop it:

- id: '1612356402854'
  alias: Start rain sounds in nursery
  description: ''
  trigger:
  - platform: state
    entity_id: input_boolean.nursery_start_sleep
  condition: []
  action:
  - service: media_player.play_media
    data:
      media_content_type: music
      media_content_id: https://www.gstatic.com/voice_delight/sounds/long/rain.mp3
      extra:
        title: Rain Sounds
    entity_id: media_player.study_bookshelf
  - service: media_player.repeat_set
    data:
      repeat: all
    entity_id: media_player.study_bookshelf
  mode: single

The little manual looping hack is a neat workaround but it would be much tidier if we could do it natively - the Cast QueueData class
seems to support this, but I don’t think that’s what Hass uses today so it’s probably a big job.

At the very least, we should update the docs for repeat_set to say “Only works for devices x, y and z” if that’s the case - many other methods on the page include that disclaimer, so its absence on repeat_set made me think it was universal. Or perhaps it is and I’m calling it wrong :slight_smile:

Edit to add: tried calling repeat before cast, in case you’re meant to do it in the other order. No dice.

You are right that the support must be added separately for each media player. It seems that Sonos, Spotify and MPD media players currently support it.

It might be a better idea to remove the other disclaimers since nobody remembers to update them when support is added.

1 Like

Thanks! On closer inspection it looks like pychromecast doesn’t support repeat_mode, so I might try a feature request over there and come back if that makes any progress.

I will be nice to have custom buttons at media player (call service).

In the same boat. I have a lullaby song that plays for my kids for nap/bed time, and I’d love to be able to easily specify repeat to easily keep it playing through the night.

If you use YouTube Music you could use the custom component to play the track on repeat

It might not be the best solution, but I created this script:

alias: Rain Sounds
sequence:
  - service: media_player.volume_set
    data:
      volume_level: 0.3
    target:
      device_id: b6e85f0827e4471c94bd298390d6c606
  - repeat:
      until:
        - condition: template
          value_template: >-
            {{ state_attr('media_player.nest_mini', 'media_title') != 'Rain
            sounds' or states('media_player.nest_mini') != 'idle' }}
      sequence:
        - service: media_player.play_media
          data:
            media_content_type: music
            extra:
              title: Rain sounds
              thumb: >-
                http://www.gstatic.com/voice_delight/images/relaxation_sounds/icons/rain.png
            media_content_id: 'https://storage.googleapis.com/relaxation-sounds/rain_3600.mp3'
          target:
            device_id: b6e85f0827e4471c94bd298390d6c606
        - delay:
            hours: 0
            minutes: 0
            seconds: 10
            milliseconds: 0
        - wait_for_trigger:
            - platform: template
              value_template: >-

                {{ state_attr('media_player.nest_mini', 'media_title') == 'Rain
                sounds' and is_state('media_player.nest_mini', 'idle') }}
          continue_on_timeout: false
          timeout: '01:01:01'
mode: restart
icon: 'mdi:weather-pouring'

Here’s the result:

image

In short, until I tell it to stop or something else plays it’ll be repeating the mp3 I’ve chosen. Once HA gets support for script blueprints I’ll create and share it.

4 Likes

Following this… would like to be able to cast an mp4 (FINALLY figured out how to do that, the secret is in the www folder) then loop/repeat that mp4 on my Chromecast devices. No dice adding the media_player.repeat_set to one or all in my automation that fires the mp4 on multiple Chromecasts.

1 Like

+1
Playing back a rignbell audio sound (wav file that only lasts for 2-3 seconds), having this option would avoid having to repeat the service call 2-3 times.

So this hasn’t been resolved? Can we loop audio with Chromecasts yet?

1 Like

Hello!

same here, when you play a song from your local media, i have a folder “music” for example, that plenty of folders of “artists”, and then inside “albums”, and then inside “songs”…you can only play one file and after music stop…

Like on all players, it will be great to play a file and then the next play automatically, and the next, and the next…should be a great feature…like “continuously” playing song…

Hope devellopers will read this post… :wink:

really strange that only a few of us expect this feature :thinking:

1 Like

I asked about this in the 2022.3.x thread. No one replied. Not sure why such functionality is missing because the media browser is almost useless without it.

2 Likes

hello @sparkydave

you are totaly right…useless without this functionality…

Dont understand why we are only 2 of us that expect this basic feature…???

have a nice day