当我使用npm start命令从命令提示符运行应用程序时,它运行良好。它返回语音api的结果。
我正在使用binaryServer和binaryclient将音频流式传输到Google Cloud API。
当我为电子应用创建包时,一切正常,但它不会从语音api返回结果。
以下是我的代码片段: 的package.json
{
"name": "test",
"version": "1.0.0",
"description": "test Web Server",
"main": "main.js",
"scripts": {
"start": "electron main.js"
},
"devDependencies": {
"electron": "^1.4.12"
},
"dependencies": {
"binaryjs": "^0.2.1",
"connect": "^3.3.4",
"biased-opener": "^0.2.8",
"serve-static": "^1.9.1",
"uaparser": "^0.0.2",
"@google-cloud/speech" : "^0.5.0"
}
}
这是我的main.js
app.on('ready', function () {
load_app();
});
var workerProcess = child_process.spawn('node', __dirname + '/binaryServer.js');
workerProcess.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
workerProcess.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
workerProcess.on('close', function (code) {
console.log('child process exited with code ' + code);
});
processes.push(workerProcess);
function load_app () {
// Launches the browser window
mainWindow = new BrowserWindow({ width: 1080, height: 1920 });
// Load just launched server in browser window
mainWindow.loadURL("http://localhost:" + config.port);
if (config.devMode) {
mainWindow.webContents.openDevTools();
}
else {
mainWindow.setFullScreen(true);
}
}
这是我的二进制服务器
var binaryServer = require('binaryjs').BinaryServer,
https = require('http'),
connect = require('connect'),
serveStatic = require('serve-static');
var config = require('./config');
var server = connect()
.use(serveStatic(__dirname));
var speech = require('@google-cloud/speech')({
projectId: config.speech.projectId,
keyFilename: config.speech.keyFilename
});
httpServer = https.createServer(server);
httpServer.timeout = 0;
httpServer.listen(config.port);
var binarySer = binaryServer({ server: httpServer });
console.log("server pid" + process.pid);
binarySer.on('connection', function (client) {
console.log("new connection...");
client.on('stream', function (stream, meta) {
var options = {
config: {
encoding: 'LINEAR16',
sampleRate: meta.sampleRate,
languageCode: "en-IN"
},
singleUtterance: false,
interimResults: true,
verbose: true
};
// Create a recognize stream
const recognizeStream = speech.createRecognizeStream(options)
.on('error', console.error)
.on('data', function (data) { if (stream.writable && !stream.destroyed) stream.write(data) }); // send data to client
if (recognizeStream.writable && !recognizeStream.destroyed && stream.readable && !stream.destroyed)
stream.pipe(recognizeStream); // pipe audio to cloud speech
});
client.on('close', function () {
console.log("Connection Closed");
});
});
感谢您的帮助
答案 0 :(得分:0)
在黑暗中拍摄(不太熟悉binaryServer,实际上可能是问题)。我还有点不清楚音频流实际来自哪里:
Electron打包自己的V8版本。当您运行[~/dirB]$ npm install ../dirA
/home/tbeadle/dirB
`-- dirA@1.0.0
`-- vue@2.1.8
npm WARN enoent ENOENT: no such file or directory, open '/home/tbeadle/dirB/package.json'
npm WARN dirB No description
npm WARN dirB No repository field.
npm WARN dirB No README data
npm WARN dirB No license field.
时,它将安装(或动态编译)针对您机器上安装的V8版本(本地版本)的本机二进制文件。当您生成子进程时,它使用相同的本地版本。
但是,当您打包电子应用程序时,它会尝试使用Electron的V8版本生成该过程,并且会出现二进制不兼容问题。
简单地说 [你的V8版本]!= [电子版的V8]
关于潜在的解决方案