在Firefox中修改音频上下文中的采样率无效

时间:2019-09-18 16:31:42

标签: javascript browser web-audio-api

我们想直接以8 KHz的采样率录制网页音频,这与默认采样率(44,1或48 kHz)不同。

以下代码在Chrome中有效,但在Firefox(版本69.0)中无效。

使用Firefox时,调用createMediaStreamSource时会出错(如果sampleRate:48000不会发生)

input = audioContext.createMediaStreamSource(stream);

我们是否不允许更改Firefox的sampleRate值,但是Chrome接受了该值?

function startRecording() {
    console.log("recordButton clicked");

    /*
        Simple constraints object, for more advanced audio features see
        https://addpipe.com/blog/audio-constraints-getusermedia/
    */

    var constraints = { audio: true, video:false }

    /*
        Disable the record button until we get a success or fail from getUserMedia() 
    */

    recordButton.disabled = true;
    stopButton.disabled = false;
    pauseButton.disabled = false

    /*
        We're using the standard promise based getUserMedia() 
        https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
    */

    navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {
        console.log("getUserMedia() success, stream created, initializing Recorder.js ...");

        /*
            create an audio context after getUserMedia is called
            sampleRate might change after getUserMedia is called, like it does on macOS when recording through AirPods
            the sampleRate defaults to the one set in your OS for your playback device

        */
        window.AudioContext = window.AudioContext||window.webkitAudioContext;
        try {

        //audioContext = new AudioContext();
        audioContext = new AudioContext({
            latencyHint: "interactive",
            sampleRate: 8000
        });     
        }
        catch(e) {
            alert('Error sampleRate '); //+audioContext.sampleRate);
        }

        //audioContext = new AudioContext();
        /*audioContext = new AudioContext({
            latencyHint: "playback",
            sampleRate: 8000
        });*/

        //update the format 
        document.getElementById("formats").innerHTML="Format: 1 channel pcm @ "+audioContext.sampleRate/1000+"kHz"

        /*  assign to gumStream for later use  */
        gumStream = stream;

    alert('Paso 1'); 
        /* use the stream */
        input = audioContext.createMediaStreamSource(stream);
    alert('Paso 2'); 

        /* 
            Create the Recorder object and configure to record mono sound (1 channel)
            Recording 2 channels  will double the file size
        */
        rec = new Recorder(input,{numChannels:1})
    alert('Paso 3'); 

        //start the recording process
        rec.record()
    alert('Paso 4'); 

        console.log("Recording started");

    }).catch(function(err) {
        //enable the record button if getUserMedia() fails
        alert('Error getUserMedia ');
        recordButton.disabled = false;
        stopButton.disabled = true;
        pauseButton.disabled = true
    });
}```

0 个答案:

没有答案