javascript AudioNodes不能更改采样率

时间:2019-12-24 15:45:09

标签: javascript web-audio-api

我不知道为什么会出现此错误。或我错过的东西。

这是我的代码:

    function mediaDeviceInit(deviceId) {
        // this for fast codding see w3c spec for audio
        navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
        console.log('IpCodec : Get user permissions for Media Access.');

        let audioConstraints = {};
        // check for default value
        if (deviceId) {
            audioConstraints = {
                audio: { deviceId: deviceId, echoCancellation: false, sampleRate: defaultSampleRate }, video: false
            };
        } else {
            audioConstraints = { audio: { echoCancellation: false, sampleRate: defaultSampleRate }, video: false };
        }

        if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
            navigator.mediaDevices.getUserMedia(audioConstraints)
                .then(function (stream) {
                    //console.log(navigator.mediaDevices.getSupportedConstraints());
                    userMediaSuccess(stream);
                })
                .catch(function (error) {
                    userMediaError(error);
                });
        } else {
            console.log('IpCodec : Browser Unsuported to getUserMedia.')
        }

        // enumerate all input audio device
        function enumAudioInput() {
            // somecode
        }

        // callback on success
        function userMediaSuccess(stream) {
            let audioSrc = audioMixer.audioContext.createMediaStreamSource(stream); // --> error here
            // some init code
            console.log('IpCodec : Media permission granted by user.');
            if (!deviceId) {
                enumAudioInput();
            }
        }

        // callback on error
        function userMediaError(error) {
            console.log('IpCodec' + error);
        }
    }

,错误如下: 当前不支持以不同的采样率从AudioContexts连接AudioNode。

以及拥有AudioContext的这部分audioMixer类:

class AudioMixer {
  constructor(type, sRate, latency) {
    this.audioContext;
    // parse all browser compability
    window.AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext;
    console.log('IpCodec : Initialize audio mixer success.');
    if (window.AudioContext) {
        this.audioContext = new window.AudioContext({ sampleRate: sRate, latencyHint: latency });
        //console.log(this.audioContext);
    } else {}
  }
}

0 个答案:

没有答案