参考错误:AudioContext 未定义

Fra*_*osi 5 html javascript audio audio-recording webkitaudiocontext

我正在尝试使用 RecorderJS 库(https://github.com/mattdiamond/Recorderjs),它要求我有一个 AudioContext。但是,在我的脚本开头声明 AudioContext 时,我在页面加载时在控制台中收到一条错误消息,显示“ReferenceError:AudioContext 未定义”。像其他人一样遇到过这样的 AudioContext 问题吗?我已经发布了我的 JS 片段和包含所有内容的 HTML。提前致谢!

JS:

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

$(document).ready(function(){
    // recording stuff
});
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Lew*_*ick 7

将其放在脚本的开头(它也在main.jsMatt Diamond 的示例中):

window.AudioContext = window.AudioContext || window.webkitAudioContext;
Run Code Online (Sandbox Code Playgroud)

这将加载正确的 AudioContext,与您的浏览器无关。


小智 0

您的代码在 Chrome29 中运行良好,您的浏览器支持 Web Audio API 吗?(http://caniuse.com/audio-api

我收到一个“ReferenceError:AudioContext未定义”错误,而AudioContext()不是像webkitAudioContext()您一样使用AudioContext Monkey Patch之前。确定它已加载并且不是缓存/硬刷新问题?