【问题标题】:Is Web Audio API source.start(0) supported on Safari (it works just fine on Chrome)?Safari 是否支持 Web Audio API source.start(0)(它在 Chrome 上运行良好)?
【发布时间】:2013-10-05 15:58:58
【问题描述】:

我正在尝试实现 Web Audio API。该代码适用于 Chrome 29.0.1547.76,但不适用于 Safari 6.0.5 (8536.30.1)。关键是我是用noteOn(0)还是start(0)。

我想使用 start() 来播放声音的一部分:

asource.start(0, 2, 1);

在 Chrome 中工作正常(立即播放,从 2 秒标记开始,播放 1 秒)但结果

TypeError: 'undefined' is not a function (evaluating 'asource.start(0, 2, 1)')

在 Safari 上。将那一行替换为

asource.noteOn(0);

有效。 [嗯,我需要调用 noteOff(0) 而不是 stop(0)。] 我得到与 start(0) 相同的错误。所以,我假设 Safari 没有实现 start(0)?但如果是这样,为什么HTML5 Rocks 中使用 start(0) 的一些示例有效?

作为参考,这里是完整的网页。我尝试了许多不同的声音/格式;都会导致相同的错误。

<!DOCTYPE html>
<html lang=en>
<head>
<meta charset="utf-8">
<title>Web Audio API Issue</title>
</head>
<body>
<p>Example working on Chrome but not Safari when using start()</p>

<button id="Load" onclick="init()" >Load</button>
<button id="Play" onclick="playSound()" disabled>Play</button>
<button id="Stop" onclick="stopSound()" disabled>Stop</button>

<script>
    var web_audio_context;
    var abuffer;
    var asource;

    function init() {
        contextClass = (window.AudioContext || 
            window.webkitAudioContext || 
            window.mozAudioContext || 
            window.msAudioContext);
        web_audio_context = new contextClass();

        var theURL = './digits.mp3';
        var xhr = new XMLHttpRequest();
        xhr.open('GET', theURL, true);
        xhr.responseType = 'arraybuffer';
        xhr.onload = function(e) {
            finishedLoading(this.response);
        };
        xhr.send();
    }

    function finishedLoading(arrayBuffer) {
        web_audio_context.decodeAudioData(arrayBuffer, function(buffer) {
            abuffer = buffer;
            document.getElementById('Load').disabled = true;
            document.getElementById('Play').disabled = false;
            document.getElementById('Stop').disabled = false;
        }, function(e) {
            console.log('Error decoding file', e);
        }); 
    }

    function playSound() {
        asource = web_audio_context.createBufferSource();
        asource.buffer = abuffer;
        asource.connect(web_audio_context.destination);
        asource.start(0, 2, 1);

    }

    function stopSound() {
        asource.stop(0);
    }
</script>
</body>
</html>

【问题讨论】:

  • 根据developer.mozilla.org/en/docs/Web/API/AudioBufferSourceNode,AudioBufferSourceNode 在 Safari 上的状态为“未知”。在这种情况下,我想这不是完全实现的。
  • Safari 6.1(10 月 22 日发布)修复了这个问题。该代码现在可以在 Chrome 和 Safari 中使用。

标签: javascript html google-chrome safari web-audio-api


【解决方案1】:

在较新的 Web Audio API 版本中,方法 noteOn 被重命名为 start。 Safari 仍然使用旧版本,而 Chrome 使用更新的版本。

试试吧:

asource.start ? asource.start(0, 2, 1) : asource.noteOn(0, 2, 1);

【讨论】:

  • 感谢您的建议,但在 Safari 6.0.5 上,asource.noteOn(0,2,1) 相当于 asource.noteOn(),即它从头开始播放整个声音,而不是从头开始播放 2 秒并播放 1 s.
  • 为了让 safari 和 chrome 在 iOS 上运行,我也必须添加以下代码: asource.stop ? asource.stop(0) : asource.noteOff(0);
  • @boosth 没错。我也不得不使用 stopsource.stop ? source.stop(0) : source.noteOff(0);
【解决方案2】:

尝试将 Chris Wilson 的 Web Audio API monkey patch 库添加到您的项目中。 这可能会有所帮助。

【讨论】:

    猜你喜欢
    • 2018-09-25
    • 1970-01-01
    • 2021-08-11
    • 1970-01-01
    • 2014-12-25
    • 2010-12-23
    • 2018-02-04
    • 1970-01-01
    • 2015-02-03
    相关资源
    最近更新 更多