Record audio and stream live

Asked

Viewed 3,245 times

9

I need to record audio and broadcast live, I found that with HTML5 and Javascript/jQuery is possible and that has plugins that can help me. I’m wearing one of them, the Mediastreamrecorder and I’m recording and broadcasting to myself live, only I can’t transmit to everyone, does anyone know how I do this transmission ? Probably using socket ? Where is the file being saved ? How to send the file in real time to the server ?

EDIT: How to send the blob in real time to the server ?

LINK IN JSFIDDLE: http://jsfiddle.net/C8WHd/2/

Code used (the plugin is on github at this link: Mediastreamrecorder):

<script src="//www.webrtc-experiment.com/MediaStreamRecorder.js"> </script>

<h1>
    Audio Recording using <a href="https://github.com/streamproc/MediaStreamRecorder"
                             target="_blank">MediaStreamRecorder</a>
</h1>
<div id="audios-container">
</div>   



 <script>
        var mediaConstraints = { audio: true };
        navigator.getUserMedia(mediaConstraints, onMediaSuccess, onMediaError);

        function onMediaSuccess(stream) {
            var audio = document.createElement('audio');
            audio = mergeProps(audio, {
                controls: true,
                src: URL.createObjectURL(stream)
            });
            audio.play();

            audiosContainer.appendChild(audio);
            audiosContainer.appendChild(document.createElement('hr'));

            var mediaRecorder = new MediaStreamRecorder(stream);
            mediaRecorder.mimeType = 'audio/ogg';
            mediaRecorder.ondataavailable = function(blob) {
                var a = document.createElement('a');
                a.target = '_blank';
                a.innerHTML = 'Open Recorded Audio No. ' + (index++);

                a.href = URL.createObjectURL(blob);

                audiosContainer.appendChild(a);
                audiosContainer.appendChild(document.createElement('hr'));
            };

            // get blob after each 5 second!
            mediaRecorder.start(5 * 1000);
        }

        function onMediaError(e) {
            console.error('media error', e);
        }

        var audiosContainer = document.getElementById('audios-container');
        var index = 1;
 </script
  • 1

    What’s the problem? Could you publish your code so we can analyze?

  • 1

    added code!

  • 3

    There is no file being recorded. It simply stores the audio bytes (and possibly transmits them) as it receives them. The point is, where’s your "everyone"? It is quite possible that you need a server to forward the audio bytes to the other clients, since only via Javascript client-side, it is not possible to create a socket where other external clients can connect. At the beginning of the project description there are links with examples to transmit: https://github.com/muaz-khan/WebRTC-Experiment/tree/master/RecordRTC#how-recordrtc-encodes-wavwebm

  • @carlosrafaelgn I use PHP, but I’ve never used sockets in php, I’ll search on, but I still don’t know how to send the file in real time to the server, if anyone has any answers...

  • So, @user3230262. There is no file to transmit... Just an array of bytes that should be transmitted as quickly as possible to the server. Take a look at the source code of this page here, which is an example of me drawing audio on the screen. These other two questions also address the general idea of this capture... http://answall.com/questions/11640/como-capturr-audio-do-micropone-usando-api-de-audio-do-html5 and http://answall.com/questions/14484/capturing-audio-pelo-microfone-via-js-ou-html

  • @carlosrafaelgn have any idea how I transmit this data ? examples!!!

  • The Ajax example of the answer to this question is a good start ;) http://answall.com/questions/14484/capturing-audio-pelo-microfone-via-js-ou-html

Show 2 more comments

2 answers

8


After much research I got results using the Webrtc-Experiment (https://github.com/muaz-khan/WebRTC-Experiment) and (http://www.rtcmulticonnection.org/docs/).

Server code: (http://jsfiddle.net/C8WHd/3/)

<script src="http://www.rtcmulticonnection.org/latest.js"></script>
<button id="audio">Audio stream</button>
<script>

var connection = new RTCMultiConnection('channel-654654-34434-12121');
connection.session = {
    data: true
};
connection.open();


document.querySelector('#audio').onclick = function() {
    connection.addStream({audio:true,oneway:true});
};
</script>

User code: (http://jsfiddle.net/C8WHd/4/)

<script src="http://www.rtcmulticonnection.org/latest.js"></script>
<script>

var connection = new RTCMultiConnection('channel-654654-34434-12121');
connection.session = {
    audio: true
};
connection.connect();

</script>
  • Thanks for complartilhar! I was needing this!

6

There is nowhere to run, when it comes to stream transmission customers have to connect to you or be you are the server that distributes (broadcast) the data to everyone who is connected to you!

In reality you open pure audio (raw audio) on your data input device (microphone or other source) and send to customers inside blocks with fixed size, in general 2048, 4096 on the other side each client receives the values of this vector and gives play encoding for some format (wav, mp3, etc)!

The javascript that presented only takes the data of the microphone in a pure way, I did not look with affection but these values must be in a vector short int or float32 of fixed size, only for you understand the process:

The values captured from the microphone generally can be in short in or float32 they are a vector of fixed size, 2048 or 4096, etc These values are passed to an Encoder that encodes this vector to the desired format, of possession of the encoded vector the player plays this vector. This process runs in a loop and is fast enough not to promote latency

  • 1

    ??? err.... What does that help? I did not understand... Well of course it is valid information, but I do not know how it helps in question...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.