expo-speech-recognition 0.2.20 → 0.2.22
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/LICENSE +21 -0
- package/README.md +123 -47
- package/android/src/main/java/expo/modules/speechrecognition/ExpoSpeechRecognitionModule.kt +4 -0
- package/android/src/main/java/expo/modules/speechrecognition/ExpoSpeechService.kt +11 -2
- package/app.plugin.js +18 -12
- package/build/ExpoSpeechRecognitionModule.d.ts +2 -40
- package/build/ExpoSpeechRecognitionModule.d.ts.map +1 -1
- package/build/ExpoSpeechRecognitionModule.js +1 -0
- package/build/ExpoSpeechRecognitionModule.js.map +1 -1
- package/build/ExpoSpeechRecognitionModule.types.d.ts +65 -43
- package/build/ExpoSpeechRecognitionModule.types.d.ts.map +1 -1
- package/build/ExpoSpeechRecognitionModule.types.js.map +1 -1
- package/build/ExpoSpeechRecognitionModule.web.d.ts.map +1 -1
- package/build/ExpoSpeechRecognitionModule.web.js +5 -4
- package/build/ExpoSpeechRecognitionModule.web.js.map +1 -1
- package/build/ExpoWebSpeechRecognition.d.ts +1 -1
- package/build/ExpoWebSpeechRecognition.d.ts.map +1 -1
- package/build/ExpoWebSpeechRecognition.js +2 -2
- package/build/ExpoWebSpeechRecognition.js.map +1 -1
- package/build/index.d.ts +14 -1
- package/build/index.d.ts.map +1 -1
- package/build/index.js +4 -0
- package/build/index.js.map +1 -1
- package/build/useSpeechRecognitionEvent.d.ts +1 -1
- package/build/useSpeechRecognitionEvent.d.ts.map +1 -1
- package/build/useSpeechRecognitionEvent.js +1 -1
- package/build/useSpeechRecognitionEvent.js.map +1 -1
- package/ios/ExpoSpeechRecognitionModule.swift +6 -2
- package/ios/ExpoSpeechRecognizer.swift +0 -1
- package/package.json +1 -1
- package/src/ExpoSpeechRecognitionModule.ts +3 -1
- package/src/ExpoSpeechRecognitionModule.types.ts +76 -45
- package/src/ExpoSpeechRecognitionModule.web.ts +8 -12
- package/src/ExpoWebSpeechRecognition.ts +6 -9
- package/src/index.ts +16 -0
- package/src/useSpeechRecognitionEvent.ts +2 -2
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 jamsch
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -18,13 +18,14 @@ expo-speech-recognition implements the iOS [`SFSpeechRecognizer`](https://develo
|
|
|
18
18
|
- [Persisting Audio Recordings](#persisting-audio-recordings)
|
|
19
19
|
- [Transcribing audio files](#transcribing-audio-files)
|
|
20
20
|
- [Supported input audio formats](#supported-input-audio-formats)
|
|
21
|
-
- [Android](#android)
|
|
22
|
-
- [iOS](#ios)
|
|
23
21
|
- [File transcription example](#file-transcription-example)
|
|
24
22
|
- [Polyfilling the Web SpeechRecognition API](#polyfilling-the-web-speechrecognition-api)
|
|
25
23
|
- [Muting the beep sound on Android](#muting-the-beep-sound-on-android)
|
|
26
24
|
- [Improving accuracy of single-word prompts](#improving-accuracy-of-single-word-prompts)
|
|
27
25
|
- [Platform Compatibility Table](#platform-compatibility-table)
|
|
26
|
+
- [Common Troubleshooting issues](#common-troubleshooting-issues)
|
|
27
|
+
- [Android issues](#android-issues)
|
|
28
|
+
- [iOS issues](#ios-issues)
|
|
28
29
|
- [API Methods](#api-methods)
|
|
29
30
|
- [start()](#startoptions-speechrecognitionoptions-void)
|
|
30
31
|
- [stop()](#stop-void)
|
|
@@ -32,13 +33,14 @@ expo-speech-recognition implements the iOS [`SFSpeechRecognizer`](https://develo
|
|
|
32
33
|
- [requestPermissionsAsync()](#requestpermissionsasync-promisepermissionresponse)
|
|
33
34
|
- [getPermissionsAsync()](#getpermissionsasync-promisepermissionresponse)
|
|
34
35
|
- [getStateAsync()](#getstateasync-promisespeechrecognitionstate)
|
|
35
|
-
- [addSpeechRecognitionListener()](#
|
|
36
|
+
- [addSpeechRecognitionListener()](#addspeechrecognitionlistener)
|
|
36
37
|
- [getSupportedLocales()](#getsupportedlocales-promise-locales-string-installedlocales-string-)
|
|
37
38
|
- [getSpeechRecognitionServices()](#getspeechrecognitionservices-string-android-only)
|
|
38
39
|
- [getDefaultRecognitionService()](#getdefaultrecognitionservice--packagename-string--android-only)
|
|
39
40
|
- [getAssistantService()](#getassistantservice--packagename-string--android-only)
|
|
41
|
+
- [isRecognitionAvailable()](#isrecognitionavailable-boolean)
|
|
40
42
|
- [supportsOnDeviceRecognition()](#supportsondevicerecognition-boolean)
|
|
41
|
-
- [supportsRecording()](#supportsrecording-boolean
|
|
43
|
+
- [supportsRecording()](#supportsrecording-boolean)
|
|
42
44
|
- [androidTriggerOfflineModelDownload()](#androidtriggerofflinemodeldownload-locale-string--promise-status-opened_dialog--download_success--download_canceled-message-string-)
|
|
43
45
|
- [setCategoryIOS()](#setcategoryios-void-ios-only)
|
|
44
46
|
- [getAudioSessionCategoryAndOptionsIOS()](#getaudiosessioncategoryandoptionsios-ios-only)
|
|
@@ -56,23 +58,29 @@ npm install expo-speech-recognition
|
|
|
56
58
|
|
|
57
59
|
> The config plugin updates the Android App Manifest to include package visibility filtering for `com.google.android.googlequicksearchbox` (Google's Speech Recognition) along with the required permissions for Android and iOS.
|
|
58
60
|
|
|
61
|
+
To configure `androidSpeechServicePackages`, add additional speech service packages here that aren't listed under the `forceQueryable` section when running the command: `adb shell dumpsys package queries`.
|
|
62
|
+
|
|
59
63
|
```js
|
|
60
64
|
// app.json
|
|
61
65
|
{
|
|
62
66
|
"expo": {
|
|
63
67
|
"plugins": [
|
|
68
|
+
"plugin-one",
|
|
69
|
+
"plugin-two",
|
|
70
|
+
|
|
71
|
+
// no config (v0.2.22+)
|
|
72
|
+
"expo-speech-recognition",
|
|
73
|
+
|
|
74
|
+
// or with config
|
|
64
75
|
[
|
|
65
76
|
"expo-speech-recognition",
|
|
66
77
|
{
|
|
67
78
|
"microphonePermission": "Allow $(PRODUCT_NAME) to use the microphone.",
|
|
68
79
|
"speechRecognitionPermission": "Allow $(PRODUCT_NAME) to use speech recognition.",
|
|
69
|
-
// Add additional speech service packages here that aren't listed
|
|
70
|
-
// under the `forceQueryable` section when running the command:
|
|
71
|
-
// "adb shell dumpsys package queries"
|
|
72
|
-
// default: ["com.google.android.googlequicksearchbox"]
|
|
73
80
|
"androidSpeechServicePackages": ["com.google.android.googlequicksearchbox"]
|
|
74
81
|
}
|
|
75
82
|
]
|
|
83
|
+
// rest of your plugins
|
|
76
84
|
]
|
|
77
85
|
}
|
|
78
86
|
}
|
|
@@ -228,7 +236,9 @@ ExpoSpeechRecognitionModule.start({
|
|
|
228
236
|
interimResults: true,
|
|
229
237
|
// The maximum number of alternative transcriptions to return.
|
|
230
238
|
maxAlternatives: 1,
|
|
231
|
-
//
|
|
239
|
+
// [Default: false] Continuous recognition.
|
|
240
|
+
// If false on iOS, recognition will run until no speech is detected for 3 seconds.
|
|
241
|
+
// Not supported on Android 12 and below.
|
|
232
242
|
continuous: true,
|
|
233
243
|
// [Default: false] Prevent device from sending audio over the network. Only enabled if the device supports it.
|
|
234
244
|
requiresOnDeviceRecognition: false,
|
|
@@ -252,7 +262,7 @@ ExpoSpeechRecognitionModule.start({
|
|
|
252
262
|
mode: "measurement",
|
|
253
263
|
},
|
|
254
264
|
// [Default: undefined] Recording options for Android & iOS
|
|
255
|
-
//
|
|
265
|
+
// Android 13+ and iOS only.
|
|
256
266
|
recordingOptions: {
|
|
257
267
|
// [Default: false] Whether to persist the audio to a local file path.
|
|
258
268
|
persist: false,
|
|
@@ -304,7 +314,7 @@ Events are largely based on the [Web Speech API](https://developer.mozilla.org/e
|
|
|
304
314
|
| ------------- | ------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
305
315
|
| `audiostart` | Audio capturing has started | Includes the `uri` if `recordingOptions.persist` is enabled. |
|
|
306
316
|
| `audioend` | Audio capturing has ended | Includes the `uri` if `recordingOptions.persist` is enabled. |
|
|
307
|
-
| `end` | Speech recognition service has disconnected. | This should be the last event dispatched.
|
|
317
|
+
| `end` | Speech recognition service has disconnected. | This should always be the last event dispatched, including after errors. |
|
|
308
318
|
| `error` | Fired when a speech recognition error occurs. | You'll also receive an `error` event (with code "aborted") when calling `.abort()` |
|
|
309
319
|
| `nomatch` | Speech recognition service returns a final result with no significant recognition. | You may have non-final results recognized. This may get emitted after cancellation. |
|
|
310
320
|
| `result` | Speech recognition service returns a word or phrase has been positively recognized. | On Android, continous mode runs as a segmented session, meaning when a final result is reached, additional partial and final results will cover a new segment separate from the previous final result. On iOS, you should expect one final result before speech recognition has stopped. |
|
|
@@ -336,27 +346,29 @@ useSpeechRecognitionEvent("error", (event) => {
|
|
|
336
346
|
const error: ExpoSpeechRecognitionErrorCode = "audio-capture";
|
|
337
347
|
```
|
|
338
348
|
|
|
339
|
-
The error code is
|
|
340
|
-
|
|
341
|
-
| Error Code | Description
|
|
342
|
-
| ------------------------ |
|
|
343
|
-
| `aborted` | The user called `ExpoSpeechRecognitionModule.abort()`
|
|
344
|
-
| `audio-capture` | Audio recording error.
|
|
345
|
-
| `bad-grammar` | Provided grammar is invalid. (
|
|
346
|
-
| `language-not-supported` | Locale is not supported by the speech recognizer.
|
|
347
|
-
| `network` | Network communication required for completing the recognition failed.
|
|
348
|
-
| `no-speech` | No final speech was detected.
|
|
349
|
-
| `not-allowed` | Permission to use speech recognition or microphone was not granted.
|
|
350
|
-
| `service-not-allowed` | Recognizer is unavailable.
|
|
351
|
-
| `busy` | The recognizer is busy and cannot accept any new recognition requests.
|
|
352
|
-
| `client` |
|
|
349
|
+
The error code is based on the [Web Speech API error codes](https://developer.mozilla.org/en-US/docs/Web/API/SpeechRecognitionErrorEvent/error).
|
|
350
|
+
|
|
351
|
+
| Error Code | Description |
|
|
352
|
+
| ------------------------ | ------------------------------------------------------------------------------- |
|
|
353
|
+
| `aborted` | The user called `ExpoSpeechRecognitionModule.abort()` |
|
|
354
|
+
| `audio-capture` | Audio recording error. |
|
|
355
|
+
| `bad-grammar` | Provided grammar is invalid. (Web only) |
|
|
356
|
+
| `language-not-supported` | Locale is not supported by the speech recognizer. |
|
|
357
|
+
| `network` | Network communication required for completing the recognition failed. |
|
|
358
|
+
| `no-speech` | No final speech was detected. |
|
|
359
|
+
| `not-allowed` | Permission to use speech recognition or microphone was not granted. |
|
|
360
|
+
| `service-not-allowed` | Recognizer is unavailable. |
|
|
361
|
+
| `busy` | The recognizer is busy and cannot accept any new recognition requests. |
|
|
362
|
+
| `client` | An unknown client-side error. Corresponds with `SpeechRecognizer.ERROR_CLIENT`. |
|
|
363
|
+
| `speech-timeout` | (Android) No speech input. |
|
|
364
|
+
| `unknown` | (Android) Unknown error |
|
|
353
365
|
|
|
354
366
|
## Persisting Audio Recordings
|
|
355
367
|
|
|
356
368
|
If you would like to persist the recognized audio for later use, you can enable the `recordingOptions.persist` option when calling `start()`. Enabling this setting will emit an `{ uri: string }` event object in the `audiostart` and `audioend` events with the local file path.
|
|
357
369
|
|
|
358
370
|
> [!IMPORTANT]
|
|
359
|
-
>
|
|
371
|
+
> This feature is available on Android 13+ and iOS. Call [`supportsRecording()`](#supportsrecording-boolean) to see if it's available before using this feature.
|
|
360
372
|
|
|
361
373
|
Default audio output formats:
|
|
362
374
|
|
|
@@ -439,11 +451,11 @@ function AudioPlayer(props: { source: string }) {
|
|
|
439
451
|
|
|
440
452
|
## Transcribing audio files
|
|
441
453
|
|
|
442
|
-
You can use the `audioSource.uri` option to transcribe audio files instead of using the microphone.
|
|
443
|
-
|
|
444
454
|
> [!IMPORTANT]
|
|
445
455
|
> This feature is available on Android 13+ and iOS. If the device does not support the feature, you'll receive an `error` event with the code `audio-capture`.
|
|
446
456
|
|
|
457
|
+
Instead of using the microphone, you can configure the `audioSource.uri` option to transcribe audio files.
|
|
458
|
+
|
|
447
459
|
### Supported input audio formats
|
|
448
460
|
|
|
449
461
|
#### Android
|
|
@@ -598,6 +610,9 @@ recognition.abort();
|
|
|
598
610
|
|
|
599
611
|
## Muting the beep sound on Android
|
|
600
612
|
|
|
613
|
+
> [!NOTE]
|
|
614
|
+
> This only applies to Android 13 and above.
|
|
615
|
+
|
|
601
616
|
On Android, you may notice that there's a beep sound when you start and stop speech recognition. This is due to a hardcoded behavior in the underlying SpeechRecognizer API. However, a workaround you can use is by enabling continuous recognition:
|
|
602
617
|
|
|
603
618
|
```ts
|
|
@@ -607,11 +622,19 @@ ExpoSpeechRecognitionModule.start({
|
|
|
607
622
|
lang: "en-US",
|
|
608
623
|
continuous: true,
|
|
609
624
|
});
|
|
625
|
+
|
|
626
|
+
// Or enable recording persistence
|
|
627
|
+
ExpoSpeechRecognitionModule.start({
|
|
628
|
+
lang: "en-US",
|
|
629
|
+
recordingOptions: {
|
|
630
|
+
persist: true,
|
|
631
|
+
},
|
|
632
|
+
});
|
|
610
633
|
```
|
|
611
634
|
|
|
612
|
-
Under the hood,
|
|
635
|
+
Under the hood, both of these set the `EXTRA_AUDIO_SOURCE` in the recognizer intent to a custom microphone source instead of using the default microphone setting.
|
|
613
636
|
|
|
614
|
-
|
|
637
|
+
If you intend to use the first option and still want to maintain the same behavior as non-continuous mode, you should listen for a result event with `isFinal: true` and then immediately call `abort()` to stop the recognition.
|
|
615
638
|
|
|
616
639
|
## Improving accuracy of single-word prompts
|
|
617
640
|
|
|
@@ -628,7 +651,7 @@ As of 7 Aug 2024, the following platforms are supported:
|
|
|
628
651
|
|
|
629
652
|
| Platform | Supported | Default Recognition Engine | Notes |
|
|
630
653
|
| ---------------------- | --------- | -------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
631
|
-
| Android (React Native) | ✅ | Google | Recording
|
|
654
|
+
| Android (React Native) | ✅ | Google | Continuous mode and Recording features are only supported on Android 13 and above. Recognition engine can be changed via `androidRecognitionServicePackage` |
|
|
632
655
|
| iOS (React Native) | ✅ | Siri | |
|
|
633
656
|
| Chrome Desktop | ✅ | Google (server-based) | Implemented via prefix `webkitSpeechRecognition`. |
|
|
634
657
|
| Safari Desktop >= v16 | ✅ | Siri | Implemented via prefix `webkitSpeechRecognition`. Siri needs to be enabled |
|
|
@@ -639,6 +662,40 @@ As of 7 Aug 2024, the following platforms are supported:
|
|
|
639
662
|
| Brave Desktop | ❌ | - | As of Aug 2024, Brave is working on an implementation however there's currently no ETA (source: [brave-browser/issues/3725](https://github.com/brave/brave-browser/issues/3725#issuecomment-2224068859)) |
|
|
640
663
|
| Firefox Desktop | ❌ | - | No SpeechRecognition implementation |
|
|
641
664
|
|
|
665
|
+
## Common Troubleshooting Issues
|
|
666
|
+
|
|
667
|
+
### Android issues
|
|
668
|
+
|
|
669
|
+
#### Single words/letters/characters not recognized
|
|
670
|
+
|
|
671
|
+
See [Improving accuracy of single-word prompts](#improving-accuracy-of-single-word-prompts).
|
|
672
|
+
|
|
673
|
+
#### Speech recognition unavailable, not supported, or hanging
|
|
674
|
+
|
|
675
|
+
- For Android 13+, check if the device has the [Speech Recognition & Synthesis](https://play.google.com/store/apps/details?id=com.google.android.tts) (`com.google.android.tts`) package installed **and enabled**. If it was previously disabled, you may need to restart the device for the changes to take effect.
|
|
676
|
+
- For Android 12 and lower, check if the device has the [Google app](https://play.google.com/store/apps/details?id=com.google.android.googlequicksearchbox) (`com.google.android.googlequicksearchbox`) package installed **and enabled**.
|
|
677
|
+
- Use the `getSpeechRecognitionServices()` function to see what services are available on the device and `getDefaultRecognitionService()` to see which service is the default.
|
|
678
|
+
|
|
679
|
+
#### On-device recognition not working
|
|
680
|
+
|
|
681
|
+
On an Android 12 emulator, I couldn't verify whether on-device recognition works (and there's no online resources that seem to verify that it works). You'll likely see that it starts and then stops with a "error" event and the code `no-speech`.
|
|
682
|
+
|
|
683
|
+
For Android 13 and above however, you can try the following:
|
|
684
|
+
|
|
685
|
+
- Call the `getSupportedLocales()` function to see what installed locales are supported by the device. On Android 13+, you likely won't see any locales installed. If that is the case, you can call `androidTriggerOfflineModelDownload()` to download an offline model for a specific locale you want to use in order to use on-device recognition.
|
|
686
|
+
- If you still can't get it to work, you may want to double check if the locale is installed by opening the Android System Intelligence app (which is somewhat hidden through a few screens in the Settings app). You can navigate there through the following steps:
|
|
687
|
+
`Settings -> Security and privacy -> More privacy settings -> Android System Intelligence -> On-device speech recognition`, or watch [this video](https://github.com/jamsch/expo-speech-recognition/issues/8#issuecomment-2309557322).
|
|
688
|
+
|
|
689
|
+
### iOS issues
|
|
690
|
+
|
|
691
|
+
#### Audio session issues / crashes
|
|
692
|
+
|
|
693
|
+
If you're running a multimedia application with audio or video playback, you'll need to keep in mind that this library does modify the current audio session category and mode which may cause issues. In order to work seamlessly in your application, you should make use of the following APIs:
|
|
694
|
+
|
|
695
|
+
- `getAudioSessionCategoryAndOptionsIOS()` to retrieve the current audio session category and options, you may want to use this prior to starting speech recognition
|
|
696
|
+
- `ExpoSpeechRecognitionModule.start({ iosCategory })` to configure the audio session category and mode when speech recognition starts
|
|
697
|
+
- `setAudioCategoryIOS({ category, categoryOptions, mode })` to set the audio session category and mode at a later point in time
|
|
698
|
+
|
|
642
699
|
## API Methods
|
|
643
700
|
|
|
644
701
|
### `start(options: SpeechRecognitionOptions): void`
|
|
@@ -689,9 +746,9 @@ For iOS, once a user has granted (or denied) location permissions by responding
|
|
|
689
746
|
the only way that the permissions can be changed is by the user themselves using the device settings app.
|
|
690
747
|
|
|
691
748
|
```ts
|
|
692
|
-
import {
|
|
749
|
+
import { ExpoSpeechRecognitionModule } from "expo-speech-recognition";
|
|
693
750
|
|
|
694
|
-
requestPermissionsAsync().then((result) => {
|
|
751
|
+
ExpoSpeechRecognitionModule.requestPermissionsAsync().then((result) => {
|
|
695
752
|
console.log("Status:", result.status); // "granted" | "denied" | "not-determined"
|
|
696
753
|
console.log("Granted:", result.granted); // true | false
|
|
697
754
|
console.log("Can ask again:", result.canAskAgain); // true | false
|
|
@@ -704,9 +761,9 @@ requestPermissionsAsync().then((result) => {
|
|
|
704
761
|
Returns the current permission status for the microphone and speech recognition.
|
|
705
762
|
|
|
706
763
|
```ts
|
|
707
|
-
import {
|
|
764
|
+
import { ExpoSpeechRecognitionModule } from "expo-speech-recognition";
|
|
708
765
|
|
|
709
|
-
getPermissionsAsync().then((result) => {
|
|
766
|
+
ExpoSpeechRecognitionModule.getPermissionsAsync().then((result) => {
|
|
710
767
|
console.log("Status:", result.status); // "granted" | "denied" | "not-determined"
|
|
711
768
|
console.log("Granted:", result.granted); // true | false
|
|
712
769
|
console.log("Can ask again:", result.canAskAgain); // true | false
|
|
@@ -719,22 +776,25 @@ getPermissionsAsync().then((result) => {
|
|
|
719
776
|
Returns the current internal state of the speech recognizer.
|
|
720
777
|
|
|
721
778
|
```ts
|
|
722
|
-
import {
|
|
779
|
+
import { ExpoSpeechRecognitionModule } from "expo-speech-recognition";
|
|
723
780
|
|
|
724
781
|
// Note: you probably should rather rely on the events emitted by the SpeechRecognition API instead
|
|
725
|
-
getStateAsync().then((state) => {
|
|
782
|
+
ExpoSpeechRecognitionModule.getStateAsync().then((state) => {
|
|
726
783
|
console.log("Current state:", state);
|
|
727
784
|
// "inactive" | "starting" | "stopping" | "recognizing"
|
|
728
785
|
});
|
|
729
786
|
```
|
|
730
787
|
|
|
731
|
-
### `addSpeechRecognitionListener(
|
|
788
|
+
### `addSpeechRecognitionListener()`
|
|
789
|
+
|
|
790
|
+
Refer to [Speech Recognition Events](#speech-recognition-events) for the list of supported events.
|
|
732
791
|
|
|
733
792
|
```ts
|
|
734
793
|
import { addSpeechRecognitionListener } from "expo-speech-recognition";
|
|
735
794
|
|
|
736
795
|
const listener = addSpeechRecognitionListener("result", (event) => {
|
|
737
|
-
console.log("
|
|
796
|
+
console.log("transcript:", event.results[0]?.transcript);
|
|
797
|
+
console.log("confidence:", event.results[0]?.confidence);
|
|
738
798
|
});
|
|
739
799
|
|
|
740
800
|
// Remove the listener when you're done
|
|
@@ -787,7 +847,7 @@ Get list of speech recognition services available on the device.
|
|
|
787
847
|
```ts
|
|
788
848
|
import { getSpeechRecognitionServices } from "expo-speech-recognition";
|
|
789
849
|
|
|
790
|
-
const packages =
|
|
850
|
+
const packages = getSpeechRecognitionServices();
|
|
791
851
|
console.log("Speech recognition services:", packages.join(", "));
|
|
792
852
|
// e.g. ["com.google.android.as", "com.google.android.tts", "com.samsung.android.bixby.agent"]
|
|
793
853
|
```
|
|
@@ -799,9 +859,10 @@ Returns the default voice recognition service on the device.
|
|
|
799
859
|
```ts
|
|
800
860
|
import { getDefaultRecognitionService } from "expo-speech-recognition";
|
|
801
861
|
|
|
802
|
-
const service =
|
|
862
|
+
const service = getDefaultRecognitionService();
|
|
803
863
|
console.log("Default recognition service:", service.packageName);
|
|
804
|
-
// Usually this is "com.google.android.tts"
|
|
864
|
+
// Usually this is "com.google.android.tts" on Android 13+ and "com.google.android.googlequicksearchbox" on Android <=12.
|
|
865
|
+
// For on-device recognition, "com.google.android.as" will likely be used.
|
|
805
866
|
```
|
|
806
867
|
|
|
807
868
|
### `getAssistantService(): { packageName: string }` (Android only)
|
|
@@ -811,15 +872,30 @@ Returns the default voice assistant service on the device.
|
|
|
811
872
|
```ts
|
|
812
873
|
import { getAssistantService } from "expo-speech-recognition";
|
|
813
874
|
|
|
814
|
-
const service =
|
|
875
|
+
const service = getAssistantService();
|
|
815
876
|
console.log("Default assistant service:", service.packageName);
|
|
816
877
|
// Usually "com.google.android.googlequicksearchbox" for Google
|
|
817
878
|
// or "com.samsung.android.bixby.agent" for Samsung
|
|
818
879
|
```
|
|
819
880
|
|
|
881
|
+
### `isRecognitionAvailable(): boolean`
|
|
882
|
+
|
|
883
|
+
Whether speech recognition is currently available on the device.
|
|
884
|
+
|
|
885
|
+
If this method returns false, calling `start()` will fail and emit an error event with the code `service-not-allowed` or `language-not-supported`. You should also ask the user to enable speech recognition in the system settings (i.e, for iOS to enable Siri & Dictation). On Android, you should ask the user to install and enable `com.google.android.tts` (Android 13+) or `com.google.android.googlequicksearchbox` (Android <= 12) as a default voice recognition service.
|
|
886
|
+
|
|
887
|
+
For Web, this method only checks if the browser has the Web SpeechRecognition API available, however keep in mind that browsers (like Brave) may still have the APIs but not have it implemented yet. Refer to [Platform Compatibility Table](#platform-compatibility-table) for more information. You may want to use a user agent parser to fill in the gaps.
|
|
888
|
+
|
|
889
|
+
```ts
|
|
890
|
+
import { isRecognitionAvailable } from "expo-speech-recognition";
|
|
891
|
+
|
|
892
|
+
const available = isRecognitionAvailable();
|
|
893
|
+
console.log("Speech recognition available:", available);
|
|
894
|
+
```
|
|
895
|
+
|
|
820
896
|
### `supportsOnDeviceRecognition(): boolean`
|
|
821
897
|
|
|
822
|
-
Whether on-device speech recognition
|
|
898
|
+
Whether the device supports on-device speech recognition.
|
|
823
899
|
|
|
824
900
|
```ts
|
|
825
901
|
import { supportsOnDeviceRecognition } from "expo-speech-recognition";
|
|
@@ -828,7 +904,7 @@ const available = supportsOnDeviceRecognition();
|
|
|
828
904
|
console.log("OnDevice recognition available:", available);
|
|
829
905
|
```
|
|
830
906
|
|
|
831
|
-
### `supportsRecording(): boolean`
|
|
907
|
+
### `supportsRecording(): boolean`
|
|
832
908
|
|
|
833
909
|
Whether audio recording is supported during speech recognition. This mostly applies to Android devices, to check if it's at least Android 13.
|
|
834
910
|
|
|
@@ -848,10 +924,10 @@ You can see which locales are supported and installed on your device by running
|
|
|
848
924
|
To download the offline model for a specific locale, use the `androidTriggerOfflineModelDownload` function.
|
|
849
925
|
|
|
850
926
|
```ts
|
|
851
|
-
import {
|
|
927
|
+
import { androidTriggerOfflineModelDownload } from "expo-speech-recognition";
|
|
852
928
|
|
|
853
929
|
// Download the offline model for the specified locale
|
|
854
|
-
|
|
930
|
+
androidTriggerOfflineModelDownload({
|
|
855
931
|
locale: "en-US",
|
|
856
932
|
})
|
|
857
933
|
.then((result) => {
|
|
@@ -183,6 +183,10 @@ class ExpoSpeechRecognitionModule : Module() {
|
|
|
183
183
|
}
|
|
184
184
|
}
|
|
185
185
|
|
|
186
|
+
Function("isRecognitionAvailable") {
|
|
187
|
+
SpeechRecognizer.isRecognitionAvailable(appContext.reactContext!!)
|
|
188
|
+
}
|
|
189
|
+
|
|
186
190
|
Function("supportsRecording") {
|
|
187
191
|
Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU
|
|
188
192
|
}
|
|
@@ -49,8 +49,10 @@ class ExpoSpeechService(
|
|
|
49
49
|
) : RecognitionListener {
|
|
50
50
|
private var speech: SpeechRecognizer? = null
|
|
51
51
|
private val mainHandler = Handler(Looper.getMainLooper())
|
|
52
|
+
|
|
52
53
|
/** Audio recorder for persisting audio */
|
|
53
54
|
private var audioRecorder: ExpoAudioRecorder? = null
|
|
55
|
+
|
|
54
56
|
/** File streamer for file-based recognition */
|
|
55
57
|
private var delayedFileStreamer: DelayedFileStreamer? = null
|
|
56
58
|
private var soundState = SoundState.INACTIVE
|
|
@@ -641,10 +643,13 @@ class ExpoSpeechService(
|
|
|
641
643
|
SpeechRecognizer.ERROR_NETWORK_TIMEOUT -> "network"
|
|
642
644
|
SpeechRecognizer.ERROR_NO_MATCH -> "no-speech"
|
|
643
645
|
SpeechRecognizer.ERROR_SERVER -> "network"
|
|
646
|
+
SpeechRecognizer.ERROR_SERVER_DISCONNECTED -> "network"
|
|
647
|
+
SpeechRecognizer.ERROR_LANGUAGE_UNAVAILABLE -> "language-not-supported"
|
|
648
|
+
SpeechRecognizer.ERROR_LANGUAGE_NOT_SUPPORTED -> "language-not-supported"
|
|
644
649
|
// Extra codes
|
|
645
650
|
SpeechRecognizer.ERROR_SPEECH_TIMEOUT -> "speech-timeout"
|
|
646
651
|
SpeechRecognizer.ERROR_RECOGNIZER_BUSY -> "busy"
|
|
647
|
-
SpeechRecognizer.
|
|
652
|
+
SpeechRecognizer.ERROR_TOO_MANY_REQUESTS -> "busy"
|
|
648
653
|
else -> "unknown"
|
|
649
654
|
}
|
|
650
655
|
|
|
@@ -657,10 +662,14 @@ class ExpoSpeechService(
|
|
|
657
662
|
SpeechRecognizer.ERROR_NETWORK_TIMEOUT -> "Network operation timed out."
|
|
658
663
|
SpeechRecognizer.ERROR_NO_MATCH -> "No speech was detected."
|
|
659
664
|
SpeechRecognizer.ERROR_SERVER -> "Server sent error status."
|
|
665
|
+
SpeechRecognizer.ERROR_SERVER_DISCONNECTED -> "Server disconnected."
|
|
666
|
+
SpeechRecognizer.ERROR_LANGUAGE_UNAVAILABLE -> "Requested language is supported, but not yet downloaded."
|
|
667
|
+
SpeechRecognizer.ERROR_LANGUAGE_NOT_SUPPORTED ->
|
|
668
|
+
"Requested language is not available to be used with the current recognizer."
|
|
660
669
|
// Extra codes/messages
|
|
661
670
|
SpeechRecognizer.ERROR_RECOGNIZER_BUSY -> "RecognitionService busy."
|
|
671
|
+
SpeechRecognizer.ERROR_TOO_MANY_REQUESTS -> "Too many requests."
|
|
662
672
|
SpeechRecognizer.ERROR_SPEECH_TIMEOUT -> "No speech input."
|
|
663
|
-
SpeechRecognizer.ERROR_LANGUAGE_UNAVAILABLE -> "Requested language is supported, but not yet downloaded."
|
|
664
673
|
else -> "Unknown error"
|
|
665
674
|
}
|
|
666
675
|
|
package/app.plugin.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// @ts-check
|
|
1
2
|
const {
|
|
2
3
|
withAndroidManifest,
|
|
3
4
|
AndroidConfig,
|
|
@@ -6,7 +7,7 @@ const {
|
|
|
6
7
|
} = require("expo/config-plugins");
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
|
-
* @type {import("expo/config-plugins").ConfigPlugin<{ packages
|
|
10
|
+
* @type {import("expo/config-plugins").ConfigPlugin<{ packages?: string[] }>}
|
|
10
11
|
*/
|
|
11
12
|
const withAndroidPackageVisibilityFiltering = (config, { packages = [] }) => {
|
|
12
13
|
return withAndroidManifest(config, (config) => {
|
|
@@ -35,22 +36,27 @@ const withAndroidPackageVisibilityFiltering = (config, { packages = [] }) => {
|
|
|
35
36
|
</intent>
|
|
36
37
|
</queries>
|
|
37
38
|
*/
|
|
38
|
-
|
|
39
|
+
|
|
40
|
+
for (const pkg of allPackages) {
|
|
39
41
|
if (
|
|
40
42
|
!config.modResults.manifest.queries.some(
|
|
41
43
|
(query) => query.package?.[0]?.$?.["android:name"] === pkg,
|
|
42
44
|
)
|
|
43
45
|
) {
|
|
44
46
|
config.modResults.manifest.queries.push({
|
|
45
|
-
package: { $: { "android:name": pkg } },
|
|
46
|
-
intent:
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
package: [{ $: { "android:name": pkg } }],
|
|
48
|
+
intent: [
|
|
49
|
+
{
|
|
50
|
+
action: [
|
|
51
|
+
{
|
|
52
|
+
$: { "android:name": "android.speech.RecognitionService" },
|
|
53
|
+
},
|
|
54
|
+
],
|
|
49
55
|
},
|
|
50
|
-
|
|
56
|
+
],
|
|
51
57
|
});
|
|
52
58
|
}
|
|
53
|
-
}
|
|
59
|
+
}
|
|
54
60
|
|
|
55
61
|
return config;
|
|
56
62
|
});
|
|
@@ -61,7 +67,7 @@ const withAndroidPackageVisibilityFiltering = (config, { packages = [] }) => {
|
|
|
61
67
|
* microphonePermission?: string;
|
|
62
68
|
* speechRecognitionPermission?: string;
|
|
63
69
|
* androidSpeechServicePackages?: string[];
|
|
64
|
-
* }>}
|
|
70
|
+
* }|undefined>}
|
|
65
71
|
*/
|
|
66
72
|
const withExpoSpeechRecognition = (config, props) => {
|
|
67
73
|
if (!config.ios) {
|
|
@@ -73,12 +79,12 @@ const withExpoSpeechRecognition = (config, props) => {
|
|
|
73
79
|
}
|
|
74
80
|
|
|
75
81
|
config.ios.infoPlist.NSSpeechRecognitionUsageDescription =
|
|
76
|
-
props
|
|
82
|
+
props?.speechRecognitionPermission ||
|
|
77
83
|
config.ios.infoPlist.NSSpeechRecognitionUsageDescription ||
|
|
78
84
|
"Allow $(PRODUCT_NAME) to use speech recognition.";
|
|
79
85
|
|
|
80
86
|
config.ios.infoPlist.NSMicrophoneUsageDescription =
|
|
81
|
-
props
|
|
87
|
+
props?.microphonePermission ||
|
|
82
88
|
config.ios.infoPlist.NSMicrophoneUsageDescription ||
|
|
83
89
|
"Allow $(PRODUCT_NAME) to use the microphone.";
|
|
84
90
|
|
|
@@ -92,7 +98,7 @@ const withExpoSpeechRecognition = (config, props) => {
|
|
|
92
98
|
[
|
|
93
99
|
withAndroidPackageVisibilityFiltering,
|
|
94
100
|
{
|
|
95
|
-
packages: props
|
|
101
|
+
packages: props?.androidSpeechServicePackages,
|
|
96
102
|
},
|
|
97
103
|
],
|
|
98
104
|
]);
|
|
@@ -1,43 +1,5 @@
|
|
|
1
1
|
import { EventEmitter } from "expo-modules-core";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
abort: () => void;
|
|
5
|
-
requestPermissionsAsync: () => Promise<import("expo-modules-core").PermissionResponse>;
|
|
6
|
-
getPermissionsAsync: () => Promise<import("expo-modules-core").PermissionResponse>;
|
|
7
|
-
getStateAsync: () => Promise<"inactive" | "starting" | "recognizing" | "stopping">;
|
|
8
|
-
getAssistantService: () => {
|
|
9
|
-
packageName: string;
|
|
10
|
-
};
|
|
11
|
-
getDefaultRecognitionService: () => {
|
|
12
|
-
packageName: string;
|
|
13
|
-
};
|
|
14
|
-
getSpeechRecognitionServices: () => string[];
|
|
15
|
-
supportsOnDeviceRecognition: () => boolean;
|
|
16
|
-
supportsRecording: () => boolean;
|
|
17
|
-
start(options: import("./ExpoSpeechRecognitionModule.types").ExpoSpeechRecognitionOptions): void;
|
|
18
|
-
getSupportedLocales(options: {
|
|
19
|
-
androidRecognitionServicePackage?: string;
|
|
20
|
-
}): Promise<{
|
|
21
|
-
locales: string[];
|
|
22
|
-
installedLocales: string[];
|
|
23
|
-
}>;
|
|
24
|
-
androidTriggerOfflineModelDownload(options: {
|
|
25
|
-
locale: string;
|
|
26
|
-
}): Promise<{
|
|
27
|
-
status: "download_success" | "opened_dialog" | "download_canceled";
|
|
28
|
-
message: string;
|
|
29
|
-
}>;
|
|
30
|
-
setCategoryIOS(options: import("./ExpoSpeechRecognitionModule.types").SetCategoryOptions): void;
|
|
31
|
-
getAudioSessionCategoryAndOptionsIOS(): {
|
|
32
|
-
category: import("./ExpoSpeechRecognitionModule.types").AVAudioSessionCategoryValue;
|
|
33
|
-
categoryOptions: import("./ExpoSpeechRecognitionModule.types").AVAudioSessionCategoryOptionsValue[];
|
|
34
|
-
mode: import("./ExpoSpeechRecognitionModule.types").AVAudioSessionModeValue;
|
|
35
|
-
};
|
|
36
|
-
setAudioSessionActiveIOS(value: boolean, options?: {
|
|
37
|
-
notifyOthersOnDeactivation: boolean;
|
|
38
|
-
}): void;
|
|
39
|
-
addListener: (eventType: string) => void;
|
|
40
|
-
removeListeners: (count: number) => void;
|
|
41
|
-
};
|
|
2
|
+
import type { ExpoSpeechRecognitionModuleType } from "./ExpoSpeechRecognitionModule.types";
|
|
3
|
+
export declare const ExpoSpeechRecognitionModule: ExpoSpeechRecognitionModuleType;
|
|
42
4
|
export declare const ExpoSpeechRecognitionModuleEmitter: EventEmitter;
|
|
43
5
|
//# sourceMappingURL=ExpoSpeechRecognitionModule.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ExpoSpeechRecognitionModule.d.ts","sourceRoot":"","sources":["../src/ExpoSpeechRecognitionModule.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EAGb,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"ExpoSpeechRecognitionModule.d.ts","sourceRoot":"","sources":["../src/ExpoSpeechRecognitionModule.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EAGb,MAAM,mBAAmB,CAAC;AAE3B,OAAO,KAAK,EAAE,+BAA+B,EAAE,MAAM,qCAAqC,CAAC;AAO3F,eAAO,MAAM,2BAA2B,EAAE,+BAsBzC,CAAC;AAEF,eAAO,MAAM,kCAAkC,cAE9C,CAAC"}
|
|
@@ -15,6 +15,7 @@ export const ExpoSpeechRecognitionModule = {
|
|
|
15
15
|
getSpeechRecognitionServices: () => ExpoSpeechRecognitionNativeModule.getSpeechRecognitionServices(),
|
|
16
16
|
supportsOnDeviceRecognition: () => ExpoSpeechRecognitionNativeModule.supportsOnDeviceRecognition(),
|
|
17
17
|
supportsRecording: () => ExpoSpeechRecognitionNativeModule.supportsRecording(),
|
|
18
|
+
isRecognitionAvailable: () => ExpoSpeechRecognitionNativeModule.isRecognitionAvailable(),
|
|
18
19
|
};
|
|
19
20
|
export const ExpoSpeechRecognitionModuleEmitter = new EventEmitter(ExpoSpeechRecognitionNativeModule ?? NativeModulesProxy.ExpoSpeechRecognition);
|
|
20
21
|
//# sourceMappingURL=ExpoSpeechRecognitionModule.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ExpoSpeechRecognitionModule.js","sourceRoot":"","sources":["../src/ExpoSpeechRecognitionModule.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,kBAAkB,EAClB,mBAAmB,GACpB,MAAM,mBAAmB,CAAC;AAI3B,kEAAkE;AAClE,4EAA4E;AAC5E,MAAM,iCAAiC,GACrC,mBAAmB,CAAkC,uBAAuB,CAAC,CAAC;AAEhF,MAAM,CAAC,MAAM,2BAA2B,
|
|
1
|
+
{"version":3,"file":"ExpoSpeechRecognitionModule.js","sourceRoot":"","sources":["../src/ExpoSpeechRecognitionModule.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,kBAAkB,EAClB,mBAAmB,GACpB,MAAM,mBAAmB,CAAC;AAI3B,kEAAkE;AAClE,4EAA4E;AAC5E,MAAM,iCAAiC,GACrC,mBAAmB,CAAkC,uBAAuB,CAAC,CAAC;AAEhF,MAAM,CAAC,MAAM,2BAA2B,GAAoC;IAC1E,GAAG,iCAAiC;IACpC,6DAA6D;IAC7D,IAAI,EAAE,GAAG,EAAE,CAAC,iCAAiC,CAAC,IAAI,EAAE;IACpD,KAAK,EAAE,GAAG,EAAE,CAAC,iCAAiC,CAAC,KAAK,EAAE;IACtD,uBAAuB,EAAE,GAAG,EAAE,CAC5B,iCAAiC,CAAC,uBAAuB,EAAE;IAC7D,mBAAmB,EAAE,GAAG,EAAE,CACxB,iCAAiC,CAAC,mBAAmB,EAAE;IACzD,aAAa,EAAE,GAAG,EAAE,CAAC,iCAAiC,CAAC,aAAa,EAAE;IACtE,mBAAmB,EAAE,GAAG,EAAE,CACxB,iCAAiC,CAAC,mBAAmB,EAAE;IACzD,4BAA4B,EAAE,GAAG,EAAE,CACjC,iCAAiC,CAAC,4BAA4B,EAAE;IAClE,4BAA4B,EAAE,GAAG,EAAE,CACjC,iCAAiC,CAAC,4BAA4B,EAAE;IAClE,2BAA2B,EAAE,GAAG,EAAE,CAChC,iCAAiC,CAAC,2BAA2B,EAAE;IACjE,iBAAiB,EAAE,GAAG,EAAE,CACtB,iCAAiC,CAAC,iBAAiB,EAAE;IACvD,sBAAsB,EAAE,GAAG,EAAE,CAC3B,iCAAiC,CAAC,sBAAsB,EAAE;CAC7D,CAAC;AAEF,MAAM,CAAC,MAAM,kCAAkC,GAAG,IAAI,YAAY,CAChE,iCAAiC,IAAI,kBAAkB,CAAC,qBAAqB,CAC9E,CAAC","sourcesContent":["import {\n EventEmitter,\n NativeModulesProxy,\n requireNativeModule,\n} from \"expo-modules-core\";\n\nimport type { ExpoSpeechRecognitionModuleType } from \"./ExpoSpeechRecognitionModule.types\";\n\n// It loads the native module object from the JSI or falls back to\n// the bridge module (from NativeModulesProxy) if the remote debugger is on.\nconst ExpoSpeechRecognitionNativeModule =\n requireNativeModule<ExpoSpeechRecognitionModuleType>(\"ExpoSpeechRecognition\");\n\nexport const ExpoSpeechRecognitionModule: ExpoSpeechRecognitionModuleType = {\n ...ExpoSpeechRecognitionNativeModule,\n // Avoid any function bindings when calling the native module\n stop: () => ExpoSpeechRecognitionNativeModule.stop(),\n abort: () => ExpoSpeechRecognitionNativeModule.abort(),\n requestPermissionsAsync: () =>\n ExpoSpeechRecognitionNativeModule.requestPermissionsAsync(),\n getPermissionsAsync: () =>\n ExpoSpeechRecognitionNativeModule.getPermissionsAsync(),\n getStateAsync: () => ExpoSpeechRecognitionNativeModule.getStateAsync(),\n getAssistantService: () =>\n ExpoSpeechRecognitionNativeModule.getAssistantService(),\n getDefaultRecognitionService: () =>\n ExpoSpeechRecognitionNativeModule.getDefaultRecognitionService(),\n getSpeechRecognitionServices: () =>\n ExpoSpeechRecognitionNativeModule.getSpeechRecognitionServices(),\n supportsOnDeviceRecognition: () =>\n ExpoSpeechRecognitionNativeModule.supportsOnDeviceRecognition(),\n supportsRecording: () =>\n ExpoSpeechRecognitionNativeModule.supportsRecording(),\n isRecognitionAvailable: () =>\n ExpoSpeechRecognitionNativeModule.isRecognitionAvailable(),\n};\n\nexport const ExpoSpeechRecognitionModuleEmitter = new EventEmitter(\n ExpoSpeechRecognitionNativeModule ?? NativeModulesProxy.ExpoSpeechRecognition,\n);\n"]}
|