There are two problems here - there is no audioContext
(small "a", doesn't affect Chrome at the moment though). Just change it to:
var ctx = new (window.AudioContext || window.webkitAudioContext);
Add support for start(), which is the more recent method. There are several ways to do this, here's a basic example:
if (osc.start) {
osc.start(0);
}
else {
osc.noteOn(0);
}
(and of course, osc.noteOff(0)
? osc.stop(0)
as well)
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)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…