npm i --save nativescript-speech-recognition
- 版本: 2.0.0
- GitHub: https://github.com/EddyVerbruggen/nativescript-speech-recognition
- NPM: https://npmjs.net.cn/package/nativescript-speech-recognition
- 下载
- 昨天: 14
- 上周: 72
- 上个月: 321
NativeScript Speech Recognition
这是该插件 演示 的效果..
..在识别荷兰语 🇳🇱 | ..识别美国英语 🇺🇸 之后 |
---|---|
安装
从命令提示符进入您的应用程序根目录并执行
NativeScript 7+
ns plugin add nativescript-speech-recognition
NativeScript < 7
tns plugin add [email protected]
测试
您需要在真实设备上测试此功能,因为模拟器/仿真器没有语音识别功能。
API
可用
根据操作系统版本,语音引擎可能不可用。
JavaScript
// require the plugin
var SpeechRecognition = require("nativescript-speech-recognition").SpeechRecognition;
// instantiate the plugin
var speechRecognition = new SpeechRecognition();
speechRecognition.available().then(
function(available) {
console.log(available ? "YES!" : "NO");
}
);
TypeScript
// import the plugin
import { SpeechRecognition } from "nativescript-speech-recognition";
class SomeClass {
private speechRecognition = new SpeechRecognition();
public checkAvailability(): void {
this.speechRecognition.available().then(
(available: boolean) => console.log(available ? "YES!" : "NO"),
(err: string) => console.log(err)
);
}
}
requestPermission
您可以让 startListening
在需要时处理权限,但如果您想更控制权限弹窗的显示时间,可以使用此函数
this.speechRecognition.requestPermission().then((granted: boolean) => {
console.log("Granted? " + granted);
});
startListening
在 iOS 上这将触发两个提示
第一个提示请求苹果分析语音输入。用户将看到一个同意屏幕,您可以通过添加如下片段到 app/App_Resources/iOS/Info.plist
来扩展您自己的消息
<key>NSSpeechRecognitionUsageDescription</key>
<string>My custom recognition usage description. Overriding the default empty one in the plugin.</string>
第二个提示请求访问麦克风
<key>NSMicrophoneUsageDescription</key>
<string>My custom microphone usage description. Overriding the default empty one in the plugin.</string>
TypeScript
// import the options
import { SpeechRecognitionTranscription } from "nativescript-speech-recognition";
this.speechRecognition.startListening(
{
// optional, uses the device locale by default
locale: "en-US",
// set to true to get results back continuously
returnPartialResults: true,
// this callback will be invoked repeatedly during recognition
onResult: (transcription: SpeechRecognitionTranscription) => {
console.log(`User said: ${transcription.text}`);
console.log(`User finished?: ${transcription.finished}`);
},
onError: (error: string | number) => {
// because of the way iOS and Android differ, this is either:
// - iOS: A 'string', describing the issue.
// - Android: A 'number', referencing an 'ERROR_*' constant from https://android-docs.cn/reference/android/speech/SpeechRecognizer.
// If that code is either 6 or 7 you may want to restart listening.
}
}
).then(
(started: boolean) => { console.log(`started listening`) },
(errorMessage: string) => { console.log(`Error: ${errorMessage}`); }
).catch((error: string | number) => {
// same as the 'onError' handler, but this may not return if the error occurs after listening has successfully started (because that resolves the promise,
// hence the' onError' handler was created.
});
Angular 提示
如果您在 Angular 中使用此插件,请注意,onResult
回调不是 Angular 生命周期的一部分。因此,您可以在 ngZone 中更新 UI,如这里所示,或者使用 ChangeDetectorRef
,如这里所示。
stopListening
TypeScript
this.speechRecognition.stopListening().then(
() => { console.log(`stopped listening`) },
(errorMessage: string) => { console.log(`Stop error: ${errorMessage}`); }
);
演示应用程序 (Angular)
此插件是我在 Angular 中构建的 插件展示应用程序 的一部分。
Angular 视频教程
想看视频?请查看 YouTube 上的这个教程。