Florian d'Erfurth
Florian d'Erfurth

Reputation: 469

Playing sound from object URL

I use XMLHttpRequest to preload files for a game. If WebAudio isnt supported I fall back to using audio elements.

On Android it works fine in Firefox but sound does not play in chrome. Here's some testing code:

document.addEventListener('touchstart', function(event) {
    event.preventDefault();
    if( window.audio ) window.audio.play();
    console.log( window.audio );
});

window.audio    = null;


var xhr = new XMLHttpRequest();
xhr.open('GET', '/sounds/shotgun.webm', true);
xhr.responseType = 'arraybuffer';

xhr.onreadystatechange = function() {
    if (xhr.readyState === 4) {
        var blob = new Blob( new Array( new Int8Array( xhr.response ) ), { type: 'audio/webm' } );
        var url = window.URL.createObjectURL( blob )
        window.audio = new Audio();
        window.audio.src = url;

        console.log( 'sound loaded' );
    }
    };

xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send();

I suspect this is a Chrome for android issue with the object url...

Anyway I'm looking for a way to play sounds on Chrome for Android using cached data from an XMLHttpRequest.

Upvotes: 2

Views: 2105

Answers (2)

Kinlan
Kinlan

Reputation: 16597

This does in fact appear to be a bug. I have raised it here https://code.google.com/p/chromium/issues/detail?id=227476 and created a test case for it. When I get back from vacation I will assign it to the correct team if it has not already been triaged. I will also update this answer if I find any other work around.

Upvotes: 3

Kevin Ennis
Kevin Ennis

Reputation: 14456

I don't have an Android phone handy, but I know that in the past you needed to use window.webkitURL instead of window.URL. Maybe try:

( window.URL || window.webkitURL ).createObjectURL( blob )

Upvotes: 0

Related Questions