Skip to content

Commit

Permalink
feat: map additional Android error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
jamsch committed Sep 30, 2024
1 parent de28c62 commit 9f32539
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
30 changes: 16 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,20 +339,22 @@ useSpeechRecognitionEvent("error", (event) => {
const error: ExpoSpeechRecognitionErrorCode = "audio-capture";
```

The error code is largely based on the Web Speech API error codes.

| Error Code | Description |
| ------------------------ | -------------------------------------------------------------------------- |
| `aborted` | The user called `ExpoSpeechRecognitionModule.abort()` |
| `audio-capture` | Audio recording error. |
| `bad-grammar` | Provided grammar is invalid. (Note: web only) |
| `language-not-supported` | Locale is not supported by the speech recognizer. |
| `network` | Network communication required for completing the recognition failed. |
| `no-speech` | No final speech was detected. |
| `not-allowed` | Permission to use speech recognition or microphone was not granted. |
| `service-not-allowed` | Recognizer is unavailable. |
| `busy` | The recognizer is busy and cannot accept any new recognition requests. |
| `client` | (Android) Unknown error. Corresponds with `SpeechRecognizer.ERROR_CLIENT`. |
The error code is based on the [Web Speech API error codes](https://developer.mozilla.org/en-US/docs/Web/API/SpeechRecognitionErrorEvent/error).

| Error Code | Description |
| ------------------------ | ------------------------------------------------------------------------------- |
| `aborted` | The user called `ExpoSpeechRecognitionModule.abort()` |
| `audio-capture` | Audio recording error. |
| `bad-grammar` | Provided grammar is invalid. (Web only) |
| `language-not-supported` | Locale is not supported by the speech recognizer. |
| `network` | Network communication required for completing the recognition failed. |
| `no-speech` | No final speech was detected. |
| `not-allowed` | Permission to use speech recognition or microphone was not granted. |
| `service-not-allowed` | Recognizer is unavailable. |
| `busy` | The recognizer is busy and cannot accept any new recognition requests. |
| `client` | An unknown client-side error. Corresponds with `SpeechRecognizer.ERROR_CLIENT`. |
| `speech-timeout` | (Android) No speech input. |
| `unknown` | (Android) Unknown error |

## Persisting Audio Recordings

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ class ExpoSpeechService(
) : RecognitionListener {
private var speech: SpeechRecognizer? = null
private val mainHandler = Handler(Looper.getMainLooper())

/** Audio recorder for persisting audio */
private var audioRecorder: ExpoAudioRecorder? = null

/** File streamer for file-based recognition */
private var delayedFileStreamer: DelayedFileStreamer? = null
private var soundState = SoundState.INACTIVE
Expand Down Expand Up @@ -641,10 +643,13 @@ class ExpoSpeechService(
SpeechRecognizer.ERROR_NETWORK_TIMEOUT -> "network"
SpeechRecognizer.ERROR_NO_MATCH -> "no-speech"
SpeechRecognizer.ERROR_SERVER -> "network"
SpeechRecognizer.ERROR_SERVER_DISCONNECTED -> "network"
SpeechRecognizer.ERROR_LANGUAGE_UNAVAILABLE -> "language-not-supported"
SpeechRecognizer.ERROR_LANGUAGE_NOT_SUPPORTED -> "language-not-supported"
// Extra codes
SpeechRecognizer.ERROR_SPEECH_TIMEOUT -> "speech-timeout"
SpeechRecognizer.ERROR_RECOGNIZER_BUSY -> "busy"
SpeechRecognizer.ERROR_LANGUAGE_UNAVAILABLE -> "language-not-supported"
SpeechRecognizer.ERROR_TOO_MANY_REQUESTS -> "busy"
else -> "unknown"
}

Expand All @@ -657,10 +662,14 @@ class ExpoSpeechService(
SpeechRecognizer.ERROR_NETWORK_TIMEOUT -> "Network operation timed out."
SpeechRecognizer.ERROR_NO_MATCH -> "No speech was detected."
SpeechRecognizer.ERROR_SERVER -> "Server sent error status."
SpeechRecognizer.ERROR_SERVER_DISCONNECTED -> "Server disconnected."
SpeechRecognizer.ERROR_LANGUAGE_UNAVAILABLE -> "Requested language is supported, but not yet downloaded."
SpeechRecognizer.ERROR_LANGUAGE_NOT_SUPPORTED ->
"Requested language is not available to be used with the current recognizer."
// Extra codes/messages
SpeechRecognizer.ERROR_RECOGNIZER_BUSY -> "RecognitionService busy."
SpeechRecognizer.ERROR_TOO_MANY_REQUESTS -> "Too many requests."
SpeechRecognizer.ERROR_SPEECH_TIMEOUT -> "No speech input."
SpeechRecognizer.ERROR_LANGUAGE_UNAVAILABLE -> "Requested language is supported, but not yet downloaded."
else -> "Unknown error"
}

Expand Down
6 changes: 5 additions & 1 deletion src/ExpoSpeechRecognitionModule.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ export type ExpoSpeechRecognitionErrorCode =
/** The recognizer is busy and cannot accept any new recognition requests. */
| "busy"
/** (Android only) An unknown client-side error occurred. */
| "client";
| "client"
/** (Android) No speech input. */
| "speech-timeout"
/** (Android) Unknown error */
| "unknown";

export type ExpoSpeechRecognitionErrorEvent = {
error: ExpoSpeechRecognitionErrorCode;
Expand Down
7 changes: 2 additions & 5 deletions src/ExpoWebSpeechRecognition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export class ExpoWebSpeechRecognition implements SpeechRecognition {
androidRecognitionServicePackage: ExpoSpeechRecognitionOptions["androidRecognitionServicePackage"];

// keyed by listener function
#subscriptionMap: Map<Function, Subscription[]> = new Map();
#subscriptionMap: Map<SpeechListener<any>, Subscription[]> = new Map();

start() {
ExpoSpeechRecognitionModule.requestPermissionsAsync().then(() => {
Expand Down Expand Up @@ -354,10 +354,7 @@ export class ExpoWebSpeechRecognition implements SpeechRecognition {

addEventListener<K extends keyof SpeechRecognitionEventMap>(
type: K,
listener: (
this: SpeechRecognition,
ev: SpeechRecognitionEventMap[K],
) => any,
listener: SpeechListener<K>,
options?: boolean | AddEventListenerOptions,
): void {
const once = typeof options === "object" && options.once;
Expand Down

0 comments on commit 9f32539

Please sign in to comment.