Phi*_*enn 17 javascript webkitaudiocontext
昨天,我对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)')Run Code Online (Sandbox Code Playgroud)
问:如何定义myAudioContext使其适用于所有浏览器?
Mar*_*lli 22
不幸的是,Web Audio API(id est AudioContext)是一个新引入的功能,并不是所有浏览器都支持.某些浏览器可能以其供应商前缀为前缀,但旧版浏览器根本不支持它.因此,要回答您的问题:您无法AudioContext在所有浏览器上使用.
不过,您仍然可以使用功能检测在支持的浏览器上使用Web Audio API(请参阅下文),或者只是在此处检查您的浏览器是否支持它.看看并找到您的Safari版本:该网站会告诉您该功能是否可用,如果有前缀,您必须使用哪个前缀.
AudioContext 特征检测为确保您可以在任何支持它的浏览器上使用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");
}
Run Code Online (Sandbox Code Playgroud)
请注意,截至目前webkit是唯一的现有供应商 - 前缀AudioContext,因为Mozilla和Opera使用常规的无前缀对象,而IE尚不支持Web Audio API.
| 归档时间: |
|
| 查看次数: |
12339 次 |
| 最近记录: |