Hi all!
I’m trying to do something very simple but I’ve been stuck on it for a while. I have these audio tags and I want to hardcode the volume level to 0.1. The code below is not working:
<div class="flex flex-col gap-2 w-fit ml-5">
<%= for sound <- @currently_playing do %>
<audio id={sound.name} src={sound.url} autoplay loop volume="0.1"></audio>
<%= end %>
</div>
Oddly, there is no volume attribute for <audio> and <video> tags, you can only set it with JS: document.querySelector("audio").volume = 0.1. I have no idea why this is the case but it is what it is ¯\(ツ)/¯
Otherwise, in more vanilla contexts (ie, without LiveView) there are a few ways to ensure the DOM is loaded before JS runs. Phoenix uses the defer attribute for the script tag: <script defer src="..."></script>. You should really be using JS hooks, though! Not only are they reusable, they play nicely with LV.
I do it now for the audio with phoenix live view and for work the buttons(Play, Pause, Vol +, Vol -) and the Presence of the Phoenix hide the built-in browser UI (by removing the controls attribute from audio) and instead build your own interface and control the playback using Javascript.
You get the element by the ID in this case is #player.