Skip to content

add allowForSilence during recording #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 56 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ No further action required.
* [`isListening()`](#islistening)
* [`checkPermissions()`](#checkpermissions)
* [`requestPermissions()`](#requestpermissions)
* [`addListener('endOfSegmentedSession', ...)`](#addlistenerendofsegmentedsession-)
* [`addListener('segmentResults', ...)`](#addlistenersegmentresults-)
* [`addListener('partialResults', ...)`](#addlistenerpartialresults-)
* [`addListener('listeningState', ...)`](#addlistenerlisteningstate-)
* [`removeAllListeners()`](#removealllisteners)
Expand Down Expand Up @@ -203,6 +205,52 @@ Request the speech recognition permission.
--------------------


### addListener('endOfSegmentedSession', ...)

```typescript
addListener(eventName: 'endOfSegmentedSession', listenerFunc: () => void) => Promise<PluginListenerHandle>
```

Called when allowForSilence set to &gt; 0 and segmented session has ended. (Android only)

On Android it doesn't work if popup is true.

| Param | Type |
| ------------------ | ------------------------------------ |
| **`eventName`** | <code>'endOfSegmentedSession'</code> |
| **`listenerFunc`** | <code>() =&gt; void</code> |

**Returns:** <code>Promise&lt;<a href="#pluginlistenerhandle">PluginListenerHandle</a>&gt;</code>

**Since:** 6.0.2

--------------------


### addListener('segmentResults', ...)

```typescript
addListener(eventName: 'segmentResults', listenerFunc: (data: { matches: string[]; }) => void) => Promise<PluginListenerHandle>
```

Called when allowForSilence set to &gt; 0 and segment result received. (Android only)

On Android it doesn't work if popup is true.

Provides segment result.

| Param | Type |
| ------------------ | ------------------------------------------------------ |
| **`eventName`** | <code>'segmentResults'</code> |
| **`listenerFunc`** | <code>(data: { matches: string[]; }) =&gt; void</code> |

**Returns:** <code>Promise&lt;<a href="#pluginlistenerhandle">PluginListenerHandle</a>&gt;</code>

**Since:** 6.0.2

--------------------


### addListener('partialResults', ...)

```typescript
Expand Down Expand Up @@ -265,13 +313,14 @@ Remove all the listeners that are attached to this plugin.

#### UtteranceOptions

| Prop | Type | Description |
| -------------------- | -------------------- | ---------------------------------------------------------------- |
| **`language`** | <code>string</code> | key returned from `getSupportedLanguages()` |
| **`maxResults`** | <code>number</code> | maximum number of results to return (5 is max) |
| **`prompt`** | <code>string</code> | prompt message to display on popup (Android only) |
| **`popup`** | <code>boolean</code> | display popup window when listening for utterance (Android only) |
| **`partialResults`** | <code>boolean</code> | return partial results if found |
| Prop | Type | Description |
| --------------------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| **`language`** | <code>string</code> | key returned from `getSupportedLanguages()` |
| **`maxResults`** | <code>number</code> | maximum number of results to return (5 is max) |
| **`prompt`** | <code>string</code> | prompt message to display on popup (Android only) |
| **`popup`** | <code>boolean</code> | display popup window when listening for utterance (Android only) |
| **`partialResults`** | <code>boolean</code> | return partial results if found |
| **`allowForSilence`** | <code>number</code> | allows milliseconds of silence during recording. Needs number over 0 (Android only) You need to listen to segmentResults to receive the data. On Android it doesn't work if popup is true. |


#### PermissionStatus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public void start(PluginCall call) {
String prompt = call.getString("prompt", null);
boolean partialResults = call.getBoolean("partialResults", false);
boolean popup = call.getBoolean("popup", false);
beginListening(language, maxResults, prompt, partialResults, popup, call);
int allowForSilence = call.getInt("allowForSilence", 0);
beginListening(language, maxResults, prompt, partialResults, popup, call, allowForSilence);
}

@PluginMethod
Expand Down Expand Up @@ -159,7 +160,8 @@ private void beginListening(
String prompt,
final boolean partialResults,
boolean showPopup,
PluginCall call
PluginCall call,
int allowForSilence
) {
Logger.info(getLogTag(), "Beginning to listen for audible speech");

Expand All @@ -171,6 +173,12 @@ private void beginListening(
intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, partialResults);
intent.putExtra("android.speech.extra.DICTATION_MODE", partialResults);

if (allowForSilence > 0) {
intent.putExtra(RecognizerIntent.EXTRA_SEGMENTED_SESSION, RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS);
intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, allowForSilence);
intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS, allowForSilence);
}

if (prompt != null) {
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt);
}
Expand Down Expand Up @@ -332,6 +340,33 @@ public void onPartialResults(Bundle partialResults) {
} catch (Exception ex) {}
}

@Override
public void onSegmentResults(Bundle results) {
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

try {
JSArray jsArray = new JSArray(matches);

if (this.call != null) {
if (!this.partialResults) {
this.call.resolve(new JSObject().put("status", "success").put("matches", jsArray));
} else {
JSObject ret = new JSObject();
ret.put("matches", jsArray);
notifyListeners("segmentResults", ret);
}
}
} catch (Exception ex) {
this.call.resolve(new JSObject().put("status", "error").put("message", ex.getMessage()));
}
}

@Override
public void onEndOfSegmentedSession() {
JSObject ret = new JSObject();
notifyListeners("endOfSegmentedSession", ret);
}

@Override
public void onEvent(int eventType, Bundle params) {}
}
Expand Down
33 changes: 32 additions & 1 deletion src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,30 @@ export interface SpeechRecognitionPlugin {
* @since 5.0.0
*/
requestPermissions(): Promise<PermissionStatus>;
/**
* Called when allowForSilence set to > 0 and segmented session has ended. (Android only)
*
* On Android it doesn't work if popup is true.
*
* @since 6.0.2
*/
addListener(
eventName: 'endOfSegmentedSession',
listenerFunc: () => void,
): Promise<PluginListenerHandle>;
/**
* Called when allowForSilence set to > 0 and segment result received. (Android only)
*
* On Android it doesn't work if popup is true.
*
* Provides segment result.
*
* @since 6.0.2
*/
addListener(
eventName: 'segmentResults',
listenerFunc: (data: { matches: string[] }) => void,
): Promise<PluginListenerHandle>;
/**
* Called when partialResults set to true and result received.
*
Expand All @@ -78,7 +102,6 @@ export interface SpeechRecognitionPlugin {
eventName: 'partialResults',
listenerFunc: (data: { matches: string[] }) => void,
): Promise<PluginListenerHandle>;

/**
* Called when listening state changed.
*
Expand Down Expand Up @@ -117,4 +140,12 @@ export interface UtteranceOptions {
* return partial results if found
*/
partialResults?: boolean;
/**
* allows milliseconds of silence during recording. Needs number over 0 (Android only)
*
* You need to listen to segmentResults to receive the data.
*
* On Android it doesn't work if popup is true.
*/
allowForSilence?: number;
}