Search code examples
javascriptaudiodesktopmediarecordervoice-recording

how i can record desktop audio and microphone audio using javascript?


i have to a record audio for both desktop audio and micophone

currently i am using media recorder

const recFunction = async() => {
    try {
        const mediaDevices = await navigator.mediaDevices.getUserMedia(typeOfMedia)
        if (mediaDevices.active === true) {
            recStream = new MediaRecorder(mediaDevices, options);
            recStream.ondataavailable = e => {
                chunks.push(e.data);
                if (recStream.state == 'inactive') {
                    let blob = new Blob(chunks, { type: 'audio/wav' });
                    $('#hiddenAudio').val(URL.createObjectURL(blob));
                    $('#sendButton').trigger("click");  
                    document.getElementById("recordData").classList.remove("recordBoxHide")
                    // createAudioElement(URL.createObjectURL(blob))
                }
            }
            recStream.onstop = () => {
                chunks = []
            }
            recStream.start()
        }
    } catch (error) {
        if (error) console.log(error);
    }
}

but problem is that when I am recording desktop audio not recording while connecting earphones and if I disconnect earphones and recording again then desktop audio is recording successful >> that is my main problem please help me if any one has solution for this problem suggest to me some library or API if available in javascript


Solution

  • you can use recordrtc for audio and video recording

    import RecordRTC, { StereoAudioRecorder } from 'recordrtc';
    const startRecording = async (remote, setChunckCount, chunckCount, userCallSid) => {
        const ac = new AudioContext();
        let stream = await navigator.mediaDevices.getUserMedia({
            audio: {
                mandatory: {
                    echoCancellation: false,
                    googAutoGainControl: false,
                    googNoiseSuppression: false,
                    googHighpassFilter: false
                }
            }
        });
        let audioTracks = []
        audioTracks.push(stream.getAudioTracks()[0])
        audioTracks.push(remote.getAudioTracks()[0])
        const sources = audioTracks.map(t => ac.createMediaStreamSource(new MediaStream([t])));
        const dest = ac.createMediaStreamDestination();
        sources.forEach(s => s.connect(dest));
        let recorder = new RecordRTC(dest.stream, {
            type: 'audio',
            mimeType: 'audio/wav',
            recorderType: StereoAudioRecorder,
            numberOfAudioChannels: 1,
            disableLogs: process.env.REACT_APP_ENV === 'development' ? false : true,
            bufferSize: 16384,
            timeSlice: 4000,
            ondataavailable: (blob) => {
                saveRecordingChunck(blob, userCallSid, chunckCount, setChunckCount)
            },
        })
        recorder.startRecording();
        return recorder
    }
    

    `