ReferenceError: AudioContext not defined

17.1k views Asked by At

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>
2

There are 2 answers

0
Lewistrick On

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.

0
Davo On

Your code ran fine for me in Chrome29, does your browser support the Web Audio API? (http://caniuse.com/audio-api)

I was getting a "ReferenceError: AudioContext not defined" error using AudioContext() instead of webkitAudioContext() before using the AudioContext Monkey Patch like you have. Sure its loaded and not a caching/hard-refresh issue?