Next.js Discord

Discord Forum

audio autoplay not working on andriod

Unanswered
American Curl posted this in #help-forum
Open in Discord
American CurlOP
Hi everyone, πŸ‘‹ so I was working on my outdated profito site ( https://personal-web-black-eight.vercel.app/ ), and I wanted to add some background music, and everything is working fine on pc, and the audio is perfectly playing, but on Andriod, it doesn't.

I am using js for audio
Const audio = new Audio(src)
audio.play()

I tried using audio tag in HTML but didn't work

I tried possibly everything I could, but it didn't work on Android. I also read about Chrome's autoplay policy on Andriod, but still, even after the interaction, audio isn't playing on Andriod, so idk what the problem is here.

45 Replies

@California pilchard It normally won't play unless the user interacted first, autoplay gets blocked (atleast by chrome). you could try listening for events like `click` and then try playing the audio, or maybe wait a second this explains it a little bit better https://stackoverflow.com/a/62720714 and this seems to be a solution https://stackoverflow.com/a/73785358
American CurlOP
This is exactly what I tried; it doesn't work at all, even after I tried interacting with the website (and tested on 3 different phones); it doesn't play (I tested across multiple browsers. I tried turning on autoplay for my site but still audio don't play on andriod
California pilchard
have you tried adding a button which starts playing on click ?
American CurlOP
Yes button works
But autoplay doesn't
California pilchard
mhm πŸ€”
i'll look around and see if i find something
are you playing it via js, or an audio tag ?
American CurlOP
Tried from both
I tried every possible way
Btw I just checked the stack overflow link you gave, and (the op) has said he achieved autoplay on multiple websites through a snippet, but I tested his website, and bruh, it is not working. Seems a problem with broswers
California pilchard
oh 😲
California pilchard
some say the attribute allow="autoplay muted" should work.

i found this comment, which basically says you can start autoplaying once the user clicks on anything
https://stackoverflow.com/questions/34837930/audio-tag-autoplay-not-working-in-mobile/63575250#63575250

this one from the same post uses an AudioContext, but he also says that it could be patched at anytime, so this would only be a temporary solution
https://stackoverflow.com/a/46485784

i've found this snippet in the [mdn web docs](https://developer.mozilla.org/en-US/docs/Web/Media/Autoplay_guide) this page seems to have a couple of "workarounds" listed
let playAttempt = setInterval(() => {
  videoElem
    .play()
    .then(() => {
      clearInterval(playAttempt);
    })
    .catch((error) => {
      console.log("Unable to play the video, User has not interacted yet.");
    });
}, 3000);


i've also got this snippet from GPT, seems to be a simpler version of the one AudioContext further up
let audioContext = new (window.AudioContext || window.webkitAudioContext)();
let audioElement = document.querySelector('audio');
let track = audioContext.createMediaElementSource(audioElement);

document.addEventListener('click', () => {
    audioContext.resume().then(() => {
        audioElement.play();
        console.log('Audio playback started');
    });
});
@American Curl how about u first fix your websites autoplay?
u are playing this without checking if the user has interacted
hence i get this error
once u done that ill debug your issue why it doesnt play on phones
cause currently it aint even work on desktop -> u have the reason above
@California pilchard <@759064479521701888>
what is he testing it on when his website isnt updated tho? lets wait for the author to reply cause i think he probably didnt correctly update his website
there is no other reason why it wouldnt work
its not a browser issue cause Audio() is widely supported
California pilchard
his answer to my suggesting implies that he already tried it with an event, such as click, that's why've pinged you, it wouldn't make much sense suggesting the same thing twice
@California pilchard his answer to my suggesting implies that he already tried it with an event, such as `click`, that's why've pinged you, it wouldn't make much sense suggesting the same thing twice
Alr listen, as i said im not chatgpt that just suggests random things that doesn't help anyone. He is clearly a beginner and without correctly debugging the issue he wont learn anything out of it. If his website doesn't even have the correct solution suggested by you we can assume he didn't correctly tried the solutions out hence making this conversation unnecessary
"Autoplay" as he saying will not work with volume enabled
the same also with video
@gin Alr listen, as i said im not chatgpt that just suggests random things that doesn't help anyone. He is clearly a beginner and without correctly debugging the issue he wont learn anything out of it. If his website doesn't even have the correct solution suggested by **you** we can assume he didn't correctly tried the solutions out hence making this conversation unnecessary
California pilchard
I already know that autoplay doesn't work without any interaction first, i tried suggesting multiple things in hope of him finding one solution, i don't know how, or if he really tested anything, i'll just have to trust his answers, so do you. You've suggested the same thing as me and also said "how about u first fix your websites autoplay?", which doesn't make sense, because that is what he's been trying to do. it's normal that you'll get an error for autoplay, since it's not allowed without any interactions. The only useful thing you've said is "u are playing this without checking if the user has interacted" and i already said that at the beginning.
California pilchard
🫑
u can continue here ill tap out
@gin u can continue here ill tap out
California pilchard
I still want to apologise for unecessarily discussing and "beefing" with you. Thank you anyways for trying to help and for giving your insights
@California pilchard I still want to apologise for unecessarily discussing and "beefing" with you. Thank you anyways for trying to help and for giving your insights
yeah all good this situations always frustrating when the author is offline and we both saying the same thing but in different terms yk
lmao
California pilchard
yh
American CurlOP
Sorry guys for being offline,
Let me clarify things
@gin Click to see attachment
American CurlOP
Yeah, this is an updated version because I removed the play based on click because it was not working and I did debug a lot, I almost had deployed 10 times with different approaches
And now I have just embedded a simple click button to play background audio cause I don't wanna mess with this anymore
@gin cause currently it aint even work on desktop -> u have the reason above
American CurlOP
Yes, it doesn't (just on Edge desktop and Firefox) and on Chrome if you toggle autoplay.
The main thing here is if it's working on a desktop (and if enable autoplay too on Android) it should work even in this case but it doesn't
Thanks a lot, guys for replying
i would try to reproduce the error and test it
@California pilchard is this your "final" solution ?
American CurlOP
Yeah, for now, though I would later try doing the audio part with howler