codehs-graphics
Version:
Helpers used to run graphics problems in the CodeHS editor.
36 lines (33 loc) • 1.16 kB
JavaScript
/*
* Defines a cross-browser compatible way of creating a new AudioContext.
* Returns 0 and logs an error message if creating a new AudioContext is not possible.
*/
function getAudioContext() {
// Test for browser compatibility
// Source: https://www.safaribooksonline.com/library/view/web-audio-api/9781449332679/s01_2.html
var contextClass =
window.AudioContext ||
window.webkitAudioContext ||
window.mozAudioContext ||
window.oAudioContext ||
window.msAudioContext;
if (contextClass) {
// Web Audio API is available. Attempt to construct an AudioContext
try {
return new contextClass();
} catch (e) {
console.log(
'Too many AudioContexts are in use. Please close all browser windows and retry.'
);
return 0;
}
} else {
// Web Audio API is not available
console.log(
'Web Audio is not supported in this browser. Please use the most up to date version of Chrome, Firefox, or Safari.'
);
return 0;
}
}
module.exports = getAudioContext;
;