@wenlongche/nativescript-speech-recognition 2.0.0 β 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
# NativeScript Speech Recognition
|
|
2
|
+
|
|
3
|
+
> **Fork Notice:** This is a fork of [nativescript-speech-recognition](https://github.com/EddyVerbruggen/nativescript-speech-recognition) by [Eddy Verbruggen](https://github.com/EddyVerbruggen). This fork includes additional improvements and enhancements. See [PR #51](https://github.com/EddyVerbruggen/nativescript-speech-recognition/pull/51) for details on the changes.
|
|
4
|
+
|
|
5
|
+
[![Build Status][build-status]][build-url]
|
|
6
|
+
[![NPM version][npm-image]][npm-url]
|
|
7
|
+
[![Downloads][downloads-image]][npm-url]
|
|
8
|
+
[![Twitter Follow][twitter-image]][twitter-url]
|
|
9
|
+
|
|
10
|
+
[build-status]:https://travis-ci.org/EddyVerbruggen/nativescript-speech-recognition.svg?branch=master
|
|
11
|
+
[build-url]:https://travis-ci.org/EddyVerbruggen/nativescript-speech-recognition
|
|
12
|
+
[npm-image]:http://img.shields.io/npm/v/nativescript-speech-recognition.svg
|
|
13
|
+
[npm-url]:https://npmjs.org/package/nativescript-speech-recognition
|
|
14
|
+
[downloads-image]:http://img.shields.io/npm/dm/nativescript-speech-recognition.svg
|
|
15
|
+
[twitter-image]:https://img.shields.io/twitter/follow/eddyverbruggen.svg?style=social&label=Follow%20me
|
|
16
|
+
[twitter-url]:https://twitter.com/eddyverbruggen
|
|
17
|
+
|
|
18
|
+
This is the plugin [demo](https://github.com/EddyVerbruggen/nativescript-speech-recognition/tree/master/demo) in action..
|
|
19
|
+
|
|
20
|
+
| ..while recognizing Dutch π³π± | .. after recognizing American-English πΊπΈ |
|
|
21
|
+
| --- | --- |
|
|
22
|
+
| <img src="https://github.com/EddyVerbruggen/nativescript-speech-recognition/raw/master/screenshots/ios-nl.jpg" width="375px" /> | <img src="https://github.com/EddyVerbruggen/nativescript-speech-recognition/raw/master/screenshots/ios-en.jpg" width="375px" /> |
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
From the command prompt go to your app's root folder and execute:
|
|
26
|
+
|
|
27
|
+
### NativeScript 7+:
|
|
28
|
+
```bash
|
|
29
|
+
ns plugin add @wenlongche/nativescript-speech-recognition
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Original package (NativeScript 7+):
|
|
33
|
+
```bash
|
|
34
|
+
ns plugin add nativescript-speech-recognition
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Testing
|
|
38
|
+
You'll need to test this on a real device as a Simulator/Emulator doesn't have speech recognition capabilities.
|
|
39
|
+
|
|
40
|
+
## API
|
|
41
|
+
|
|
42
|
+
### `available`
|
|
43
|
+
|
|
44
|
+
Depending on the OS version a speech engine may not be available.
|
|
45
|
+
|
|
46
|
+
#### JavaScript
|
|
47
|
+
```js
|
|
48
|
+
// require the plugin
|
|
49
|
+
var SpeechRecognition = require("nativescript-speech-recognition").SpeechRecognition;
|
|
50
|
+
|
|
51
|
+
// instantiate the plugin
|
|
52
|
+
var speechRecognition = new SpeechRecognition();
|
|
53
|
+
|
|
54
|
+
speechRecognition.available().then(
|
|
55
|
+
function(available) {
|
|
56
|
+
console.log(available ? "YES!" : "NO");
|
|
57
|
+
}
|
|
58
|
+
);
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
#### TypeScript
|
|
62
|
+
```typescript
|
|
63
|
+
// import the plugin
|
|
64
|
+
import { SpeechRecognition } from "nativescript-speech-recognition";
|
|
65
|
+
|
|
66
|
+
class SomeClass {
|
|
67
|
+
private speechRecognition = new SpeechRecognition();
|
|
68
|
+
|
|
69
|
+
public checkAvailability(): void {
|
|
70
|
+
this.speechRecognition.available().then(
|
|
71
|
+
(available: boolean) => console.log(available ? "YES!" : "NO"),
|
|
72
|
+
(err: string) => console.log(err)
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### `requestPermission`
|
|
79
|
+
You can either let `startListening` handle permissions when needed, but if you want to have more control
|
|
80
|
+
over when the permission popups are shown, you can use this function:
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
this.speechRecognition.requestPermission().then((granted: boolean) => {
|
|
84
|
+
console.log("Granted? " + granted);
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### `startListening`
|
|
89
|
+
|
|
90
|
+
On iOS this will trigger two prompts:
|
|
91
|
+
|
|
92
|
+
The first prompt requests to allow Apple to analyze the voice input. The user will see a consent screen which you can extend with your own message by adding a fragment like this to `app/App_Resources/iOS/Info.plist`:
|
|
93
|
+
|
|
94
|
+
```xml
|
|
95
|
+
<key>NSSpeechRecognitionUsageDescription</key>
|
|
96
|
+
<string>My custom recognition usage description. Overriding the default empty one in the plugin.</string>
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
The second prompt requests access to the microphone:
|
|
100
|
+
|
|
101
|
+
```xml
|
|
102
|
+
<key>NSMicrophoneUsageDescription</key>
|
|
103
|
+
<string>My custom microphone usage description. Overriding the default empty one in the plugin.</string>
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
#### TypeScript
|
|
107
|
+
```typescript
|
|
108
|
+
// import the options
|
|
109
|
+
import { SpeechRecognitionTranscription } from "nativescript-speech-recognition";
|
|
110
|
+
|
|
111
|
+
this.speechRecognition.startListening(
|
|
112
|
+
{
|
|
113
|
+
// optional, uses the device locale by default
|
|
114
|
+
locale: "en-US",
|
|
115
|
+
// set to true to get results back continuously
|
|
116
|
+
returnPartialResults: true,
|
|
117
|
+
// set to true to keep listening after each speech utterance (Android only)
|
|
118
|
+
// when enabled, the recognizer automatically restarts after recognition completes
|
|
119
|
+
listenContinuously: false,
|
|
120
|
+
// this callback will be invoked repeatedly during recognition
|
|
121
|
+
onResult: (transcription: SpeechRecognitionTranscription) => {
|
|
122
|
+
console.log(`User said: ${transcription.text}`);
|
|
123
|
+
console.log(`User finished?: ${transcription.finished}`);
|
|
124
|
+
},
|
|
125
|
+
onError: (error: string | number) => {
|
|
126
|
+
// because of the way iOS and Android differ, this is either:
|
|
127
|
+
// - iOS: A 'string', describing the issue.
|
|
128
|
+
// - Android: A 'number', referencing an 'ERROR_*' constant from https://developer.android.com/reference/android/speech/SpeechRecognizer.
|
|
129
|
+
// When listenContinuously is enabled, the recognizer will automatically restart
|
|
130
|
+
// after errors, providing hands-free continuous voice input.
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
).then(
|
|
134
|
+
(started: boolean) => { console.log(`started listening`) },
|
|
135
|
+
(errorMessage: string) => { console.log(`Error: ${errorMessage}`); }
|
|
136
|
+
).catch((error: string | number) => {
|
|
137
|
+
// same as the 'onError' handler, but this may not return if the error occurs after listening has successfully started (because that resolves the promise,
|
|
138
|
+
// hence the' onError' handler was created.
|
|
139
|
+
});
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
##### Angular tip
|
|
143
|
+
If you're using this plugin in Angular, then note that the `onResult` callback is not part of Angular's lifecycle.
|
|
144
|
+
So either update the UI in [an `ngZone` as shown here](https://github.com/EddyVerbruggen/nativescript-pluginshowcase/blob/28f65ef98716ad7c4698071b9c394cceb2d9748f/app/speech/speech.component.ts#L154),
|
|
145
|
+
or use [`ChangeDetectorRef` as shown here](https://blog.paulhalliday.io/2017/06/24/nativescript-speech-recognition/).
|
|
146
|
+
|
|
147
|
+
### `stopListening`
|
|
148
|
+
|
|
149
|
+
#### TypeScript
|
|
150
|
+
```typescript
|
|
151
|
+
this.speechRecognition.stopListening().then(
|
|
152
|
+
() => { console.log(`stopped listening`) },
|
|
153
|
+
(errorMessage: string) => { console.log(`Stop error: ${errorMessage}`); }
|
|
154
|
+
);
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Continuous Listening Mode
|
|
158
|
+
|
|
159
|
+
### Using `listenContinuously` Option
|
|
160
|
+
|
|
161
|
+
You can enable **continuous listening mode** on **Android** by setting the `listenContinuously` option to `true`:
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
this.speechRecognition.startListening({
|
|
165
|
+
locale: "en-US",
|
|
166
|
+
returnPartialResults: true,
|
|
167
|
+
listenContinuously: true, // Enable continuous listening
|
|
168
|
+
onResult: (transcription: SpeechRecognitionTranscription) => {
|
|
169
|
+
console.log(`User said: ${transcription.text}`);
|
|
170
|
+
// transcription.finished will be true after each utterance,
|
|
171
|
+
// but listening continues automatically
|
|
172
|
+
},
|
|
173
|
+
onError: (error: string | number) => {
|
|
174
|
+
console.log(`Error: ${error}`);
|
|
175
|
+
// Recognizer will automatically restart after errors
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
When `listenContinuously` is enabled:
|
|
181
|
+
|
|
182
|
+
- **Automatic restart**: After each speech utterance completes, listening automatically restarts
|
|
183
|
+
- **Continuous operation**: The recognizer keeps running until you explicitly call `stopListening()`
|
|
184
|
+
- **Error recovery**: Automatically restarts after errors (including speech timeout)
|
|
185
|
+
- **Text accumulation**: Recognized text accumulates across multiple utterances
|
|
186
|
+
- **Hands-free experience**: Ideal for voice assistants and applications requiring continuous voice input
|
|
187
|
+
|
|
188
|
+
To stop continuous listening, call `stopListening()`:
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
191
|
+
this.speechRecognition.stopListening().then(() => {
|
|
192
|
+
console.log('Stopped continuous listening');
|
|
193
|
+
});
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
**Note**: This feature is currently available on **Android only**. iOS support may be added in future versions.
|
|
197
|
+
|
|
198
|
+
## Demo app (Angular)
|
|
199
|
+
This plugin is part of the [plugin showcase app](https://github.com/EddyVerbruggen/nativescript-pluginshowcase/tree/master/app/speech) I built using Angular.
|
|
200
|
+
|
|
201
|
+
### Angular video tutorial
|
|
202
|
+
Rather watch a video? Check out [this tutorial on YouTube](https://www.youtube.com/watch?v=C5i_EYjfuTE).
|
|
203
|
+
|
|
204
|
+
## Credits
|
|
205
|
+
|
|
206
|
+
This project is based on [nativescript-speech-recognition](https://github.com/EddyVerbruggen/nativescript-speech-recognition) by [Eddy Verbruggen](https://github.com/EddyVerbruggen), licensed under the MIT License.
|
|
207
|
+
|
|
208
|
+
Original contributors:
|
|
209
|
+
- [Eddy Verbruggen](https://github.com/EddyVerbruggen) (original author)
|
|
210
|
+
- [Brad Martin](https://github.com/bradmartin)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wenlongche/nativescript-speech-recognition",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"description": "Fork of nativescript-speech-recognition - Speech to text plugin, leveraging iOS and Android's built-in recognition engines.",
|
|
5
5
|
"main": "speech-recognition",
|
|
6
6
|
"typings": "index.d.ts",
|
|
Binary file
|