Javascript Safari是否支持Web Audio API source.start(0)(在Chrome上运行良好)?

Javascript Safari是否支持Web Audio API source.start(0)(在Chrome上运行良好)?,javascript,html,google-chrome,safari,web-audio-api,Javascript,Html,Google Chrome,Safari,Web Audio Api,我正在尝试实现Web音频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(

我正在尝试实现Web音频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)')
在旅行中。将这一行替换为

asource.noteOn(0);
工作。[嗯,我需要调用noteOff(0)而不是stop(0)。]我在start(0)中遇到相同的错误。那么,我假设Safari没有实现start(0)?但是如果是这样的话,为什么使用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>

网络音频API问题
使用start()时使用Chrome而不是Safari的示例

负载 玩 停止 var web_audio_上下文; var abuffer; var asource; 函数init(){ contextClass=(window.AudioContext | | window.webkitadiocontext | window.mozAudioContext | | window.msAudioContext); web_audio_context=new contextClass(); var theURL='./digits.mp3'; var xhr=new XMLHttpRequest(); open('GET',theURL,true); xhr.responseType='arraybuffer'; xhr.onload=函数(e){ 完成加载(此响应); }; xhr.send(); } 功能完成加载(阵列缓冲){ web\u audio\u上下文。解码音频数据(arrayBuffer,函数(缓冲区){ abuffer=缓冲区; document.getElementById('Load').disabled=true; document.getElementById('Play')。disabled=false; document.getElementById('Stop')。disabled=false; },功能(e){ console.log('Error decoding file',e); }); } 函数playSound(){ asource=web\u audio\u context.createBufferSource(); asource.buffer=abuffer; 连接(web\u音频\u上下文.destination); a源启动(0,2,1); } 函数stopSound(){ a源停止(0); }
尝试将Chris Wilson的库添加到您的项目中。
这可能会有所帮助。

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

试试看:

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

根据AudioBufferSourceNode,Safari上的状态为“未知”。在这种情况下,我想这不是完全实现的。Safari6.1(10月22日发布)修复了这一问题。该代码现在可以在Chrome和Safari中使用。感谢您的建议,但在Safari 6.0.5中,
asource.noteOn(0,2,1)
相当于
asource.noteOn()
,即。,它从一开始就播放整个声音,而不是在2秒钟内开始播放1秒钟。为了让safari和chrome都能在iOS上运行,我还必须添加以下代码:asource.stop?asource.stop(0):asource.noteOff(0);