语音识别(语音转文本 - STT cordova 插件)

Voice recognition (speech to text - STT cordova plugin)

我正在使用 cordova pluginIonic2 框架中寻找 语音识别

如果可以实现的话,能不能给个代码示例(.html and .ts)?

我找到了这个,但它是针对 Ionic1 的:http://devgirl.org/2016/01/08/speaking-with-cordova/ 我无法为 Ionic2 调整代码。

非常感谢您提供的任何帮助,抱歉我的英语不好。

来源:https://github.com/macdonst/SpeechRecognitionPlugin.

使用命令行,将此插件添加到您的 Ionic2 项目中:

cd Your_Project_Root_Folder

从 iOS 10 开始,必须在 info.plist 中添加 NSMicrophoneUsageDescription 才能访问麦克风。

要添加此条目,您可以在插件安装时传递 MICROPHONE_USAGE_DESCRIPTION 变量。

ionic plugin add https://github.com/macdonst/SpeechRecognitionPlugin --variable MICROPHONE_USAGE_DESCRIPTION="your usage message"

在 iOS 10 及更高版本上,它使用本机 SFSpeechRecognizer(与 Siri 相同)。 在 iOS 9 及更早版本上,它使用 iSpeech SDK,需要一个 API 密钥,在 https://www.ispeech.org/ 上获得一个,它是免费的。要提供密钥,请在 config.xml

中添加此首选项
<preference name="apiKey" value="yourApiKeyHere" />

.ts 文件的开头添加声明,就在导入之后,class 定义之前:

declare const SpeechRecognition: any;

然后,在你的 class:

recognition: any;

constructor() {}

SpeechToText() {
    this.platform.ready().then(() => {
        this.recognition = new SpeechRecognition(); 
        this.recognition.lang = 'en-US';
        this.recognition.onnomatch = (event => {
            console.log('No match found.');
        });
        this.recognition.onerror = (event => {
            console.log('Error happens.');
        });
        this.recognition.onresult = (event => {
            if (event.results.length > 0) {
                console.log('Output STT: ', event.results[0][0].transcript);            
            }
        });     
        this.recognition.start();
    });
}

iSpeech 支持的语言有: 英语(加拿大)(en-CA) 英语(美国)(en-US) 西班牙语(西班牙)(es-ES) 法语(法国)(fr-FR) 意大利语(意大利)(it-IT ) 波兰语(波兰)(pl-PL) 葡萄牙语(葡萄牙)(pt-PT)

ps:对于 iOS 10 错误 kAFAssistantErrorDomain 或者如果您必须等待结果,请检查 this.

完成!

编辑:在 Ionic v3.0.1 (2017-04-06) 上测试并且工作正常:)

我使用这个 AngularJS 指令:

ng-speech-recognition