麦克风到扬声器流(JS)

时间:2014-06-21 10:08:26

标签: javascript html5 html5-audio microphone web-audio

我一直在玩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. 

为什么需要更多参数? 有人可以帮我解决这个问题吗?

感谢。

1 个答案:

答案 0 :(得分:7)

getUserMedia的API需要3个参数。

  

让约束成为方法的第一个参数。

     

让successCallback成为方法的第二个参数所指示的回调。

     

让errorCallback成为方法的第三个参数所指示的回调。

请参阅mediacapture-stream spec

所以你需要做的就是添加一个第三个参数,这是一个错误回调。这样的事情会起作用。

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")});
};

您可以查看this piece of code which does something similar以供参考。

相关问题