How to capture audio from microphone using HTML5 audio API?

8

How do I capture audio from the microphone using the HTML5 Audio API and play it at the same time?

    
asked by anonymous 03.04.2014 / 18:59

2 answers

3

Here's a short snippet of code that accomplishes this (tested successfully on Chrome 33 and Firefox 28):

var audioContext, microphoneStream;

function getUserMedia_Success(mediaStream) {
    microphoneStream = audioContext.createMediaStreamSource(mediaStream);
    //Conecta o microfone à saída
    microphoneStream.connect(audioContext.destination);
    return true;
}

function getUserMedia_Error(error) {
    alert("Erro ao obter acesso ao microfone: " + error);
    return true;
}

//Valida a capacidade do browser de capturar mídia
if (!navigator.getUserMedia) {
    navigator.getUserMedia = (navigator.webkitGetUserMedia ||
        navigator.mozGetUserMedia ||
        navigator.msGetUserMedia);
}
if (!navigator.getUserMedia) {
    alert("Aparentemente seu browser não possui a API necessária para capturar mídia :(");
    return;
}

//Tenta criar o contexto de áudio para capturar e reproduzir o áudio
audioContext = (window.AudioContext ? new AudioContext() : (window.webkitAudioContext ? new webkitAudioContext() : null));
if (!audioContext) {
    alert("Aparentemente seu browser não possui a API necessária para trabalhar com áudio! :(");
} else {
    //Tenta obter acesso ao microfone
    navigator.getUserMedia({ audio: true }, getUserMedia_Success, getUserMedia_Error);
}
    
25.04.2014 / 15:38
0

You can take a look at HTML Media Capture and its API .

Ericsson Labs have already accomplished this, see: here (Note: Note that the device element and APIs are not available in any browser, however, and the APIs may change before this happens.) But it gives you an idea of how it is done.

I'm currently working on something like this, see: here .

  

Source:    link

    
03.04.2014 / 20:11