【问题标题】:Playing sound from object URL从对象 URL 播放声音
【发布时间】:2013-03-23 14:04:42
【问题描述】:

我使用 XMLHttpRequest 为游戏预加载文件。如果不支持 WebAudio,我会退回到使用音频元素。

在 Android 上,它在 Firefox 中运行良好,但在 chrome 中无法播放声音。下面是一些测试代码:

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();

我怀疑这是对象 url 的 Chrome for android 问题...

无论如何,我正在寻找一种方法来使用来自 XMLHttpRequest 的缓存数据在 Chrome for Android 上播放声音。

【问题讨论】:

    标签: android google-chrome xmlhttprequest html5-audio


    【解决方案1】:

    这实际上似乎是一个错误。我在这里提出了https://code.google.com/p/chromium/issues/detail?id=227476 并为它创建了一个测试用例。当我从假期回来时,如果尚未对其进行分类,我会将其分配给正确的团队。如果我发现任何其他解决方法,我也会更新此答案。

    【讨论】:

    • 您对此有进一步了解吗?
    【解决方案2】:

    我手边没有安卓手机,但我知道过去您需要使用window.webkitURL 而不是window.URL。也许试试:

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

    【讨论】:

    • 不行不行。我还尝试使用文件系统 API 创建 url,但运气不好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 2015-02-04
    相关资源
    最近更新 更多