0

I want to access the microphone input with navigator.getUserMedia(), but am unsure of how to proceed.

I can start the audio just fine, but I have no idea what to do with it after that.

if (!navigator.getUserMedia) {
    navigator.getUserMedia = navigator.getUserMedia 
                           || navigator.webkitGetUserMedia 
                           || navigator.mozGetUserMedia 
                           || navigator.msGetUserMedia;    
}

if (navigator.getUserMedia) {
    navigator.getUserMedia({audio: true}, function (e) {
        // what goes here?
    }, function (e) {
        alert('Error capturing audio.');
    });
} else {
    alert('getUserMedia not supported in this browser.');
}

I would like to access it as a stream. I don't even need stereo, just a way to get the data.

EDIT: I want to send the data back to the server using websockets, to create a sort of intercom system. Here, i need to be able to access a simple audio stream, stopping and starting it on certain events.

Scimonster
  • 30,695
  • 8
  • 67
  • 84
  • 1
    https://github.com/mattdiamond/Recorderjs – epascarello Jan 21 '14 at 19:50
  • 1
    I'm no expert on this, but I've been reading about the Web Real Time Communication project. http://www.webrtc.org/ and I found this hopefully helpful blog post: http://www.html5rocks.com/en/tutorials/getusermedia/intro/ – Harvey A. Ramer Jan 21 '14 at 19:52
  • @HarveyA.Ramer I saw that html5rocks post, but it talked mostly about video. – Scimonster Jan 21 '14 at 19:53
  • It would be helpful to know exactly what you want to do. Are you trying to add effects? Record the audio? Analyze it? – Kevin Ennis Jan 21 '14 at 23:02
  • 1
    @KevinEnnis I want to send it back to the server using websockets, to create a sort of intercom. – Scimonster Jan 22 '14 at 07:24
  • see answer here: https://stackoverflow.com/questions/27846392/access-microphone-from-a-browser-javascript – Z80 Apr 30 '20 at 14:18

1 Answers1

5

If you mean you want access to the raw samples of the ongoing audio stream - use a ScriptProcessorNode in Web Audio (http://webaudio.github.io/web-audio-api/#ScriptProcessorNode). RecordJS, mentioned above, will help show you how to do this.

cwilso
  • 12,493
  • 23
  • 31