昨天,我吃了a question about the noteOn method of the AudioContext object。在这个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)')
问:我如何定义myAudioContext,使其在所有浏览器上都能工作?
发布于 2015-04-01 00:38:54
浏览器支持限制
并非所有浏览器都支持Web Audio API (id est AudioContext
)。一些浏览器可能会以其供应商前缀作为前缀,但较旧的浏览器根本不支持它。因此,回答你的问题:你不能在所有的浏览器上使用AudioContext
。
无论如何,你仍然可以通过功能检测在支持的浏览器上使用Web Audio API (见下文),或者只是检查你的浏览器是否支持。看看并找到你的Safari版本:这个站点告诉你这个特性是否可用,如果有前缀,你必须使用哪个前缀。
AudioContext
特征检测
为确保您可以在支持 it的任何浏览器上使用网络音频应用编程接口,您可以使用带有供应商前缀对象的相对回退的功能检测。如果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");
}
请注意,到目前为止,webkit
是唯一存在的以供应商为前缀的AudioContext
__,因为Mozilla和Opera使用常规的无前缀对象,而IE还不支持Web Audio API。
发布于 2020-06-22 20:41:39
var AudioContext = window.AudioContext // Default
|| (window as any).webkitAudioContext;// Safari and old versions of Chrome
this.audioContext = new AudioContext();
https://stackoverflow.com/questions/29373563
复制相似问题