Home > JavaScript, Uncategorized > Android Chrome does not allow applications to play HTML5 audio without an explicit action by the user

Android Chrome does not allow applications to play HTML5 audio without an explicit action by the user

I was working a site that required to play some sound effects at different times and when testing on my Android phone, I was very surprised to find that only some of the sounds would play.

I assumed that there must be a restriction on bit rate or something else specific to the sound file as some played fine and others didn’t play at all, all using the same code.

I then stumbled across this chromium issue 178297 https://code.google.com/p/chromium/issues/detail?id=178297

It turns out there is a mobile specific restriction put in place to disable playback of media if not a direct result of user interaction.

Now I can see why this exists as I HATE auto play videos and ads and they would be even worse on mobile, however I have a valid use for this.

The reason only some of my sounds worked was some were triggered by a button click, while other were trigged by a URL hash change.

A button click is a user interaction / gesture but hash change is not.

Luckily Philip Jägenstedt has found a work around to the situation by using the first user interaction to “warm up” or initialise each audio element.

This then allows non user interaction events to use the elements moving forward.

His post on the issue, including his workaround is at http://blog.foolip.org/2014/02/10/media-playback-restrictions-in-blink/

I have also put this work around into simple-audio which is a JavaScript module to make playing audio in the browser easier.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: