Ragy Morkos
Ragy Morkos

Reputation: 83

Setting sampling frequency in JavaScript's navigator.mediaDevices.getUserMedia

I am currently trying to set the sampling frequency inside navigator.mediaDevices.getUserMedia. Here is a code snippet:

navigator.mediaDevices.getUserMedia({audio: {"sampleRate": {"exact": 8000}}, video: false}).then(handleSuccess).catch(e => console.log(e));

var handleSuccess = function(stream)
{
    mediaRecorder = new MediaRecorder(stream);
    mediaRecorder.start(1000); // the argument here is the duration of each audio chunk in milliseconds
    mediaRecorder.addEventListener
    ("dataavailable", event => 
        {
            blob = event.data;
            console.log(blob)
        }
    );
}

I don't believe setting sampleRate in getUserMedia is actually working because when I check the blob size with different sampleRate values, it never changes. Each blob is always within the range of 6000 to 7000.

Upvotes: 1

Views: 2342

Answers (1)

Ragy Morkos
Ragy Morkos

Reputation: 83

I found out what was wrong. The sampleRate constraint is not supported on Firefox as of October 6th, 2018. It is supposed to be supported on Chrome, but it is not functional, and I have filed a bug report in for Chromium: https://bugs.chromium.org/p/chromium/issues/detail?id=889851

If you need to check it out for yourself, Mozilla has done a very good demo for supported media constraints: https://mdn.mozillademos.org/en-US/docs/Web/API/Media_Streams_API/Constraints$samples/Example_Constraint_exerciser?revision=1411870

You will see at the top the supported audio constraints. If you enter the link on Firefox, you will see that sampleRate isn't listed there. On Chrome, it is listed, but if you enter {"sampleRate": 8000} inside the "Requested audio constraints" entry box and click on "Apply Constraints", you should see in the "Actual audio settings" box that the sample rate isn't affected.

Upvotes: 1

Related Questions