gpt4 book ai didi

javascript - 变量为空(仅在 Mozilla 中)

转载 作者:行者123 更新时间:2023-11-30 12:11:44 25 4
gpt4 key购买 nike

这里是代码的简历:

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

在 chrome 中我的代码工作得很好。在 Mozilla 中,我总是收到“存储数据未定义”的错误。知道为什么吗?因为我将 store_data 声明为全局,并且当 getusermediasucess 时,该值已更改。

最佳答案

不知道什么叫success功能,很难准确地说,但我很确定你想要你的 client.on('open')监听器取决于运行的成功函数。

我不知道它会如何影响其余省略的代码,但我只会连接 BinaryClient当成功函数运行并且你确定你有 store_data 时定义。

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

将所有代码移到成功函数中可能会奏效,但并不优雅。一旦流程正常运行,我建议重构代码,将每个逻辑位拆分为自己的函数。

关于javascript - 变量为空(仅在 Mozilla 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33573034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com