我从一个网站获得了这个片段(目前无法记住它)并且它已停止工作。
我用它来演奏音调。
我最近做错了还是Chrome更改了?
Play = (function () {
var ctx = new(window.audioContext || window.webkitAudioContext);
return function (duration, freq, finishedCallback) {
duration = +duration;
if (typeof finishedCallback != "function") {
finishedCallback = function () {};
}
var osc = ctx.createOscillator();
osc.type = 0;
osc.connect(ctx.destination);
osc.frequency.value = freq;
osc.noteOn(0);
setTimeout(
function () {
osc.noteOff(0);
finishedCallback();
}
,duration
);
};
})();
Play(50,500)

答案 0 :(得分:5)
这里有两个问题 - 没有audioContext
(小“a”,但此刻不影响Chrome)。只需将其更改为:
var ctx = new (window.AudioContext || window.webkitAudioContext);
添加对start()的支持,这是最新的方法。有几种方法可以做到这一点,这是一个基本的例子:
if (osc.start) {
osc.start(0);
}
else {
osc.noteOn(0);
}
(当然还有osc.noteOff(0)
➜osc.stop(0)
)
Play = (function() {
var ctx = new(AudioContext || webkitAudioContext);
return function(duration, freq, finishedCallback) {
duration = +duration;
if (typeof finishedCallback != "function") {
finishedCallback = function() {};
}
var osc = ctx.createOscillator();
osc.type = 0;
osc.connect(ctx.destination);
osc.frequency.value = freq;
if (osc.start) osc.start();
else osc.noteOn(0);
setTimeout(
function() {
if (osc.stop) osc.stop(0);
else osc.noteOff(0);
finishedCallback();
}, duration
);
};
})();
Play(50, 500)