I am getting audio stream from getUserMeda and then convert it into a blob or buffer and send it to server as audio is comming I am using socket.io to emit it to server how can i convert audio mediastream into buffer?
Following is the code that i have written yet
navigator.getUserMedia({audio: true, video: false}, function(stream) {
webcamstream = stream;
var media = stream.getAudioTracks();
socket.emit("sendaudio", media);
},
function(e){
console.log(e);
}
});
How to convert stream into buffer and emit it to node.js server as stream comes from getusermedia function?
Per @MuazKhan's comment, use MediaRecorder (in Firefox, eventually will be in Chrome) or RecordRTC/etc to capture the data into blobs. Then you can export it via one of several methods to the server for distribution: WebSockets, WebRTC DataChannels, etc. Note that these are NOT guaranteed to transfer the data in realtime, and also MediaRecorder does not yet have bitrate controls. If transmission is delayed, data may build up locally.
If realtime (re)transmission is important, strongly consider using instead a PeerConnection to a server (per @Robert's comment) and then transform it there into a stream. (How that is done will depend on the server, but you should have encoded Opus data to either repackage or decode and re-encode.) While re-encoding is generally not good, in this case you would do best to decode through NetEq (webrtc.org stack's jitter-buffer and PacketLossConcealment code) and get a clean realtime audio stream to re-encode for streaming, with loss and jitter dealt with.
mediaRecorder = new MediaRecorder(stream);//Cria um elemento para gavar a Stream
let chunks = [];//Cria uma matriz para receber as parte.
mediaRecorder.ondataavailable = data =>
{
chunks.push(data.data)//Vai adicionando as partes na matriz
}
mediaRecorder.onstop = () => {//Quando ativar a função parar a gravação
//Cria o BLOB com as partes acionadas na Matriz
const blob = new Blob(chunks, { type: 'audio/wav' });
}
//Voce pode ainda criar um leitor
var reader = new FileReader();
//Passa o BLOB como parametro
reader.readAsText(blob);
//Pode visualizar os dados gerados em texto
alert(reader.result);
//Pode passar o dados para uma variável
var enviar_dados = reader.result;
//Pode passa via AJAX e ou JQUERY para o servidor, salvar no banco de dados...
PS-> O Type pode ser
//const blob = new Blob(chunks, { type: 'audio/ogg; code=opus' });
//const blob = new Blob(chunks, { type: 'application/octet-binary' });
//const blob = new Blob(chunks, { type: 'text/plain' });
//const blob = new Blob(chunks, { type: 'text/html' });
.......
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With