我在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.
为什么需要更多的论点?谁能帮我用JavaScript做这个吗?
谢谢。
getUserMedia的API需要3个参数。
让约束作为方法的第一个参数。
令successCallback为方法的第二个参数指示的回调。
令errorCallback为方法的第三个参数指示的回调。
因此,您需要做的就是添加第3个参数,这是一个错误回调。这样的事情会起作用。
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")});
};
您可以看一下这段代码,其功能类似,以供参考。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句