我一直在研究 HTML5,但我无法完成以下任务。 javascript 必须请求访问麦克风的权限,然后它必须将麦克风输入流式传输到计算机扬声器。 这是我的 javascript:
navigator.getUserMedia = ( navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||navigator.msGetUserMedia);
var aCtx;
var analyser;
var microphone;
if (navigator.getUserMedia) {
navigator.getUserMedia({audio: true}, function(stream) {
aCtx = new webkitAudioContext();
analyser = aCtx.createAnalyser();
microphone = aCtx.createMediaStreamSource(stream);
microphone.connect(analyser);
analyser.connect(aCtx.destination);
});
};
但是 Chrome(和 Opera)说
Uncaught TypeError: Failed to execute 'webkitGetUserMedia' on 'Navigator': 3 arguments required, but only 2 present.
为什么它需要更多参数? 任何人都可以帮我解决这个问题吗?
谢谢。
最佳答案
getUserMedia 的 API 需要 3 个参数。
Let constraints be the method's first argument.
Let successCallback be the callback indicated by the method's second argument.
Let errorCallback be the callback indicated by the method's third argument.
所以你需要做的就是添加第三个参数,它是一个错误回调。这样的事情会起作用。
navigator.getUserMedia = ( navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||navigator.msGetUserMedia);
var aCtx;
var analyser;
var microphone;
if (navigator.getUserMedia) {
navigator.getUserMedia({audio: true}, function(stream) {
aCtx = new webkitAudioContext();
analyser = aCtx.createAnalyser();
microphone = aCtx.createMediaStreamSource(stream);
microphone.connect(analyser);
analyser.connect(aCtx.destination);
}, function (){console.warn("Error getting audio stream from getUserMedia")});
};
关于javascript - 麦克风到扬声器流 (JS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24340531/