Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple way to test if the browser supports the Web Audio Api?

Can anyone tell me a simple and reliable test (in javascript/jquery) for whether the current browser supports the Web Audio Api? Needs to work on mobile.

https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API

like image 209
Max Williams Avatar asked Dec 14 '22 09:12

Max Williams


2 Answers

If I understand you correctly...

var context;
window.addEventListener('load', init, false);
function init() {
  try {
    // Fix up for prefixing
    window.AudioContext = window.AudioContext||window.webkitAudioContext;
    context = new AudioContext();
  }
  catch(e) {
    alert('Web Audio API is not supported in this browser');
  }
}

That's taken directly from here (article by Boris Smus).

like image 175
ann0nC0d3r Avatar answered Dec 17 '22 00:12

ann0nC0d3r


Here's a way to initialize a web audio context taking into account future implementations of the Web Audio API which don't exist yet. Taken from here.

var contextClass = (window.AudioContext || 
  window.webkitAudioContext || 
  window.mozAudioContext || 
  window.oAudioContext || 
  window.msAudioContext);
if (contextClass) {
  // Web Audio API is available.
  var context = new contextClass();
} else {
  // Web Audio API is not available. Ask the user to use a supported browser.
}
like image 45
thanksd Avatar answered Dec 17 '22 00:12

thanksd