2016-12-16 77 views
1

當我使用npm start命令從命令提示符運行應用程序時,它運行良好。它從語音API返回結果。谷歌雲語音不在電子包中工作

我使用binaryServer和binaryclient將音頻傳輸到谷歌雲API。

當我爲電子應用程序創建包時,一切正常,但不會返回語音API的結果。

這裏是我的代碼snippe: 的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

在這裏拍攝黑暗(沒有太多熟悉binaryServer,現實可能是問題)。我還有點不清楚音頻流實際來自哪裏:

電子封裝了它自己的V8版本。運行npm install時,它將安裝(或即時編譯)安裝在本機(本地版本)上的V8版本的本機二進制文件。當你產生子進程時,它使用相同的本地版本。

但是,當你打包你的電子應用程序時,它會嘗試用Electron的V8版本產生進程,並且會有二進制不兼容。

簡而言之 [你的版本V8的]!= [電子版的V8引擎]

在潛在的解決方案