Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send audio over WebSocket

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);
  });
});
like image 858
jihuuNI Avatar asked Sep 06 '19 09:09

jihuuNI


People also ask

Can WebSocket stream video?

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.

Can we send file through 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).

Does twilio use WebSockets?

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.


1 Answers

Binary data are by far more effective than JSON, I do not recomend to pick that way

like image 193
Mosè Raguzzini Avatar answered Oct 23 '22 19:10

Mosè Raguzzini