Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

send audio with socket io node js

var constraints = { audio: true };
navigator.mediaDevices.getUserMedia(constraints).then(function(mediaStream) {
    var mediaRecorder = new MediaRecorder(mediaStream);
    mediaRecorder.onstart = function(e) {
        this.chunks = [];
    };
    mediaRecorder.ondataavailable = function(e) {
        this.chunks.push(e.data);
    };
    mediaRecorder.onstop = function(e) {
        var blob = new Blob(this.chunks, { 'type' : 'audio/ogg; codecs=opus' });
        socket.emit('radio', blob);
    };

    // Start recording
    mediaRecorder.start();

    // Stop recording after 5 seconds and broadcast it to server
    setTimeout(function() {
        mediaRecorder.stop()
    }, 5000);
});

// When the client receives a voice message it will play the sound
socket.on('voice', function(arrayBuffer) {
    var blob = new Blob([arrayBuffer], { 'type' : 'audio/ogg; codecs=opus' });
    var audio = document.createElement('audio');
    audio.src = window.URL.createObjectURL(blob);
    audio.play();
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: copia independiente array javascript 
Javascript :: an arrow function 
Javascript :: npm update package.json version field by code 
Javascript :: string splice 
Javascript :: shuffle array in javascript 
Javascript :: Setting darkmode using Tailwind 
Javascript :: date picker type react 
Javascript :: momentum 
Javascript :: threejs perspectivecamera 
Javascript :: Accessing user input through js 
Javascript :: lodash remove not in array 
Javascript :: react-native-safe-area-context 
Javascript :: hoisting in javascript 
Javascript :: aos animation 
Javascript :: select jquery display none 
Javascript :: node fetch 
Javascript :: find match in array object js 
Javascript :: angularjs 
Javascript :: render html in js.erb 
Javascript :: simple express server 
Javascript :: shadow react native generator 
Javascript :: checked unchecked through js 
Javascript :: how can i do metaname csrf token attrcontent in vanilla javascrip 
Javascript :: datatable dropdown toggle not working 
Javascript :: how to use text onclick to display images in javascript 
Javascript :: append item in treeview vuetify 
Javascript :: autocomplete required material ui 
Javascript :: file upload nest 
Javascript :: switch case statement in javascript 
Javascript :: angular 11 export excel with customize header 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =