fireindark707

fireindark707

Overcoming Audio Playback Issues on mobile(iOS) with Phoenix LiveView - Seeking Better Solutions

Hello, everyone!

I recently encountered an issue with audio playback in a Phoenix LiveView application that I thought I’d share. I believe it could be a common issue many of us may face, particularly when dealing with user interactions on different browsers and devices, specifically iOS mobile browsers in this case.

The Issue:

My application has an audio playback feature. I had implemented this feature using a custom Phoenix LiveView event, playAudio, handled in a JS Hook. The relevant JavaScript code looked something like this:

javascriptCopy code

this.handleEvent("playAudio", ({ audio_id, audio_url }) => {
    let audio = document.getElementById(`audio-${audio_id}`);
    audio.src = audio_url;
    audio.addEventListener("loadeddata", () => {
        audio.play();
    });
});

also tried just put audio.play() in handleEvent without “loadeddata” event, but still failed.

This code worked perfectly on desktop browsers but failed on all iOS mobile browsers. I hypothesized that this was due to audio.play() not being able to be triggered inside the custom LiveView event playAudio.

The Solution:

I found out that on mobile browsers (especially iOS), audio playback might need explicit user interaction. I had to modify the code to directly play the audio on user interaction, not waiting for the loadeddata event. My working solution was to use the traditional onclick handler directly on the HTML element to trigger audio playback:

htmlCopy code

<a href="#" class="text-blue-600 hover:mb-2 hover:text-blue-800" onclick={"document.getElementById('audio-#{word.id}').play()"} phx-click="playAudio" phx-value-audio-id={word.id} phx-value-audio-text={word.word}>
    <!-- SVG for the play button -->
</a>
<audio controls id={"audio-#{word.id}"}>
    <p>Your browser does not support HTML audio</p>
</audio>

By doing this, clicking the play button triggers the audio to play directly, bypassing the LiveView event. This works across all browsers, including iOS mobile browsers.

Seeking Better Solutions:

While this solution works, it might not be the most elegant or efficient one. I am very much interested to hear if anyone else has encountered similar issues and how you’ve dealt with them. Is there a better approach to handle audio playback in Phoenix LiveView applications, particularly compatible across all devices and browsers?

Thank you for your time!

Marked As Solved

al2o3cr

al2o3cr

For what it’s worth, this isn’t a LiveView-specific problem, it’s a fundamental limitation of how autoplay is handled in the browser:

Where Next?

Popular in Questions Top

New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement