Possible to queue multiple calls to media_player.play_media for sequential playback?

Is it possible to coordinate (queue or wait to finish) multiple calls to media_player.play_media? I think to do so would require knowing when the media player is done playing what you sent it, but when you send to multiple that may be problematic if they are not all closely in sync. Maybe knowing the time duration of the audio file being played would be sufficient if there isn’t a way to know whether what you played has finished.

I’m playing local audio notifications with a script config so that I can have a chime sound followed by the TTS message. What I have found is that the TTS plays before the chime has completed (sometimes the chime hasn’t even started). I have also found that a subsequent call to my script will end anything already playing, so if more than one thing at a time is trying to notify the last call wins and the other notifications are either partially heard or not heard at all.

My current solution is a delay in the script sequence to ensure the chime plays fully before the TTS plays, but that’s weak because different chime sounds are different lengths. It also means we can’t have the TTS generation happening in parallel with the chime sound so it’s less delayed if the TTS generation is slow.

I actually have 2 scenarios I’d like to achieve. First, if the same entity sends a subsequent notification while playing a previous notification it’s ok if it cuts off the playing notification because only the latest state matters (i.e., a door opens and then closes quickly. The other scenario is when different entities play notifications at the same time, they should all be heard one after the other without cutting each other off. In my mind I’m picturing a queue where each entity can only be in the queue once and only the most recent notification for each entity is played in turn. I’m sure anything that fancy will require a custom integration or something.

Thoughts?

I just had the same issue and I’ve solved it using triggers:

wait_for_trigger:
  - platform: state
    entity_id: media_player.living_room_speaker
    from: playing
continue_on_timeout: false

Of course this is not the best solution :frowning: but at least it might help you to solve your current problem. Please not that the downside is that the script will be on-hold while script is waiting for the trigger (the same as wait action).

1 Like

Good solution, although I don’t think it will work in my case. I’ve noticed that when I broadcast sounds to multiple devices they don’t all start playing at the same time. Whichever device I choose to wait for a trigger on would sometimes be finished before other devices are finished (or even started) and they wouldn’t sequence the sounds one after the other as intended.

I’ve been living with it behaving this way since I originated this thread because l haven’t had time to go any deeper with custom development. I keep hoping someone else has time to find a way :slight_smile:

1 Like