Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't make HTML5 Audio Tag to work on mobile browsers

I have a web app that uses the HTML5 <audio> tag and for some reason, while it works fine on Windows and Mac PCs, it doesn't work on iOS and Android. Here's a relevant snippet of my code:

Javascript:

var audioElement = document.querySelector('#audioplayer');
var source = document.querySelector('#mp3');
source.src = tokObject._url;
audioElement.load();
audioElement.play();

HTML:

<center>
    <audio id="audioplayer" style="width:480px;">
        <source id="mp3" src="random-placeholder" type="audio/mp3" />
    </audio>
</center>
like image 839
Pavel Zagalsky Avatar asked Feb 14 '15 11:02

Pavel Zagalsky


People also ask

Does HTML5 <audio> tag work on iOS and Android?

Bookmark this question. Show activity on this post. I have a web app that uses the HTML5 <audio> tag and for some reason, while it works fine on Windows and Mac PCs, it doesn't work on iOS and Android. Here's a relevant snippet of my code:

Is HTML5 audio a mess on mobile?

HTML5 Audio on desktop browsers is a mess. But you haven't experienced true pain and suffering until you try to get HTML5 audio done on mobile devices. This article is a follow-up to my talk at the Barcamp in Salzburg in March and was inspired by Chris Heilmans tweets on that topic a few days ago.

What are the advantages of HTML5 delegation of audio?

So the “advantage” of HTML5 delegation, just putting simple <audio> tags on the page and leaving implementers to take care of all the rest, which should bring optimized user experiences, actually doesn’t work. Do sounds – or better, even a sad, single sound – autoplay on loading a page on mobile?

What browsers do you use to play HTML5 games on?

This story starts when we had to provide audio to an HTML5 game we were building. The game is for mobile browsers, actually a limited and updated range of mobile browsers: Safari for iOS 6+, and the stock browser (i.e. the default browser) for Android 4+.


1 Answers

You normally can't autoplay audio or video files on mobile devices, this is often a restriction by the OSes such as Android and iOS to stop sites from downloading huge media files and autoplaying them.

If such code is called from within a click or touch handler, it will probably work, but not the way you are currently doing it.

Also, the <center> element has been deprecated and you shouldn't use it anymore.

like image 181
Ian Devlin Avatar answered Sep 20 '22 22:09

Ian Devlin