首页 > 解决方案 > 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()我返回的地方,然后在我检查变量是否不等于然后只执行它下面的代码。所以它不应该执行该代码,但它仍然会在该行抛出语法错误。undefinedAudioContextaudioRecording()audioContextundefinedIE

我的代码中也有声明,但在(在开发工具中)console.log()控制台上没有看到任何内容。IE 11

我在这里犯了什么错误,什么是更优雅的方法来确保 IE 不会进入错误抛出线,因此不会抛出错误?

标签: javascriptinternet-explorerinternet-explorer-11audiocontext

解决方案


推荐阅读