I'm implementig getUserMedia()
to record audio messages. I want to record the audio and then pass it using websocket to another peer. How I can accomplish this?I've searched on SO and I've found only some interesting question, but nothing that can guide me to a working solution. I'm using Pusher API for the websocket part of my app.
Here is the code I'm testing.
$(document).on("click", ".audio-chat",function(){
console.log('clicked');
var channel = $('input[name="channelName"]').val();
navigator.mediaDevices.getUserMedia({
audio: true
})
.then(function(stream){
var mediaRecorder = new MediaRecorder(stream);
mediaRecorder.start();
console.log(mediaRecorder.state);
console.log("recorder started");
mediaRecorder.ondataavailable = function(e) {
chunks.push(e.data);
console.log(chunks);
}
setTimeout(function(){
mediaRecorder.stop();
console.log(mediaRecorder.state);
console.log("recorder stopped");
var blob = new Blob(chunks, {'type':'audio/webm; codecs=opus'});
//console.log(blob);
chunks = [];
const audioUrl = window.URL.createObjectURL(blob);
var data = {channel:channel,message:audioUrl,socketId:socketId}
$.post('api/message.php', data);
}, 10000);
});
});
Web Call Server can receive a video stream via multiple protocols: WebRTC, RTMP, RTMFP, SIP / RTP, RTSP and can deliver it to the iOS Safari browser via Websocket.
You can send raw binary data through the WebSocket. It's quite easy to manage. One option is to prepend a "magic byte" (an identifier that marks the message as non-JSON).
At Twilio, we use WebSockets to connect our SDKs to our backend in several of our products: Sync maintains and distributes a single source of state in the cloud, managed by application developers and disseminated to browsers and mobiles.
Binary data are by far more effective than JSON, I do not recomend to pick that way
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