Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Audio Tag Autoplay Not working in mobile

Tags:

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

like image 523
Lakshya Avatar asked Jan 17 '16 11:01

Lakshya


People also ask

How do you set audio tag to play automatically?

The HTML <audio> autoplay attribute is used to specify that the audio should automatically start playing as soon as it is loaded. It is a Boolean attribute.

Does Chrome support autoplay HTML?

As of April 2018, Chrome's autoplay policies changed: "Chrome's autoplay policies are simple: Muted autoplay is always allowed.

How do you automatically add music to HTML?

The autoplay attribute is a boolean attribute. When present, the audio will automatically start playing as soon as it can do so without stopping. Note: Chromium browsers do not allow autoplay in most cases. However, muted autoplay is always allowed.

Which tag is used for audio?

The <audio> tag is used to embed sound content in a document, such as music or other audio streams.


2 Answers

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.

like image 200
Xenos Avatar answered Oct 22 '22 08:10

Xenos


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

like image 39
Ender Kaya Avatar answered Oct 22 '22 07:10

Ender Kaya