2017-08-09 84 views
1

我正在浏览器或控制台中看到单词副本,但我没有看到诸如{'state': 'listening'}之类的消息。更重要的是,我没有看到如{"results": [{"alternatives": [{"transcript": "name the mayflower "}],"final": true}],"result_index": 0}的结果。IBM Watson语音到文本JavaScript SDK:如何获取消息?

我读了RecognizeStream documentation,并试图将此代码:

stream.on('message', function(message) { 
    console.log(message); 
    }); 

,但不起作用。我在truefalse都试过object_mode,但输出结果是一样的。

下面是我使用的全码:

document.querySelector('#button').onclick = function() { 

    var stream = WatsonSpeech.SpeechToText.recognizeMicrophone({ 
    token: token, 
    model: 'en-US_BroadbandModel', 
    keywords: ["Colorado"], 
    keywords_threshold: 0.50, 
    word_confidence: true, 
    // outputElement: '#output' // send text to browser instead of console 
    object_mode: false 
    }); 

    stream.setEncoding('utf8'); // get text instead of Buffers for on data events 

    stream.on('data', function(data) { // send text to console instead of browser 
    console.log(data); 
    }); 

    stream.on('error', function(err) { 
    console.log(err); 
    }); 

    document.querySelector('#stop').onclick = function() { 
    stream.stop(); 
    }; 
}; 

回答

2

recognizeMicrophone()方法是一个帮手链结合在一起的多个流。 message事件在中间的其中一个流上触发。但是,您可以通过stream.recognizeStream访问该链接 - 它始终与链中的最后一个链接,以支持这种情况。

所以,在你的代码,它应该是这个样子:

stream.recognizeStream.on('message', function(frame, data) { 
    console.log('message', frame, data) 
}); 

然而,这主要是没有进行调试。如果您设置objectMode: true请勿请致电stream.setEncoding('utf8');,结果JSON应在data事件中发出。

(这是沃森的Node.js SDK有些不同,如果你熟悉它的行为。有计划统一两个,但从来没有足够的时间...)

+0

谢谢,成功了!你能提出我的问题吗? –

相关问题