I want to record audio in JavaScript at a 16khz sampling rate in real-time. I have the following code:
navigator.getUserMedia(
{
"audio": {
"mandatory": {
"googEchoCancellation": "false",
"googAutoGainControl": "false",
"googNoiseSuppression": "false",
"googHighpassFilter": "false"
},
"optional": {
"sampleRate": 16000
}
},
}, gotStream, function(e) {
console.log(e);
});
But this throws the error:
index.html:1026 Uncaught TypeError: Failed to execute 'webkitGetUserMedia' on 'Navigator': The value provided is neither an array, nor does it have indexed properties.
How can I record audio at 16khz in JavaScript in real-time?
Several problems:
optional
(now advanced
) takes an array, e.g. optional: [{ sampleRate: 16000 }]
sampleRate
is not yet implemented in any browser AFAIK.So it won't work, at least not yet. In the future, use spec syntax:
navigator.mediaDevices.getUserMedia({
audio: {
echoCancellation: {exact: false},
sampleRate: 16000,
}
})
.then(gotStream)
.catch(e => console.log(e));