我正在使用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); });
有任何想法吗?
编辑/ 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);