[英]Why does this code work in Safari but not Chrome? Arrrgh
Chrome v6, Safari v7 (works in v6) Chrome v6,Safari v7(适用于v6)
if('AudioContext' in window) {
var myAudioContext = new AudioContext();
const PATH = 'Sounds/',
SOUNDS = ['Tock08'];
var soundBuffers = {}, myNodes = {};
function fetchSounds() {
var request = new XMLHttpRequest();
for (var i = 0, len = SOUNDS.length; i < len; i++) {
request = new XMLHttpRequest();
request._soundName = SOUNDS[i];
request.open('GET', PATH + request._soundName + '.aiff', true);
request.responseType = 'arraybuffer';
request.addEventListener('load', bufferSound, false);
request.send();
}
}
function bufferSound(event) {
var request = event.target;
var buffer = myAudioContext.createBuffer(request.response, false);
soundBuffers[request._soundName] = buffer;
}
}
function clickSound() {
if('AudioContext' in window ) { //Chrome doesn't work with this or above code
// create a new AudioBufferSourceNode
source = myAudioContext.createBufferSource();
source.buffer = soundBuffers['Tock08'];
source.loop = false;
source.connect(myNodes.volume);
myNodes.volume.gain.value = 1;
myNodes.volume.connect(myAudioContext.destination);
source.noteOn(0); // play right now (0 seconds from now)
}
}
In Safari, all is well. 在Safari中,一切都很好。 An array of sound buffers is created, and a call to clickSound results in a satisfying "click". 将创建一个声音缓冲区数组,并且对clickSound的调用会产生令人满意的“ click”。
In Chrome, things are different. 在Chrome中,情况有所不同。
The line: 该行:
var buffer = myAudioContext.createBuffer(request.response, false);
is flagged with 被标记为
Uncaught SyntaxError: An invalid or illegal string was specified.
on loading. 在加载时。
Then if I call "clickSound" I get: 然后,如果我调用“ clickSound”,则会得到:
source.buffer = soundBuffers['Tock08'];
Uncaught TypeError: Value is not of type AudioBuffer.
Does anyone know why this problem occurs? 有谁知道为什么会出现此问题?
Chrome still uses an older webkitAudioContext. Chrome仍使用较旧的webkitAudioContext。 This is how I set up context in SoundJS , and it works everywhere: 这就是我在SoundJS中设置上下文的方式 ,它可以在任何地方使用:
if (window.webkitAudioContext) {
s.context = new webkitAudioContext();
} else if (window.AudioContext) {
s.context = new AudioContext();
}
Hope that helps. 希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.