javascript - Javascript AudioContext 检查在 Internet Explorer 11 中不起作用
问题描述
我有这个JS
代码,我正在检查是否AudioContext
已定义然后设置它,否则返回undefined
:
function audioContextCheck() {
if (typeof AudioContext !== "undefined") {
return new AudioContext();
} else if (typeof webkitAudioContext !== "undefined") {
return new webkitAudioContext();
} else if (typeof mozAudioContext !== "undefined") {
return new mozAudioContext();
} else {
// return undefined
return "undefined"
}
}
var audioContext = audioContextCheck();
console.log(audioContext)
function audioRecording( e ) {
try{
console.log('audioContext is '+ audioContext)
if (audioContext != "undefined") {
audioContext.resume().then(() => {
console.log('Resumed audioContext on end-recording');
e.classList.remove('recording');
recording = false;
});
}
else {
console.log('IE audioContext not supported')
e.classList.remove('recording');
recording = false;
socketio.emit('end-recording');
}
}
catch(error){
console.log(error)
console.log('Resumed audioContext on end-recording');
e.classList.remove('recording');
recording = false;
}
}
}
现在这个文件是从一个页面JS
加载的。html
但是,Internet Explorer 11
每当我加载此页面时,我都可以syntax error
在 line看到audioContext.resume().then(() => {
。我知道 IE 不支持AudioContext
,这就是为什么我在不支持的情况下使用audioContextCheck()
我返回的地方,然后在我检查变量是否不等于然后只执行它下面的代码。所以它不应该执行该代码,但它仍然会在该行抛出语法错误。undefined
AudioContext
audioRecording()
audioContext
undefined
IE
我的代码中也有声明,但在(在开发工具中)console.log()
控制台上没有看到任何内容。IE 11
我在这里犯了什么错误,什么是更优雅的方法来确保 IE 不会进入错误抛出线,因此不会抛出错误?
解决方案
推荐阅读
- android - 颤振 APK 构建
- python - python如何使用烧瓶中的werkzeug记录器将错误消息写入错误日志和主日志
- c# - 如何在不同的 Outlook 客户端中获取相同的 MailItem 唯一属性
- python - 如何使用 ipywidget 按钮下载文件?
- c# - 异步任务中的MVVMLight WPF ProgressBar绑定
- mysql - 如何在 MySQL 5.x 中选择部分字符串
- android - Android:BottomNavigationView 图标不可见
- python-3.x - 检查输入时出错:预期 dense_28_input 有 2 维,但得到的数组形状为 (3084, 32, 32)
- node.js - 使用 nodejs 在数据库中安排作业
- android - 如何使用约束布局在一行中对齐内容