Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
166
rated 0 times [  171] [ 5]  / answers: 1 / hits: 7415  / 10 Years ago, sat, june 21, 2014, 12:00:00

I've been playing around a lot with HTML5, but I can't get the following done.
The javascript has to ask permission to access the microphone, and then it has to stream the microphone input to the computer speakers.
This is the javascript I had:



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


But Chrome (and Opera) say



Uncaught TypeError: Failed to execute 'webkitGetUserMedia' on 'Navigator': 3 arguments required, but only 2 present. 


Why would it need more arguments?
Can anyone please help me with the javascript for this?



Thanks.


More From » html

 Answers
20

The API for getUserMedia requires 3 arguments.



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.



Refer to the mediacapture-stream spec


So all you need to do is add a 3rd argument which is an error callback. Something like this would work.


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

You can look at this piece of code which does something similar for reference.


[#44411] Thursday, June 19, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
keric

Total Points: 572
Total Questions: 93
Total Answers: 97

Location: Cyprus
Member since Mon, Oct 24, 2022
2 Years ago
;