Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 Microphone capture stops after 5 seconds in Firefox

I'm capturing audio input from microphone with getUserMedia() function, works fine in chrome, but in firefox sound dies out after 5 seconds. If I send request for microphone again (without reloading the page) same thing happens. Here is the code (I used http://updates.html5rocks.com/2012/09/Live-Web-Audio-Input-Enabled as guidance):

//getting the function depending on browser

navigator.getMedia = ( navigator.getUserMedia ||
                       navigator.webkitGetUserMedia ||
                       navigator.mozGetUserMedia ||
                       navigator.msGetUserMedia);

// success callback when requesting audio input stream
function gotAudioStream(stream) {
    window.AudioContext = window.AudioContext || window.webkitAudioContext;
    var audioContext = new AudioContext();

    // Create an AudioNode from the stream.
    var mediaStreamSource = audioContext.createMediaStreamSource( stream );

    // Connect it to the destination to hear yourself (or any other node for processing!)
    mediaStreamSource.connect( audioContext.destination );
}


function gotError(err) {
      alert("An error occured! " + err);
}


//when button clicked, browser asks a permission to access microphone

jQuery("#sound_on").click(function()
{
    navigator.getMedia({audio: true},gotAudioStream,gotError);
});

Any ideas?


EDIT/UPDATE

Thank you, csch, for the reference. Workaround by Karoun Kasraie worked!

context = new AudioContext();

navigator.getUserMedia({ audio: true }, function(stream) {
  // the important thing is to save a reference to the MediaStreamAudioSourceNode
  // thus, *window*.source or any other object reference will do
  window.source = context.createMediaStreamSource(stream);
  source.connect(context.destination);
}, alert);
like image 232
Starwave Avatar asked Apr 04 '14 10:04

Starwave


1 Answers

It's a bug in Firefox, it can be found here:

https://bugzilla.mozilla.org/show_bug.cgi?id=934512

There's also a workaround:

context = new AudioContext();

navigator.getUserMedia({ audio: true }, function(stream) {
  // the important thing is to save a reference to the MediaStreamAudioSourceNode
  // thus, *window*.source or any other object reference will do
  window.source = context.createMediaStreamSource(stream);
  source.connect(context.destination);
}, alert);

source

like image 126
csch Avatar answered Sep 25 '22 01:09

csch