HTML5 Audio doesn't Autoplay for Chrome on Android

    Recently I was playing Penguins Rising on my nexus 7 and notice that the background music wasn't playing but strangely all the other sounds were. After a hard look at the code, I found no reason for the audio not to be work. So I could tell right away this wasn't going to be an easy fix. The first thing I did was check the support to see if  the HTML5 audio element had any support issues. Since the support for the audio element seemed to be alright with what I was using, the next thing I thought was that it must be the attribute autoplay. Its important to note that I am also using the loop attribute but I will return to that later.

Example from Penguins Rising
<audio autoplay loop>
   <source src="content/ArcticWind.wav" type="audio/wav"> 
   <source src="content/ArcticWind.ogg" type="audio/ogg">          
   <source src="mp3/ArcticWind.mp3" type="audio/mpeg">
</audio>

Currently I have for my solution is to simply start the music when the user click the start button at the beginning of the game. As indicated in the resources the cause of this issue is that all sounds/music must be user event driven and activated by user input. So a quick fix would be to create the audio element and then attach a method to play the audio after a click, but I am looking at a better solution listed below.

var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'loading.ogg');
audioElement.play();

Resources
Chrome on Android doesn't automatically play song vs Chrome on PC does - Stack Overflow
Issue 138132 - To play HTML5 audio an explicit action by the user is needed - Won't Fix!

Possibly a better Solution Using Web Audio API
Getting Started with Web Audio API - HTML5 Rocks - Exploring this possibility!

Comments

Popular posts from this blog

EF: The database cannot be opened because it is version 782

Updating Razor 2.0 to 3.0 with Asp.net MVC