首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Safari上的AudioContext

Safari上的AudioContext
EN

Stack Overflow用户
提问于 2015-04-01 00:23:49
回答 2查看 24.5K关注 0票数 27

昨天,我吃了a question about the noteOn method of the AudioContext object。在这个AudioContext对象上,我已经完全改变了方向。以下是我在桌面上的Safari中尝试过的内容及其相关的错误消息:

代码语言:javascript
运行
复制
	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,使其在所有浏览器上都能工作?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-01 00:38:54

浏览器支持限制

并非所有浏览器都支持Web Audio API (id est AudioContext)。一些浏览器可能会以其供应商前缀作为前缀,但较旧的浏览器根本不支持它。因此,回答你的问题:你不能在所有的浏览器上使用AudioContext

无论如何,你仍然可以通过功能检测在支持的浏览器上使用Web Audio API (见下文),或者只是检查你的浏览器是否支持。看看并找到你的Safari版本:这个站点告诉你这个特性是否可用,如果有前缀,你必须使用哪个前缀。

AudioContext特征检测

为确保您可以在支持 it的任何浏览器上使用网络音频应用编程接口,您可以使用带有供应商前缀对象的相对回退的功能检测。如果AudioContext对象不受支持,您将暂停脚本的执行并警告用户。下面是一个例子:

代码语言:javascript
运行
复制
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。

票数 41
EN

Stack Overflow用户

发布于 2020-06-22 20:41:39

代码语言:javascript
运行
复制
  var AudioContext = window.AudioContext // Default
      || (window as any).webkitAudioContext;// Safari and old versions of Chrome

    this.audioContext = new AudioContext();
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29373563

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档