简体   繁体   中英

How can I set this parameter in JavaScript getusermedia?

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:

  1. optional (now advanced ) takes an array, eg optional: [{ sampleRate: 16000 }]
  2. This constraints syntax is outdated and only works in Chrome.
  3. 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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM