Safari上的AudioContext [英] AudioContext on Safari

查看:476
本文介绍了Safari上的AudioContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天,我有有关AudioContext对象的noteOn方法的问题. 我现在已经在这个AudioContext对象上把所有的东西都转了过来. 这是我在台式机上的Safari浏览器中尝试过的内容以及与之相关的错误消息:

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:

	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使其在所有浏览器上都能正常工作?

Q: How do I define myAudioContext such that it works on all browsers?

推荐答案

浏览器支持限制

Web音频API (标识为AudioContext )不受所有浏览器支持.某些浏览器可能以其供应商前缀作为前缀,但是较旧的浏览器根本不支持它.因此,要回答您的问题:您不能在所有浏览器上使用AudioContext.

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 all the browsers.

没什么,您仍然可以使用功能检测在支持的浏览器上使用Web Audio API(请参见下文),或者只是检查您的浏览器是否支持

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,可以将功能检测与供应商前缀对象的相对后备时间一起使用.如果不支持AudioContext对象,则将停止脚本的执行并警告用户.这是一个示例:

To be sure you can use the Web Audio API on any browser which supports it, 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:

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.

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.

这篇关于Safari上的AudioContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆