Audio context in Safari

Yesterday I had a question about the noteOn method of an AudioContext object . Now I have completely turned around on this AudioContext object. Here is what I tried and the related error messages in Safari on my desktop:

var ctx // ctx = new(AudioContext || webkitAudioContext); // ReferenceError: Can't find variable: AudioContext // ctx = new(audioContext || webkitAudioContext); // ReferenceError: Can't find variable: audioContext // ctx = new(window.AudioContext || webkitAudioContext); // ReferenceError: Can't find variable: webkitAudioContext ctx = new(window.AudioContext || window.webkitAudioContext); // TypeError: 'undefined' is not a constructor (evaluating 'new(window.AudioContext || window.webkitAudioContext)') 

Q: How to define myAudioContext so that it works in all browsers?

+9
source share
1 answer

Browser Support Limitations

Web Audio API (i.e. AudioContext ) is not supported by all browsers. Some browsers may have a prefix prefixed with their provider, but older browsers do not support it at all. Therefore, to answer your question: you cannot use AudioContext in all browsers.

However, you can still use the Web Audio API in a supported browser using the discovery function (see below), or just check if your browser supports it here . Look and find your version of Safari: this site tells you if this feature is available, and if there is a prefix, which prefix you will have to use.

AudioContext Function Detection

To make sure that you can use the Web Audio API in any browser that supports it , you can use the function detection function with relative rollbacks to objects with a provider prefix. If the AudioContext object is not supported, you will stop the script and warn the user. Here is an example:

 var AudioContext = window.AudioContext // Default || window.webkitAudioContext // Safari and old versions of Chrome || false; if (AudioContext) { // Do whatever you want using the Web Audio API var ctx = new AudioContext; // ... } else { // Web Audio API is not supported // Alert the user alert("Sorry, but the Web Audio API is not supported by your browser. Please, consider upgrading to the latest version or downloading Google Chrome or Mozilla Firefox"); } 

Please note that at the moment webkit is the only existing AudioContext provider prefix, since Mozilla and Opera use a regular object without a prefix, and IE does not yet support the Web Audio API.

+14
source

Source: https://habr.com/ru/post/984511/


All Articles