Javascript 变量为null(仅在Mozilla中)
以下是代码的简历:Javascript 变量为null(仅在Mozilla中),javascript,mozilla,Javascript,Mozilla,以下是代码的简历: var client = new BinaryClient('ws://localhost:9001'); var context = null; var store_data = null; //(.....) if (!navigator.getUserMedia) navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.
var client = new BinaryClient('ws://localhost:9001');
var context = null;
var store_data = null;
//(.....)
if (!navigator.getUserMedia)
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia;
if (navigator.getUserMedia) {
navigator.getUserMedia({audio:true}, success, function(e) {
alert('Error capturing audio.');
});
} else alert('getUserMedia not supported in this browser.');
function success(e) {
audioContext = window.AudioContext || window.webkitAudioContext;
context = new audioContext();
audioInput = context.createMediaStreamSource(e);
var bufferSize = 2048;
store_data = context.createScriptProcessor(bufferSize, 1, 1);
//(...)
}
//(....)
client.on('open', function() {
console.log("createStream");
Stream = client.createStream(command_list);
var recording = false;
window.startRecording = function() {
document.getElementById("startbutton").disabled = true;
document.getElementById("stopbutton").disabled = false;
recording = true;
window.Stream.resume();
}
window.stopRecording = function() {
document.getElementById("startbutton").disabled = false;
document.getElementById("stopbutton").disabled = true;
recording = false
//window.Stream.end();
window.Stream.pause();
}
store_data.onaudioprocess = function(e){ //<---line of the error
if(!recording) return;
console.log ('recording');
var left = e.inputBuffer.getChannelData(0);
window.Stream.write(convertoFloat32ToInt16(left));
}
//(..events generated from server..)
var-client=newbinaryclient('ws://localhost:9001');
var-context=null;
var store_data=null;
//(.....)
如果(!navigator.getUserMedia)
navigator.getUserMedia=navigator.getUserMedia | | navigator.webkitGetUserMedia||
navigator.mozGetUserMedia | | navigator.msGetUserMedia;
if(navigator.getUserMedia){
getUserMedia({audio:true},成功,函数(e){
警报(“捕获音频时出错”);
});
}else警报(“此浏览器不支持getUserMedia”);
功能成功(e){
audioContext=window.audioContext | | window.webkitAudioContext;
上下文=新的audioContext();
audioInput=context.createMediaStreamSource(e);
var bufferSize=2048;
store_data=context.createScriptProcessor(bufferSize,1,1);
//(...)
}
//(....)
client.on('open',function()){
console.log(“createStream”);
Stream=client.createStream(命令列表);
var记录=错误;
window.startRecording=函数(){
document.getElementById(“startbutton”).disabled=true;
document.getElementById(“stopbutton”).disabled=false;
记录=真;
window.Stream.resume();
}
window.stopRecording=函数(){
document.getElementById(“startbutton”).disabled=false;
document.getElementById(“stopbutton”).disabled=true;
录音=假
//window.Stream.end();
window.Stream.pause();
}
store_data.onaudioprocess=function(e){/如果不知道是什么调用了success
函数,很难说清楚,但我相当肯定您希望客户端.on('open')
侦听器取决于函数运行的成功
我不知道它将如何影响其余省略的代码,但我只会在成功函数运行并且您确定已定义了存储数据时连接二进制客户端
function success() {
var client = new BinaryClient('ws://localhost:9001');
var context = null;
var store_data = null;
// do the original success code here
// now create that listener.
client.on('open', function() {
// do original code here
});
}
// you probably have a line of code that looks like this
navigator.getUserMedia({}, success);
将所有代码移到success函数中可能会起作用,但这并不优雅。一旦流程正常运行,我建议重构代码,将每个逻辑位分解为自己的函数。是的,这是一场竞赛。您的代码必须等到getUserMedia成功并触发open
承诺是解决这一问题的好方法:
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
这将给用户时间在麦克风权限提示中选择“允许”(与Chrome不同,Firefox每次都会向用户请求权限,除非用户选择“始终允许”)
这是一个在Mozilla上使用Chrome浏览器的BinaryJS流媒体的解决方案。多亏了@jib和@kaido,你从哪里得到了这个错误?可能是a。听起来在Mozilla中,BinaryClient在你调用success
函数之前打开了。这个函数是从哪里调用的?@Oriol I aleady编辑并标记了navigator.getUserMedia
已弃用,FF仅保留navigator.mozGetUserMedia
以实现向后兼容性。您应该使用navigator.mediaDevices.getUserMedia()
API及其Promissions系统。要将流连接到脚本处理器,您应该执行以下操作:navigator.mediaDevices.getUserMedia({audio:true})。然后(函数(流){context=new AudioContext();audioInput=context.createMediaStreamSource(流);var bufferSize=2048;store_data=context.createScriptProcessor(bufferSize,1,1);store_data.onaudioprocess=function(e){/success
如果浏览器与getusermedia
兼容,则会调用它。您可以添加负责调用它的代码吗?好的,我在示例中添加了一些代码,向您展示该模式的工作原理。要点是,在确保运行success
之前,您无法连接BinaryClient(因为它取决于该函数中的数据)。为了确保它在成功之前不会连接,您必须在成功期间连接它。此解决方案会给我以下错误:未捕获引用错误:开始录制未定义
,当我单击按钮时,此错误指向:类型错误:无法在“AudioNode”上执行“connect”:
我认为问题出在哪里是我没有正确连接音频节点。@Jib是的,我只是拿一把锯子到你的示例中,来解释一般的想法。你可能想重构它,以便在用户授予麦克风权限之前对任何录制按钮进行重影,这样他们就不会按错误的顺序单击它们。我已经解决了问题。我会给出解决方案。谢谢大家起重臂
var client = new BinaryClient('ws://localhost:9001');
var context = null;
var store_data = null;
//(.....)
var haveStoreData = navigator.mediaDevices.getUserMedia({audio:true})
.then(function(stream) {
audioContext = window.AudioContext || window.webkitAudioContext;
context = new audioContext();
audioInput = context.createMediaStreamSource(stream);
var bufferSize = 2048;
return context.createScriptProcessor(bufferSize, 1, 1);
});
//(....)
client.on('open', function() {
console.log("opened");
haveStoreData.then(function(store_data) {
console.log("createStream");
Stream = client.createStream(command_list);
var recording = false;
window.startRecording = function() {
document.getElementById("startbutton").disabled = true;
document.getElementById("stopbutton").disabled = false;
recording = true;
window.Stream.resume();
};
window.stopRecording = function() {
document.getElementById("startbutton").disabled = false;
document.getElementById("stopbutton").disabled = true;
recording = false;
//window.Stream.end();
window.Stream.pause();
};
store_data.onaudioprocess = function(e){
if(!recording) return;
console.log ('recording');
var left = e.inputBuffer.getChannelData(0);
window.Stream.write(convertoFloat32ToInt16(left));
};
//(..events generated from server..)
})
.catch(function(e) { console.error(e); });
});
var client = new BinaryClient('ws://193.136.94.233:9001');
var context = null;
var gain = null;
var store_data = null;
//(.....)
navigator.mediaDevices.getUserMedia({audio:true}) .then( function(stream){
context = new AudioContext();
audioInput = context.createMediaStreamSource(stream);
var bufferSize = 4096;
store_data = context.createScriptProcessor(bufferSize, 1, 1);
biquadFilter = context.createBiquadFilter();
biquadFilter.type = "lowpass";
biquadFilter.frequency.value = 11500;
biquadFilter.Q.value = 3;
ganho = context.createGain();
ganho.gain.value=0.5;
//audioInput.connect(ganho);//compresso
//ganho.connect(recorder);
//recorder.connect(context.destination);
audioInput.connect(biquadFilter);
biquadFilter.connect(ganho);
ganho.connect(store_data);
store_data.connect(context.destination);
store_data.onaudioprocess = function(e){
if(!recording){
//console.log("nada faz nada desta vida")
return;
}
console.log ('recording');
var left = e.inputBuffer.getChannelData(0);
Stream.write(convertoFloat32ToInt16(left));
}
//audioInput.connect(store_data);
} ) .catch( function(e){ console.log(e) } );
//(...)
client.on('open', function() {
console.log("opened connection");
//haveStoreData.then(function(store_data) {
Stream = client.createStream(command_list);
//recording = false;
//(........)
);
//Other function