Audio Tag Autoplay Not working in mobile
Asked Answered
H

3

23

i am using this code and when i see the controls i see the autoplay is not working.

<audio autoplay="true" src="music/lathe_di_chadar.mp3" type="audio/mp3" loop></audio>

and its not working in the mobile devices and very well working in website. Can anyone tell me the problem in this?.

Thanks and well Appreciated

Hexylresorcinol answered 17/1, 2016 at 11:44 Comment(1)
opera mini in ios supports autoplay by default, while chrome, firefox and safari does not and have not offering options to turn on.Bertina
P
22

Now, it's2020

Note that (for the below reason?) Chrome has changed their autoplay policy (see https://developers.google.com/web/updates/2017/09/autoplay-policy-changes ) so you now must either:

  • resume() the audio context after some (any) user interaction with the page
  • or be "highly ranked" (ie trust Chrome not to stop audio by default based on user's and world's behavior)
  • or (as far as I get it) user must be on origin A then click a link to same origin A and that new page of A can autoplay things.

You can play a sound using the AudioContext API and taking the source from any ArrayBuffer (ie: from a XMLHttpRequestor a File)

    window.addEventListener('load', function () {
        var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
        var source = audioCtx.createBufferSource();
        var xhr = new XMLHttpRequest();
        xhr.open('GET', 'audio-autoplay.wav');
        xhr.responseType = 'arraybuffer';
        xhr.addEventListener('load', function (r) {
            audioCtx.decodeAudioData(
                    xhr.response, 
                    function (buffer) {
                        source.buffer = buffer;
                        source.connect(audioCtx.destination);
                        source.loop = false;
                    });
            source.start(0);
        });
        xhr.send();
    });

Live example

Works on Chrome and Firefox both mobile and Desktop

Important notes

It's worth mentioning, IMO, that this "trick" can actually be considered as a browser bug, and might no longer work at any time if browser decide that this breaks user experience/becomes a widely-used annoyance (like ads).

It's also worth mentioning that, at least on my mobile and FF 54, the sound will still be played, even if your mobile is muted...

It's also also worth mentionning that user might set the autoplay behavior to fit their wishes and needs either through the browser's usual options or through the more-advanced about:config page (autoplay behavior is set by Firefox's media.autoplay.enabled and media.block-autoplay-until-in-foreground preferences).

So forcing the audio autoplay is a bad UX idea no matter how you do it.

Pina answered 29/9, 2017 at 9:24 Comment(11)
It's critical to mention that this solution will lead to memory leak or a huge usage of memory for audio files larger than 1 MB. An estimation would be, a 1 MB encoded audio file will be 11MB of decoded buffer of data in memory.Hydromel
@FarzadYZ It would be interesting if you can show their is a "memory leak" (which I doubt and is not a matter of the executed JS code but a browser-or-so issue, ie the executor) or a "huge memory consumption" (compared to usual audio tag)Pina
Loading the audio, decoding it using the web audio API’s context.decodeAudioData, creating an AudioBufferSourceNode and playing that  gives you a lot more flexibility, but comes with a rather important caveat: it will crash your phone.There’s a simple reason for that. The browser needs to store the entire audio clip, decoded, in memory, which  since a 5Mb mp3 file typically equates to a 55Mb wav file  you can quickly find yourself running out of memory if you’re using large audio files. When that happens, the way you find out about it is the whole tab going kaput.Hydromel
Source: medium.com/@Rich_Harris/…Hydromel
It's all about Buffers, and not plain raw memory data. Thus, Firefox devtools show no huge memory print for the MDN sample mdn.github.io/webaudio-examples/decode-audio-data So that empiric source (which tries to promote yet another JS framework) seems to just be about "I crashed my phone and blindly blamed the Audio Buffer" IMOPina
I don't have personal experience with this API and can't argue. Just wanted to mention that users who are using this approach better see the edge case of memory too. This doesn't necessarily mean that it would crash or leak the memory.Hydromel
its play audio.but after some time.any help ??Calfskin
@Calfskin The delay is due to network latency (loading the audio resource). Reduce file size, or precache it (ie: from a page loaded before the time your sound is required). Or find a way to make streams out of this JS code (but I doubt it can be done)Pina
@xenos audio size is 850Kb.I want to play audio as the page loaded.Calfskin
This doesn't appear to work anymore. On Safari I get no sound, and on Chrome I only get sound after clicking on the page someplace. On Safari 12.1.2 I had to convert the decodeAudioData syntax back to the pre-promises form to avoid a syntax error.Alanis
@JasonCampbell Yes, Chrome changed the policy they have for Autoplay when landing on a page: you must either be "high ranked using Google stats" to autoplay something when user arrives on your origin, or you must wait for a user interaction on the page, or you must be the 2nd page user visits on your origin (that is, user clicked a link from your website to your website). See developers.google.com/web/updates/2017/09/…Pina
F
9

There is no way to get autoplay working in mobile browsers. (This is not allowed)

But some tricks do this thing.

Click on the links below to view some tricks

Autoplay audio on mobile safari

iOS-Specific Considerations | Loop Attribute

Feverous answered 17/1, 2016 at 12:40 Comment(2)
You're right about the autoplay for audio tag, but it seems you can autoplay a sound using AudioContext (see my answer)Pina
Then why is that supported ? Reference: developer.mozilla.org/en-US/docs/Web/HTML/Element/…Homestretch
S
0

I have a lot of experience with this problem. It also applies to Javascript audio that loads before the user has had a chance to interact with the page.

HOWEVER, once a user clicks absolutely anything, it's game on. I personally recommend a full-page entrance overlay right on the front page: "Click to enter my awesome site!" If the user enters the page at all, they are doomed to hear whatever sounds you want to throw at them! :D

Stomy answered 25/8, 2020 at 8:49 Comment(3)
Not sarcastically - how can I make that happen? Im working on a web game and I'm currently facing this problem. I want user to click "play" button and let the music play in the background.Horribly
If you just want to know how to play background audio, Google "play audio in JavaScript", for example https://mcmap.net/q/53205/-how-to-play-audio. The main thing for this question is that you have to know the following: you CAN'T autoplay audio on most phones (the "autoplay" flag will be ignored), and you CAN'T start playing audio from document load events, so you have to start the audio playing in a user event (like a button click). For a game, just make sure that the "Start" button handler starts playing your audio and you should be fine.Stomy
I'm not focusing on phones, but only computer browsers. I actually got it working after messing with js for few hours straight. I made the play button followed by multiple z-index hacks. Thanks a lot ♥.Horribly

© 2022 - 2024 — McMap. All rights reserved.