Record Audio Stream from getUserMedia
Asked Answered
S

5

13

In recent days, I tried to use javascript to record audio stream. I found that there is no example code which works.

Is there any browser supporting?

Here is my code

navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
                         navigator.mozGetUserMedia || navigator.msGetUserMedia; 

navigator.getUserMedia({ audio: true }, gotStream, null);
function gotStream(stream) {

        msgStream = stream;        
        msgStreamRecorder = stream.record(); // no method record :(
}
Suburbia answered 16/8, 2012 at 1:32 Comment(0)
W
2

You could check this site: https://webaudiodemos.appspot.com/AudioRecorder/index.html

It stores the audio into a file (.wav) on the client side.

Wally answered 22/8, 2013 at 6:58 Comment(2)
Note that link-only answers are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference.Harpp
The audio cannot be played using windows media player if I record and download from the link above. If I use windows media player and record audio using that thing then it's broken audio can be played if I download media player classic. Not even vlc can play that audioPublicness
S
35

getUserMedia gives you access to the device, but it is up to you to record the audio. To do that, you'll want to 'listen' to the device, building a buffer of the data. Then when you stop listening to the device, you can format that data as a WAV file (or any other format). Once formatted you can upload it to your server, S3, or play it directly in the browser.

To listen to the data in a way that is useful for building your buffer, you will need a ScriptProcessorNode. A ScriptProcessorNode basically sits between the input (microphone) and the output (speakers), and gives you a chance to manipulate the audio data as it streams. Unfortunately the implementation is not straightforward.

You'll need:

  • getUserMedia to access the device
  • AudioContext to create a MediaStreamAudioSourceNode and a ScriptProcessorNode
  • MediaStreamAudioSourceNode to represent the audio stream
  • ScriptProcessorNode to get access to the streaming audio data via an onaudioprocessevent. The event exposes the channel data that you'll build your buffer with.

Putting it all together:

navigator.getUserMedia({audio: true},
  function(stream) {
    // create the MediaStreamAudioSourceNode
    var context = new AudioContext();
    var source = context.createMediaStreamSource(stream);
    var recLength = 0,
      recBuffersL = [],
      recBuffersR = [];

    // create a ScriptProcessorNode
    if(!context.createScriptProcessor){
       node = context.createJavaScriptNode(4096, 2, 2);
    } else {
       node = context.createScriptProcessor(4096, 2, 2);
    }

    // listen to the audio data, and record into the buffer
    node.onaudioprocess = function(e){
      recBuffersL.push(e.inputBuffer.getChannelData(0));
      recBuffersR.push(e.inputBuffer.getChannelData(1));
      recLength += e.inputBuffer.getChannelData(0).length;
    }

    // connect the ScriptProcessorNode with the input audio
    source.connect(node);
    // if the ScriptProcessorNode is not connected to an output the "onaudioprocess" event is not triggered in chrome
    node.connect(context.destination);
  },
  function(e) {
    // do something about errors
});

Rather than building all of this yourself I suggest you use the AudioRecorder code, which is awesome. It also handles writing the buffer to a WAV file. Here is a demo.

Here's another great resource.

Setup answered 8/10, 2013 at 2:34 Comment(1)
ScriptProcessorNode is now deprecated - developer.mozilla.org/en-US/docs/Web/API/ScriptProcessorNodeDiscouragement
W
7

for browsers that support MediaRecorder API, use it.

for older browsers that does not support MediaRecorder API, there are three ways to do it

  1. as wav
  2. as mp3
  3. as opus packets (can get output as wav, mp3 or ogg)
Waterlogged answered 25/3, 2015 at 5:0 Comment(0)
W
2

You could check this site: https://webaudiodemos.appspot.com/AudioRecorder/index.html

It stores the audio into a file (.wav) on the client side.

Wally answered 22/8, 2013 at 6:58 Comment(2)
Note that link-only answers are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference.Harpp
The audio cannot be played using windows media player if I record and download from the link above. If I use windows media player and record audio using that thing then it's broken audio can be played if I download media player classic. Not even vlc can play that audioPublicness
A
1

There is a bug that currently does not allow audio only. Please see http://code.google.com/p/chromium/issues/detail?id=112367

Abfarad answered 30/8, 2012 at 8:46 Comment(0)
W
-2

Currently, this is not possible without sending the data over to the server side. However, this would soon become possible in the browser if they start supporting the MediaRecorder working draft.

Weinstock answered 16/3, 2013 at 17:53 Comment(1)
Here is the bug tracker for this Feature linkWeinstock

© 2022 - 2024 — McMap. All rights reserved.