HTML5麦克风捕获在Firefox中停止5秒后停止

时间:2014-04-04 10:38:48

标签: audio web-audio getusermedia

我正在使用getUserMedia()函数从麦克风捕获音频输入,在chrome中运行良好,但在5秒后firefox声音消失。如果我再次发送麦克风请求(没有重新加载页面)同样的事情发生。以下是代码(我使用http://updates.html5rocks.com/2012/09/Live-Web-Audio-Input-Enabled作为指导):

//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);
});

有什么想法吗?


EDIT / UPDATE

谢谢你,csch,供参考。 Karoun Kasraie的解决方法有效!

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);

1 个答案:

答案 0 :(得分:19)

这是Firefox中的一个错误,可以在这里找到:

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

还有一种解决方法:

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