0

I want to send a audio file to a server (in my case discord) easly as if it was comming from the microphone

I found this code at Send sound through microphone in javascript and modified it to try to fit my use case, but I still cannot get it to work.

navigator.mediaDevices.getUserMedia = () => {
    const audioContext = new AudioContext();

    return fetch('http://127.0.0.1:8000/enemey.ogg',{mode: 'no-cors'})
        .then((response) => response.arrayBuffer())
        .then((arrayBuffer) => audioContext.decodeAudioData(arrayBuffer))
        .then((audioBuffer) => {
            const audioBufferSourceNode = audioContext.createBufferSource();
            const mediaStreamAudioDestinationNode = audioContext.createMediaStreamDestination();

            audioBufferSourceNode.buffer = audioBuffer;
            // Maybe it makes sense to loop the buffer.
            audioBufferSourceNode.loop = true;

            audioBufferSourceNode.start();

            audioBufferSourceNode.connect(mediaStreamAudioDestinationNode);

            return mediaStreamAudioDestinationNode.stream;
        });
};

any Ideas? I cannot find a fix for this, and the error is

[AudioActionCreators] unknown getUserMedia error: EncodingError

by discord

(all of this is done with the console, not a external program)

  • The code you copied from the referenced SO post fetches an audio file and turns it into a stream. Is that what discord is expecting? Please be more specific about **what** you're to send, **where** it needs to go to and **how** it should be sent. – Emiel Zuurbier Jul 10 '22 at 10:45
  • @EmielZuurbier If I am correct, https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia states that is is in fact a stream, whitch I am using, it will also call the .then function as this all is async, whitch the documentation says it will, I am going to send a audio file through my microphone. Discord makes a call to this function requesting the audio stream, which I am overwriting with my own function and returning this, I am not sending the data, just retrieving different data and forwarding it. (sorry writing this quickly doing something else right now) – Davidawesome02 Jul 10 '22 at 16:28
  • Well, not exactly. You're not using the stream from `navigator.mediaDevices.getUserMedia`, although it creates a stream. You don't need that part as it request your device for your microphone and/or webcam for streaming. Fetching the file, decoding it to an `AudioBuffer` and transferring it into the `MediaStreamAudioDestinationNode` will do. I'm still missing the part where you send it to Discord. – Emiel Zuurbier Jul 10 '22 at 17:00
  • Discord has to get my microphone input somehow, and If im correct, discord gets it throught this function, as this function effects discord in this way: [AudioActionCreators] unknown getUserMedia error: EncodingError So, discord makes a call to this function, that is how you can get microphone input I dont understand how you think discord gets audio input whithout calling the function that gives it the stream so to clarify, discord calls this function as dose any voice chating website @EmielZuurbier – Davidawesome02 Jul 10 '22 at 17:09
  • My apologies. I only just now realized that you're overwriting the native `navigator.mediaDevices.getUserMedia` function. If Discord doesn't have a specific API for this case, then I don't know how to help you. Good luck. – Emiel Zuurbier Jul 10 '22 at 17:40

0 Answers0