javascript Safari 上的 AudioContext
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29373563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AudioContext on Safari
提问by Phillip Senn
Yesterday, I had a question about the noteOn method of the AudioContext object. I've gotten myself all turned around now on this AudioContext object. Here's what I've tried and their associated error messages in Safari on my desktop:
昨天,我有一个关于 AudioContext 对象的 noteOn 方法的问题。我现在对这个 AudioContext 对象彻底改观了。这是我在桌面上的 Safari 中尝试过的及其相关的错误消息:
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 do I define myAudioContext such that it works on all browsers?
问:如何定义 myAudioContext 使其适用于所有浏览器?
回答by Marco Bonelli
Browser support limitations
浏览器支持限制
The Web Audio API(id est AudioContext
) is not supported by all the browsers. Some browsers may have it prefixed with their vendor prefix, but older browsers do not support it at all. Therefore, to answer your question: you cannot use the AudioContext
on allthe browsers.
该网络音频API(ID EST AudioContext
)是不是所有的浏览器都支持。某些浏览器可能会在其供应商前缀前加上前缀,但较旧的浏览器根本不支持它。因此,要回答您的问题:您不能AudioContext
在所有浏览器上使用 。
Nonethless, you can still use the Web Audio API on supported browser using feature detection (see below), or just check if your browser supports it here. Take a look and find your Safari version: this site tells you if the feature is available and, if prefixed, which prefix you'll have to use.
尽管如此,您仍然可以使用功能检测(见下文)在受支持的浏览器上使用 Web Audio API,或者只需在此处检查您的浏览器是否支持它。查看并找到您的 Safari 版本:此站点会告诉您该功能是否可用,如果有前缀,则您必须使用哪个前缀。
AudioContext
feature detection
AudioContext
特征检测
To be sure you can use the Web Audio API on any browser which supportsit, you can use feature detection with relative fallbacks to the vendor-prefixed objects. In case the AudioContext
object is not supported, you'll halt the execution of your script and alert the user. Here's an example:
为确保您可以在任何支持Web Audio API 的浏览器上使用它,您可以使用功能检测以及对供应商前缀对象的相对回退。如果AudioContext
对象不受支持,您将停止执行脚本并提醒用户。下面是一个例子:
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");
}
Note that as of now webkit
is the only existing vendor-prefixed AudioContext
, because Mozilla and Opera use the regular unprefixed object, and IE doesn't support the Web Audio API yet.
请注意,截至目前webkit
是唯一存在的供应商前缀AudioContext
,因为 Mozilla 和 Opera 使用常规的无前缀对象,而 IE 尚不支持 Web Audio API。