How do I capture audio from the microphone using the HTML5 Audio API and play it at the same time?
How do I capture audio from the microphone using the HTML5 Audio API and play it at the same time?
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);
}
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