14

I've made an HTML5 iPod.

You can try it here.

http://inventikasolutions.com/demo/iPod

On a PC, while using Chrome. If I navigate to a song, it starts playing automatically. But while using Chrome on Android it doesn't play the song. I have to hit the play/pause button again to play the audio.

Here is the code which runs, when you select the song to play:

audioPlayer.src=songurl[number];
audioPlayer.oncanplaythrough = "isAppLoaded";
audioPlayer.autoplay = "autoplay";
audioPlayer.addEventListener('ended',nextSong,false);
document.getElementById("player").appendChild(audioPlayer);

and here is the play/pause code.

        if (audioPlayer.paused)
        {
        audioPlayer.play();
        $("#pauseindicator").hide();
        $("#playindicator").show();
        }
        else
        {
        audioPlayer.pause();
        $("#pauseindicator").show();
        $("#playindicator").hide();
        }

Could it have some thing to do with the 'autoplay' variable? The default browser in Android plays the song immediately.

Thanks.

Bilbo Baggins
  • 3,644
  • 8
  • 40
  • 64

5 Answers5

34

In case someone bumps into this - Android version of Chrome in fact blocks autoplay function, but you can change settings of the browser. To do this, you have to enter chrome://flags and set Disable gesture requirement for media playback to on. It's impossible to force this setting via web, but if you're writing a dedicated web app like I did, you can use it.

  • 2
    I was working on an Android App, and encountered this problem. After I saw your solution, I guess there may be some way to handle this in my java code. And finally I found this worked well. **webview.getSettings().setMediaPlaybackRequiresUserGesture(false);** – wizcheu Mar 03 '17 at 13:17
  • 1
    Fun fact: slashes aren't required for the about pages, so `about:flags` is fine (will redirect to `chrome://flags`) – David Gilbertson May 15 '17 at 07:24
  • As of 9/28/21, the "disable gesture requirement for media playback" is no longer a flag. – John Miller Sep 28 '21 at 18:18
8

Please refer this link ...

http://code.google.com/p/chromium/issues/detail?id=138132

Chrome does not allow applications to play HTML5 audio without an explicit action by the user, similar to how it is handled by iOS, but differently than the stock Android browser handles it.

Autoplay is not honored on android as it will cost data usage.

Muhammed Basil
  • 1,834
  • 1
  • 21
  • 39
8

Add create.js to your page:

<script src="https://code.createjs.com/createjs-2015.11.26.min.js"></script>

and then launch sound this way:

<script>
    createjs.Sound.registerSound("./click.mp3", "x");
    setTimeout(function () {
        createjs.Sound.play("x");
    }, 10000)
</script>

Works for me on lastest Android.

Stepan Yakovenko
  • 8,670
  • 28
  • 113
  • 206
6

It looks like there is new way to active Chrome to autoplay audio and video

Go to chrome://flags

Set Autoplay policy to No user gesture is required

Chrome flags

Hemerson Varela
  • 24,034
  • 16
  • 68
  • 69
3

Update your Chrome to 29, this version now support WebAudio API and fixed many issues.

see: Chrome for Android Update

I'm using the SoundJS Lib and work very well in all browsers (only don't work well in Default Android Browser and Chrome with the Android version is less than 4.1).

See these examples: www.createjs.com/#!/SoundJS/demos/testSuite

Support for Web Audio API on Chrome for Android: code.google.com/p/chromium/issues/detail?id=166003

SoundJS limitations: community.createjs.com/kb/faq/soundjs-faq

Victor Jatobá
  • 811
  • 7
  • 13