ReferenceError: AudioContext not defined

Frank Cangialosi picture Frank Cangialosi · Sep 7, 2013 · Viewed 11.8k times · Source

I'm trying to use the RecorderJS library (https://github.com/mattdiamond/Recorderjs) which requires me to have an AudioContext. However, when declaring the AudioContext at the very beginning of my script, I am getting an error in the console on page load that says "ReferenceError: AudioContext not defined." As anyone else ever encountered issues with the AudioContext like this? I've posted both a snippet of my JS, and the HTML where everything is being included. Thanks in advance!

JS:

var audioContext = new AudioContext();
var audioInput = null, inputPoint = null, audioRecorder = null;

$(document).ready(function(){
    // recording stuff
});

HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>Recording</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <link rel='stylesheet' type='text/css' href='stylesheet.css'/>
        <script src="http://cwilso.github.io/AudioContext-MonkeyPatch/AudioContextMonkeyPatch.js"></script>
        <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
        <script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
        <script src="recorder.js"></script>
        <script src="script.js"></script>
    </head>
    <body>
        <button class="record" type='button'>Record</button>
    </body>
</html>

Answer

Lewistrick picture Lewistrick · Jan 15, 2019

Put this at the very beginning of your script (it's also in main.js in Matt Diamond's example):

window.AudioContext = window.AudioContext || window.webkitAudioContext;

This loads the correct AudioContext independent of your browser.