capacitor-microphone 0.0.3 → 0.0.7
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 +77 -0
- package/android/src/main/java/com/eddieagvictoria/capacitor_microphone/CapacitorMicrophonePlugin.java +127 -0
- package/dist/docs.json +118 -0
- package/dist/esm/definitions.d.ts +14 -0
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/web.d.ts +8 -7
- package/dist/esm/web.js +49 -3
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +51 -5
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +51 -5
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/capacitor_microphonePlugin/CapacitorMicrophonePlugin.swift +123 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -16,6 +16,10 @@ npx cap sync
|
|
|
16
16
|
* [`checkPermission()`](#checkpermission)
|
|
17
17
|
* [`requestPermission()`](#requestpermission)
|
|
18
18
|
* [`checkRequestPermission()`](#checkrequestpermission)
|
|
19
|
+
* [`startListening(...)`](#startlistening)
|
|
20
|
+
* [`stopListening()`](#stoplistening)
|
|
21
|
+
* [`addListener('partialResult', ...)`](#addlistenerpartialresult-)
|
|
22
|
+
* [`removeAllListeners()`](#removealllisteners)
|
|
19
23
|
* [Interfaces](#interfaces)
|
|
20
24
|
|
|
21
25
|
</docgen-index>
|
|
@@ -56,6 +60,57 @@ checkRequestPermission() => Promise<MicrophonePermissions>
|
|
|
56
60
|
--------------------
|
|
57
61
|
|
|
58
62
|
|
|
63
|
+
### startListening(...)
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
startListening(options?: StartListeningOptions | undefined) => Promise<SpeechResult>
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
| Param | Type |
|
|
70
|
+
| ------------- | ----------------------------------------------------------------------- |
|
|
71
|
+
| **`options`** | <code><a href="#startlisteningoptions">StartListeningOptions</a></code> |
|
|
72
|
+
|
|
73
|
+
**Returns:** <code>Promise<<a href="#speechresult">SpeechResult</a>></code>
|
|
74
|
+
|
|
75
|
+
--------------------
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
### stopListening()
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
stopListening() => Promise<{ stopped: boolean; }>
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
**Returns:** <code>Promise<{ stopped: boolean; }></code>
|
|
85
|
+
|
|
86
|
+
--------------------
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
### addListener('partialResult', ...)
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
addListener(eventName: 'partialResult', listenerFunc: (data: SpeechResult) => void) => Promise<PluginListenerHandle>
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
| Param | Type |
|
|
96
|
+
| ------------------ | ------------------------------------------------------------------------ |
|
|
97
|
+
| **`eventName`** | <code>'partialResult'</code> |
|
|
98
|
+
| **`listenerFunc`** | <code>(data: <a href="#speechresult">SpeechResult</a>) => void</code> |
|
|
99
|
+
|
|
100
|
+
**Returns:** <code>Promise<<a href="#pluginlistenerhandle">PluginListenerHandle</a>></code>
|
|
101
|
+
|
|
102
|
+
--------------------
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
### removeAllListeners()
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
removeAllListeners() => Promise<void>
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
--------------------
|
|
112
|
+
|
|
113
|
+
|
|
59
114
|
### Interfaces
|
|
60
115
|
|
|
61
116
|
|
|
@@ -68,4 +123,26 @@ checkRequestPermission() => Promise<MicrophonePermissions>
|
|
|
68
123
|
| **`status`** | <code>string</code> |
|
|
69
124
|
| **`errorMessage`** | <code>string</code> |
|
|
70
125
|
|
|
126
|
+
|
|
127
|
+
#### SpeechResult
|
|
128
|
+
|
|
129
|
+
| Prop | Type |
|
|
130
|
+
| ------------- | -------------------- |
|
|
131
|
+
| **`text`** | <code>string</code> |
|
|
132
|
+
| **`isFinal`** | <code>boolean</code> |
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
#### StartListeningOptions
|
|
136
|
+
|
|
137
|
+
| Prop | Type |
|
|
138
|
+
| ---------- | ------------------- |
|
|
139
|
+
| **`lang`** | <code>string</code> |
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
#### PluginListenerHandle
|
|
143
|
+
|
|
144
|
+
| Prop | Type |
|
|
145
|
+
| ------------ | ----------------------------------------- |
|
|
146
|
+
| **`remove`** | <code>() => Promise<void></code> |
|
|
147
|
+
|
|
71
148
|
</docgen-api>
|
|
@@ -11,6 +11,14 @@ import com.getcapacitor.annotation.Permission;
|
|
|
11
11
|
import com.getcapacitor.annotation.PermissionCallback;
|
|
12
12
|
import com.getcapacitor.PermissionState;
|
|
13
13
|
|
|
14
|
+
import android.content.Intent;
|
|
15
|
+
import android.os.Bundle;
|
|
16
|
+
import android.speech.RecognitionListener;
|
|
17
|
+
import android.speech.RecognizerIntent;
|
|
18
|
+
import android.speech.SpeechRecognizer;
|
|
19
|
+
|
|
20
|
+
import java.util.ArrayList;
|
|
21
|
+
|
|
14
22
|
@CapacitorPlugin(
|
|
15
23
|
name = "CapacitorMicrophone",
|
|
16
24
|
permissions = {
|
|
@@ -22,6 +30,9 @@ import com.getcapacitor.PermissionState;
|
|
|
22
30
|
)
|
|
23
31
|
public class CapacitorMicrophonePlugin extends Plugin {
|
|
24
32
|
|
|
33
|
+
private SpeechRecognizer speechRecognizer;
|
|
34
|
+
private PluginCall currentCall;
|
|
35
|
+
|
|
25
36
|
@PluginMethod
|
|
26
37
|
public void checkPermission(PluginCall call) {
|
|
27
38
|
PermissionState state = getPermissionState("microphone");
|
|
@@ -89,4 +100,120 @@ public class CapacitorMicrophonePlugin extends Plugin {
|
|
|
89
100
|
}
|
|
90
101
|
|
|
91
102
|
|
|
103
|
+
|
|
104
|
+
@PluginMethod
|
|
105
|
+
public void startListening(PluginCall call) {
|
|
106
|
+
|
|
107
|
+
if (speechRecognizer != null) {
|
|
108
|
+
call.reject("Speech recognition already running");
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (getPermissionState("microphone") != PermissionState.GRANTED) {
|
|
113
|
+
call.reject("Microphone permission not granted");
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (!SpeechRecognizer.isRecognitionAvailable(getContext())) {
|
|
118
|
+
call.reject("Speech recognition not available on this device");
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
currentCall = call;
|
|
123
|
+
|
|
124
|
+
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(getContext());
|
|
125
|
+
|
|
126
|
+
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
|
|
127
|
+
intent.putExtra(
|
|
128
|
+
RecognizerIntent.EXTRA_LANGUAGE_MODEL,
|
|
129
|
+
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
|
|
130
|
+
);
|
|
131
|
+
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "es-MX");
|
|
132
|
+
intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
|
|
133
|
+
|
|
134
|
+
speechRecognizer.setRecognitionListener(new RecognitionListener() {
|
|
135
|
+
|
|
136
|
+
@Override
|
|
137
|
+
public void onResults(Bundle results) {
|
|
138
|
+
ArrayList<String> matches =
|
|
139
|
+
results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
|
|
140
|
+
|
|
141
|
+
JSObject ret = new JSObject();
|
|
142
|
+
ret.put("text", matches != null && !matches.isEmpty() ? matches.get(0) : "");
|
|
143
|
+
ret.put("isFinal", true);
|
|
144
|
+
if (currentCall != null) {
|
|
145
|
+
currentCall.resolve(ret);
|
|
146
|
+
currentCall = null;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
@Override
|
|
151
|
+
public void onPartialResults(Bundle partialResults) {
|
|
152
|
+
ArrayList<String> matches =
|
|
153
|
+
partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
|
|
154
|
+
|
|
155
|
+
if (matches != null && !matches.isEmpty()) {
|
|
156
|
+
JSObject ret = new JSObject();
|
|
157
|
+
ret.put("text", matches.get(0));
|
|
158
|
+
ret.put("isFinal", false);
|
|
159
|
+
|
|
160
|
+
notifyListeners("partialResult", ret);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
@Override public void onError(int error) {
|
|
165
|
+
if (currentCall != null) {
|
|
166
|
+
currentCall.reject(mapError(error));
|
|
167
|
+
|
|
168
|
+
currentCall = null;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Métodos obligatorios (vacíos)
|
|
173
|
+
@Override public void onReadyForSpeech(Bundle params) {}
|
|
174
|
+
@Override public void onBeginningOfSpeech() {}
|
|
175
|
+
@Override public void onRmsChanged(float rmsdB) {}
|
|
176
|
+
@Override public void onBufferReceived(byte[] buffer) {}
|
|
177
|
+
@Override public void onEndOfSpeech() {}
|
|
178
|
+
@Override public void onEvent(int eventType, Bundle params) {}
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
speechRecognizer.startListening(intent);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
@PluginMethod
|
|
185
|
+
public void stopListening(PluginCall call) {
|
|
186
|
+
if (speechRecognizer != null) {
|
|
187
|
+
speechRecognizer.stopListening();
|
|
188
|
+
speechRecognizer.destroy();
|
|
189
|
+
speechRecognizer = null;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
JSObject ret = new JSObject();
|
|
193
|
+
ret.put("stopped", true);
|
|
194
|
+
call.resolve(ret);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
@Override
|
|
198
|
+
protected void handleOnDestroy() {
|
|
199
|
+
if (speechRecognizer != null) {
|
|
200
|
+
speechRecognizer.destroy();
|
|
201
|
+
speechRecognizer = null;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
private String mapError(int error) {
|
|
206
|
+
switch (error) {
|
|
207
|
+
case SpeechRecognizer.ERROR_AUDIO: return "Audio error";
|
|
208
|
+
case SpeechRecognizer.ERROR_NETWORK: return "Network error";
|
|
209
|
+
case SpeechRecognizer.ERROR_NO_MATCH: return "No speech recognized";
|
|
210
|
+
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT: return "Speech timeout";
|
|
211
|
+
default: return "Unknown error";
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
|
|
92
216
|
}
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
|
package/dist/docs.json
CHANGED
|
@@ -40,6 +40,69 @@
|
|
|
40
40
|
"MicrophonePermissions"
|
|
41
41
|
],
|
|
42
42
|
"slug": "checkrequestpermission"
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
"name": "startListening",
|
|
46
|
+
"signature": "(options?: StartListeningOptions | undefined) => Promise<SpeechResult>",
|
|
47
|
+
"parameters": [
|
|
48
|
+
{
|
|
49
|
+
"name": "options",
|
|
50
|
+
"docs": "",
|
|
51
|
+
"type": "StartListeningOptions | undefined"
|
|
52
|
+
}
|
|
53
|
+
],
|
|
54
|
+
"returns": "Promise<SpeechResult>",
|
|
55
|
+
"tags": [],
|
|
56
|
+
"docs": "",
|
|
57
|
+
"complexTypes": [
|
|
58
|
+
"SpeechResult",
|
|
59
|
+
"StartListeningOptions"
|
|
60
|
+
],
|
|
61
|
+
"slug": "startlistening"
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
"name": "stopListening",
|
|
65
|
+
"signature": "() => Promise<{ stopped: boolean; }>",
|
|
66
|
+
"parameters": [],
|
|
67
|
+
"returns": "Promise<{ stopped: boolean; }>",
|
|
68
|
+
"tags": [],
|
|
69
|
+
"docs": "",
|
|
70
|
+
"complexTypes": [],
|
|
71
|
+
"slug": "stoplistening"
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
"name": "addListener",
|
|
75
|
+
"signature": "(eventName: 'partialResult', listenerFunc: (data: SpeechResult) => void) => Promise<PluginListenerHandle>",
|
|
76
|
+
"parameters": [
|
|
77
|
+
{
|
|
78
|
+
"name": "eventName",
|
|
79
|
+
"docs": "",
|
|
80
|
+
"type": "'partialResult'"
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
"name": "listenerFunc",
|
|
84
|
+
"docs": "",
|
|
85
|
+
"type": "(data: SpeechResult) => void"
|
|
86
|
+
}
|
|
87
|
+
],
|
|
88
|
+
"returns": "Promise<PluginListenerHandle>",
|
|
89
|
+
"tags": [],
|
|
90
|
+
"docs": "",
|
|
91
|
+
"complexTypes": [
|
|
92
|
+
"PluginListenerHandle",
|
|
93
|
+
"SpeechResult"
|
|
94
|
+
],
|
|
95
|
+
"slug": "addlistenerpartialresult-"
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
"name": "removeAllListeners",
|
|
99
|
+
"signature": "() => Promise<void>",
|
|
100
|
+
"parameters": [],
|
|
101
|
+
"returns": "Promise<void>",
|
|
102
|
+
"tags": [],
|
|
103
|
+
"docs": "",
|
|
104
|
+
"complexTypes": [],
|
|
105
|
+
"slug": "removealllisteners"
|
|
43
106
|
}
|
|
44
107
|
],
|
|
45
108
|
"properties": []
|
|
@@ -81,6 +144,61 @@
|
|
|
81
144
|
"type": "string"
|
|
82
145
|
}
|
|
83
146
|
]
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
"name": "SpeechResult",
|
|
150
|
+
"slug": "speechresult",
|
|
151
|
+
"docs": "",
|
|
152
|
+
"tags": [],
|
|
153
|
+
"methods": [],
|
|
154
|
+
"properties": [
|
|
155
|
+
{
|
|
156
|
+
"name": "text",
|
|
157
|
+
"tags": [],
|
|
158
|
+
"docs": "",
|
|
159
|
+
"complexTypes": [],
|
|
160
|
+
"type": "string"
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
"name": "isFinal",
|
|
164
|
+
"tags": [],
|
|
165
|
+
"docs": "",
|
|
166
|
+
"complexTypes": [],
|
|
167
|
+
"type": "boolean"
|
|
168
|
+
}
|
|
169
|
+
]
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
"name": "StartListeningOptions",
|
|
173
|
+
"slug": "startlisteningoptions",
|
|
174
|
+
"docs": "",
|
|
175
|
+
"tags": [],
|
|
176
|
+
"methods": [],
|
|
177
|
+
"properties": [
|
|
178
|
+
{
|
|
179
|
+
"name": "lang",
|
|
180
|
+
"tags": [],
|
|
181
|
+
"docs": "",
|
|
182
|
+
"complexTypes": [],
|
|
183
|
+
"type": "string | undefined"
|
|
184
|
+
}
|
|
185
|
+
]
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
"name": "PluginListenerHandle",
|
|
189
|
+
"slug": "pluginlistenerhandle",
|
|
190
|
+
"docs": "",
|
|
191
|
+
"tags": [],
|
|
192
|
+
"methods": [],
|
|
193
|
+
"properties": [
|
|
194
|
+
{
|
|
195
|
+
"name": "remove",
|
|
196
|
+
"tags": [],
|
|
197
|
+
"docs": "",
|
|
198
|
+
"complexTypes": [],
|
|
199
|
+
"type": "() => Promise<void>"
|
|
200
|
+
}
|
|
201
|
+
]
|
|
84
202
|
}
|
|
85
203
|
],
|
|
86
204
|
"enums": [],
|
|
@@ -1,7 +1,14 @@
|
|
|
1
|
+
import type { PluginListenerHandle } from '@capacitor/core';
|
|
1
2
|
export interface CapacitorMicrophonePlugin {
|
|
2
3
|
checkPermission(): Promise<MicrophonePermissions>;
|
|
3
4
|
requestPermission(): Promise<MicrophonePermissions>;
|
|
4
5
|
checkRequestPermission(): Promise<MicrophonePermissions>;
|
|
6
|
+
startListening(options?: StartListeningOptions): Promise<SpeechResult>;
|
|
7
|
+
stopListening(): Promise<{
|
|
8
|
+
stopped: boolean;
|
|
9
|
+
}>;
|
|
10
|
+
addListener(eventName: 'partialResult', listenerFunc: (data: SpeechResult) => void): Promise<PluginListenerHandle>;
|
|
11
|
+
removeAllListeners(): Promise<void>;
|
|
5
12
|
}
|
|
6
13
|
export interface MicrophonePermissions {
|
|
7
14
|
details: string;
|
|
@@ -9,3 +16,10 @@ export interface MicrophonePermissions {
|
|
|
9
16
|
status: string;
|
|
10
17
|
errorMessage: string;
|
|
11
18
|
}
|
|
19
|
+
export interface StartListeningOptions {
|
|
20
|
+
lang?: string;
|
|
21
|
+
}
|
|
22
|
+
export interface SpeechResult {
|
|
23
|
+
text: string;
|
|
24
|
+
isFinal: boolean;
|
|
25
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"","sourcesContent":["
|
|
1
|
+
{"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"","sourcesContent":["import type { PluginListenerHandle } from '@capacitor/core';\n\nexport interface CapacitorMicrophonePlugin {\n checkPermission(): Promise<MicrophonePermissions>;\n requestPermission(): Promise<MicrophonePermissions>;\n checkRequestPermission(): Promise<MicrophonePermissions>;\n startListening(options?: StartListeningOptions): Promise<SpeechResult>;\n stopListening(): Promise<{ stopped: boolean }>;\n addListener(eventName: 'partialResult', listenerFunc: (data: SpeechResult) => void\n ): Promise<PluginListenerHandle>;\n removeAllListeners(): Promise<void>;\n}\n\nexport interface MicrophonePermissions {\n details: string;\n granted: boolean;\n status:string;\n errorMessage: string;\n}\n\nexport interface StartListeningOptions {\n lang?: string; //'es-MX', 'en-US'\n}\n\nexport interface SpeechResult {\n text: string;\n isFinal: boolean;\n}\n"]}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { registerPlugin } from '@capacitor/core';
|
|
2
2
|
const capacitor_microphone = registerPlugin('CapacitorMicrophone', {
|
|
3
|
-
web: () => import('./web').then((m) => new m.
|
|
3
|
+
web: () => import('./web').then((m) => new m.CapacitorMicrophoneWeb()),
|
|
4
4
|
});
|
|
5
5
|
export * from './definitions';
|
|
6
6
|
export { capacitor_microphone };
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAIjD,MAAM,oBAAoB,GAAG,cAAc,CAA4B,qBAAqB,EAAE;IAC5F,GAAG,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAIjD,MAAM,oBAAoB,GAAG,cAAc,CAA4B,qBAAqB,EAAE;IAC5F,GAAG,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,sBAAsB,EAAE,CAAC;CACvE,CAAC,CAAC;AAEH,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,oBAAoB,EAAE,CAAC","sourcesContent":["import { registerPlugin } from '@capacitor/core';\n\nimport type { CapacitorMicrophonePlugin } from './definitions';\n\nconst capacitor_microphone = registerPlugin<CapacitorMicrophonePlugin>('CapacitorMicrophone', {\n web: () => import('./web').then((m) => new m.CapacitorMicrophoneWeb()),\n});\n\nexport * from './definitions';\nexport { capacitor_microphone };\n"]}
|
package/dist/esm/web.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { WebPlugin } from '@capacitor/core';
|
|
2
|
-
import type { CapacitorMicrophonePlugin, MicrophonePermissions } from './definitions';
|
|
3
|
-
export declare class
|
|
4
|
-
|
|
5
|
-
value: string;
|
|
6
|
-
}): Promise<{
|
|
7
|
-
value: string;
|
|
8
|
-
}>;
|
|
2
|
+
import type { CapacitorMicrophonePlugin, MicrophonePermissions, SpeechResult, StartListeningOptions } from './definitions';
|
|
3
|
+
export declare class CapacitorMicrophoneWeb extends WebPlugin implements CapacitorMicrophonePlugin {
|
|
4
|
+
private recognition;
|
|
9
5
|
checkPermission(): Promise<MicrophonePermissions>;
|
|
10
6
|
requestPermission(): Promise<MicrophonePermissions>;
|
|
11
7
|
checkRequestPermission(): Promise<MicrophonePermissions>;
|
|
8
|
+
startListening(options?: StartListeningOptions): Promise<SpeechResult>;
|
|
9
|
+
stopListening(): Promise<{
|
|
10
|
+
stopped: boolean;
|
|
11
|
+
}>;
|
|
12
|
+
private stopRecognition;
|
|
12
13
|
}
|
package/dist/esm/web.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { WebPlugin } from '@capacitor/core';
|
|
2
|
-
export class
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
export class CapacitorMicrophoneWeb extends WebPlugin {
|
|
3
|
+
constructor() {
|
|
4
|
+
super(...arguments);
|
|
5
|
+
this.recognition = null;
|
|
5
6
|
}
|
|
6
7
|
async checkPermission() {
|
|
7
8
|
var _a;
|
|
@@ -60,5 +61,50 @@ export class capacitor_microphoneWeb extends WebPlugin {
|
|
|
60
61
|
}
|
|
61
62
|
return this.requestPermission();
|
|
62
63
|
}
|
|
64
|
+
async startListening(options) {
|
|
65
|
+
var _a;
|
|
66
|
+
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
|
67
|
+
if (!SpeechRecognition) {
|
|
68
|
+
throw new Error('Speech recognition not supported in this browser');
|
|
69
|
+
}
|
|
70
|
+
if (this.recognition) {
|
|
71
|
+
throw new Error('Speech recognition already running');
|
|
72
|
+
}
|
|
73
|
+
const lang = (_a = options === null || options === void 0 ? void 0 : options.lang) !== null && _a !== void 0 ? _a : 'es-MX';
|
|
74
|
+
this.recognition = new SpeechRecognition();
|
|
75
|
+
this.recognition.lang = lang;
|
|
76
|
+
this.recognition.interimResults = true;
|
|
77
|
+
this.recognition.continuous = false;
|
|
78
|
+
return new Promise((resolve, reject) => {
|
|
79
|
+
this.recognition.onresult = (event) => {
|
|
80
|
+
const result = event.results[event.results.length - 1];
|
|
81
|
+
const text = result[0].transcript;
|
|
82
|
+
const isFinal = result.isFinal;
|
|
83
|
+
this.notifyListeners('partialResult', {
|
|
84
|
+
text,
|
|
85
|
+
isFinal,
|
|
86
|
+
});
|
|
87
|
+
if (isFinal) {
|
|
88
|
+
resolve({ text, isFinal: true });
|
|
89
|
+
this.stopRecognition();
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
this.recognition.onerror = (event) => {
|
|
93
|
+
reject(event.error || 'Speech recognition error');
|
|
94
|
+
this.stopRecognition();
|
|
95
|
+
};
|
|
96
|
+
this.recognition.start();
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
async stopListening() {
|
|
100
|
+
this.stopRecognition();
|
|
101
|
+
return { stopped: true };
|
|
102
|
+
}
|
|
103
|
+
stopRecognition() {
|
|
104
|
+
if (this.recognition) {
|
|
105
|
+
this.recognition.stop();
|
|
106
|
+
this.recognition = null;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
63
109
|
}
|
|
64
110
|
//# sourceMappingURL=web.js.map
|
package/dist/esm/web.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"web.js","sourceRoot":"","sources":["../../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAI5C,MAAM,OAAO,
|
|
1
|
+
{"version":3,"file":"web.js","sourceRoot":"","sources":["../../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAI5C,MAAM,OAAO,sBAAuB,SAAQ,SAAS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CAAC,CAAC,UAAU,CAAC;gBAClC,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;gBAE/B,IAAI,CAAC,eAAe,CAAC,eAAe,EAAE;oBACpC,IAAI;oBACJ,OAAO;iBACR,CAAC,CAAC;gBAEH,IAAI,OAAO,EAAE,CAAC;oBACZ,OAAO,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;oBACjC,IAAI,CAAC,eAAe,EAAE,CAAC;gBACzB,CAAC;YACH,CAAC,CAAC;YAEF,IAAI,CAAC,WAAY,CAAC,OAAO,GAAG,CAAC,KAAU,EAAE,EAAE;gBACzC,MAAM,CAAC,KAAK,CAAC,KAAK,IAAI,0BAA0B,CAAC,CAAC;gBAClD,IAAI,CAAC,eAAe,EAAE,CAAC;YACzB,CAAC,CAAC;YAEF,IAAI,CAAC,WAAY,CAAC,KAAK,EAAE,CAAC;QAC5B,CAAC,CAAC,CAAC;IACL,CAAC;IAED,KAAK,CAAC,aAAa;QACjB,IAAI,CAAC,eAAe,EAAE,CAAC;QACvB,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;IAC3B,CAAC;IAEO,eAAe;QACrB,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC;YACrB,IAAI,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC;YACxB,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC;QAC1B,CAAC;IACH,CAAC;CACF","sourcesContent":["import { WebPlugin } from '@capacitor/core';\n\nimport type { CapacitorMicrophonePlugin, MicrophonePermissions, SpeechResult, StartListeningOptions } from './definitions';\n\nexport class CapacitorMicrophoneWeb extends WebPlugin implements CapacitorMicrophonePlugin {\n\n private recognition: any = null;\n\n async checkPermission(): Promise<MicrophonePermissions> {\n if (!navigator.permissions) {\n return {\n granted: false,\n status: 'prompt',\n details: 'Permissions API not supported',\n errorMessage: '',\n };\n }\n\n try {\n const result = await navigator.permissions.query({\n name: 'microphone' as any,\n });\n\n return {\n granted: result.state === 'granted',\n status: result.state as 'granted' | 'denied' | 'prompt',\n details: 'Web checkPermission',\n errorMessage: '',\n };\n } catch (err: any) {\n return {\n granted: false,\n status: 'denied',\n details: 'Web checkPermission error',\n errorMessage: err?.message ?? 'Unknown error',\n };\n }\n }\n\n async requestPermission(): Promise<MicrophonePermissions> {\n try {\n await navigator.mediaDevices.getUserMedia({ audio: true });\n\n return {\n granted: true,\n status: 'granted',\n details: 'Web requestPermission',\n errorMessage: '',\n };\n } catch (err: any) {\n return {\n granted: false,\n status: 'denied',\n details: 'Web requestPermission',\n errorMessage: err?.message ?? 'User denied microphone permission',\n };\n }\n }\n\n async checkRequestPermission(): Promise<MicrophonePermissions> {\n const permission = await this.checkPermission();\n\n if (permission.granted) {\n return permission;\n }\n\n return this.requestPermission();\n }\n\n async startListening(options?: StartListeningOptions): Promise<SpeechResult> {\n const SpeechRecognition = (window as any).SpeechRecognition || (window as any).webkitSpeechRecognition;\n\n if (!SpeechRecognition) {\n throw new Error('Speech recognition not supported in this browser');\n }\n\n if (this.recognition) {\n throw new Error('Speech recognition already running');\n }\n\n const lang = options?.lang ?? 'es-MX';\n\n this.recognition = new SpeechRecognition();\n this.recognition.lang = lang;\n this.recognition.interimResults = true;\n this.recognition.continuous = false;\n\n return new Promise((resolve, reject) => {\n this.recognition!.onresult = (event: any) => {\n const result = event.results[event.results.length - 1];\n const text = result[0].transcript;\n const isFinal = result.isFinal;\n\n this.notifyListeners('partialResult', {\n text,\n isFinal,\n });\n\n if (isFinal) {\n resolve({ text, isFinal: true });\n this.stopRecognition();\n }\n };\n\n this.recognition!.onerror = (event: any) => {\n reject(event.error || 'Speech recognition error');\n this.stopRecognition();\n };\n\n this.recognition!.start();\n });\n }\n\n async stopListening(): Promise<{ stopped: boolean }> {\n this.stopRecognition();\n return { stopped: true };\n }\n\n private stopRecognition() {\n if (this.recognition) {\n this.recognition.stop();\n this.recognition = null;\n }\n }\n}\n\n"]}
|
package/dist/plugin.cjs.js
CHANGED
|
@@ -3,12 +3,13 @@
|
|
|
3
3
|
var core = require('@capacitor/core');
|
|
4
4
|
|
|
5
5
|
const capacitor_microphone = core.registerPlugin('CapacitorMicrophone', {
|
|
6
|
-
web: () => Promise.resolve().then(function () { return web; }).then((m) => new m.
|
|
6
|
+
web: () => Promise.resolve().then(function () { return web; }).then((m) => new m.CapacitorMicrophoneWeb()),
|
|
7
7
|
});
|
|
8
8
|
|
|
9
|
-
class
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
class CapacitorMicrophoneWeb extends core.WebPlugin {
|
|
10
|
+
constructor() {
|
|
11
|
+
super(...arguments);
|
|
12
|
+
this.recognition = null;
|
|
12
13
|
}
|
|
13
14
|
async checkPermission() {
|
|
14
15
|
var _a;
|
|
@@ -67,11 +68,56 @@ class capacitor_microphoneWeb extends core.WebPlugin {
|
|
|
67
68
|
}
|
|
68
69
|
return this.requestPermission();
|
|
69
70
|
}
|
|
71
|
+
async startListening(options) {
|
|
72
|
+
var _a;
|
|
73
|
+
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
|
74
|
+
if (!SpeechRecognition) {
|
|
75
|
+
throw new Error('Speech recognition not supported in this browser');
|
|
76
|
+
}
|
|
77
|
+
if (this.recognition) {
|
|
78
|
+
throw new Error('Speech recognition already running');
|
|
79
|
+
}
|
|
80
|
+
const lang = (_a = options === null || options === void 0 ? void 0 : options.lang) !== null && _a !== void 0 ? _a : 'es-MX';
|
|
81
|
+
this.recognition = new SpeechRecognition();
|
|
82
|
+
this.recognition.lang = lang;
|
|
83
|
+
this.recognition.interimResults = true;
|
|
84
|
+
this.recognition.continuous = false;
|
|
85
|
+
return new Promise((resolve, reject) => {
|
|
86
|
+
this.recognition.onresult = (event) => {
|
|
87
|
+
const result = event.results[event.results.length - 1];
|
|
88
|
+
const text = result[0].transcript;
|
|
89
|
+
const isFinal = result.isFinal;
|
|
90
|
+
this.notifyListeners('partialResult', {
|
|
91
|
+
text,
|
|
92
|
+
isFinal,
|
|
93
|
+
});
|
|
94
|
+
if (isFinal) {
|
|
95
|
+
resolve({ text, isFinal: true });
|
|
96
|
+
this.stopRecognition();
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
this.recognition.onerror = (event) => {
|
|
100
|
+
reject(event.error || 'Speech recognition error');
|
|
101
|
+
this.stopRecognition();
|
|
102
|
+
};
|
|
103
|
+
this.recognition.start();
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
async stopListening() {
|
|
107
|
+
this.stopRecognition();
|
|
108
|
+
return { stopped: true };
|
|
109
|
+
}
|
|
110
|
+
stopRecognition() {
|
|
111
|
+
if (this.recognition) {
|
|
112
|
+
this.recognition.stop();
|
|
113
|
+
this.recognition = null;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
70
116
|
}
|
|
71
117
|
|
|
72
118
|
var web = /*#__PURE__*/Object.freeze({
|
|
73
119
|
__proto__: null,
|
|
74
|
-
|
|
120
|
+
CapacitorMicrophoneWeb: CapacitorMicrophoneWeb
|
|
75
121
|
});
|
|
76
122
|
|
|
77
123
|
exports.capacitor_microphone = capacitor_microphone;
|
package/dist/plugin.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.cjs.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from '@capacitor/core';\nconst capacitor_microphone = registerPlugin('CapacitorMicrophone', {\n web: () => import('./web').then((m) => new m.
|
|
1
|
+
{"version":3,"file":"plugin.cjs.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from '@capacitor/core';\nconst capacitor_microphone = registerPlugin('CapacitorMicrophone', {\n web: () => import('./web').then((m) => new m.CapacitorMicrophoneWeb()),\n});\nexport * from './definitions';\nexport { capacitor_microphone };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\nexport class CapacitorMicrophoneWeb extends WebPlugin {\n constructor() {\n super(...arguments);\n this.recognition = null;\n }\n async checkPermission() {\n var _a;\n if (!navigator.permissions) {\n return {\n granted: false,\n status: 'prompt',\n details: 'Permissions API not supported',\n errorMessage: '',\n };\n }\n try {\n const result = await navigator.permissions.query({\n name: 'microphone',\n });\n return {\n granted: result.state === 'granted',\n status: result.state,\n details: 'Web checkPermission',\n errorMessage: '',\n };\n }\n catch (err) {\n return {\n granted: false,\n status: 'denied',\n details: 'Web checkPermission error',\n errorMessage: (_a = err === null || err === void 0 ? void 0 : err.message) !== null && _a !== void 0 ? _a : 'Unknown error',\n };\n }\n }\n async requestPermission() {\n var _a;\n try {\n await navigator.mediaDevices.getUserMedia({ audio: true });\n return {\n granted: true,\n status: 'granted',\n details: 'Web requestPermission',\n errorMessage: '',\n };\n }\n catch (err) {\n return {\n granted: false,\n status: 'denied',\n details: 'Web requestPermission',\n errorMessage: (_a = err === null || err === void 0 ? void 0 : err.message) !== null && _a !== void 0 ? _a : 'User denied microphone permission',\n };\n }\n }\n async checkRequestPermission() {\n const permission = await this.checkPermission();\n if (permission.granted) {\n return permission;\n }\n return this.requestPermission();\n }\n async startListening(options) {\n var _a;\n const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;\n if (!SpeechRecognition) {\n throw new Error('Speech recognition not supported in this browser');\n }\n if (this.recognition) {\n throw new Error('Speech recognition already running');\n }\n const lang = (_a = options === null || options === void 0 ? void 0 : options.lang) !== null && _a !== void 0 ? _a : 'es-MX';\n this.recognition = new SpeechRecognition();\n this.recognition.lang = lang;\n this.recognition.interimResults = true;\n this.recognition.continuous = false;\n return new Promise((resolve, reject) => {\n this.recognition.onresult = (event) => {\n const result = event.results[event.results.length - 1];\n const text = result[0].transcript;\n const isFinal = result.isFinal;\n this.notifyListeners('partialResult', {\n text,\n isFinal,\n });\n if (isFinal) {\n resolve({ text, isFinal: true });\n this.stopRecognition();\n }\n };\n this.recognition.onerror = (event) => {\n reject(event.error || 'Speech recognition error');\n this.stopRecognition();\n };\n this.recognition.start();\n });\n }\n async stopListening() {\n this.stopRecognition();\n return { stopped: true };\n }\n stopRecognition() {\n if (this.recognition) {\n this.recognition.stop();\n this.recognition = null;\n }\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;;AACK,MAAC,oBAAoB,GAAGA,mBAAc,CAAC,qBAAqB,EAAE;AACnE,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,sBAAsB,EAAE,CAAC;AAC1E,CAAC;;ACFM,MAAM,sBAAsB,SAASC,cAAS,CAAC;AACtD,IAAI,WAAW,GAAG;AAClB,QAAQ,KAAK,CAAC,GAAG,SAAS,CAAC;AAC3B,QAAQ,IAAI,CAAC,WAAW,GAAG,IAAI;AAC/B,IAAI;AACJ,IAAI,MAAM,eAAe,GAAG;AAC5B,QAAQ,IAAI,EAAE;AACd,QAAQ,IAAI,CAAC,SAAS,CAAC,WAAW,EAAE;AACpC,YAAY,OAAO;AACnB,gBAAgB,OAAO,EAAE,KAAK;AAC9B,gBAAgB,MAAM,EAAE,QAAQ;AAChC,gBAAgB,OAAO,EAAE,+BAA+B;AACxD,gBAAgB,YAAY,EAAE,EAAE;AAChC,aAAa;AACb,QAAQ;AACR,QAAQ,IAAI;AACZ,YAAY,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,WAAW,CAAC,KAAK,CAAC;AAC7D,gBAAgB,IAAI,EAAE,YAAY;AAClC,aAAa,CAAC;AACd,YAAY,OAAO;AACnB,gBAAgB,OAAO,EAAE,MAAM,CAAC,KAAK,KAAK,SAAS;AACnD,gBAAgB,MAAM,EAAE,MAAM,CAAC,KAAK;AACpC,gBAAgB,OAAO,EAAE,qBAAqB;AAC9C,gBAAgB,YAAY,EAAE,EAAE;AAChC,aAAa;AACb,QAAQ;AACR,QAAQ,OAAO,GAAG,EAAE;AACpB,YAAY,OAAO;AACnB,gBAAgB,OAAO,EAAE,KAAK;AAC9B,gBAAgB,MAAM,EAAE,QAAQ;AAChC,gB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EAAE;AAC7B,oBAAoB,OAAO,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;AACpD,oBAAoB,IAAI,CAAC,eAAe,EAAE;AAC1C,gBAAgB;AAChB,YAAY,CAAC;AACb,YAAY,IAAI,CAAC,WAAW,CAAC,OAAO,GAAG,CAAC,KAAK,KAAK;AAClD,gBAAgB,MAAM,CAAC,KAAK,CAAC,KAAK,IAAI,0BAA0B,CAAC;AACjE,gBAAgB,IAAI,CAAC,eAAe,EAAE;AACtC,YAAY,CAAC;AACb,YAAY,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE;AACpC,QAAQ,CAAC,CAAC;AACV,IAAI;AACJ,IAAI,MAAM,aAAa,GAAG;AAC1B,QAAQ,IAAI,CAAC,eAAe,EAAE;AAC9B,QAAQ,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE;AAChC,IAAI;AACJ,IAAI,eAAe,GAAG;AACtB,QAAQ,IAAI,IAAI,CAAC,WAAW,EAAE;AAC9B,YAAY,IAAI,CAAC,WAAW,CAAC,IAAI,EAAE;AACnC,YAAY,IAAI,CAAC,WAAW,GAAG,IAAI;AACnC,QAAQ;AACR,IAAI;AACJ;;;;;;;;;"}
|
package/dist/plugin.js
CHANGED
|
@@ -2,12 +2,13 @@ var capacitorcapacitor_microphone = (function (exports, core) {
|
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
4
|
const capacitor_microphone = core.registerPlugin('CapacitorMicrophone', {
|
|
5
|
-
web: () => Promise.resolve().then(function () { return web; }).then((m) => new m.
|
|
5
|
+
web: () => Promise.resolve().then(function () { return web; }).then((m) => new m.CapacitorMicrophoneWeb()),
|
|
6
6
|
});
|
|
7
7
|
|
|
8
|
-
class
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
class CapacitorMicrophoneWeb extends core.WebPlugin {
|
|
9
|
+
constructor() {
|
|
10
|
+
super(...arguments);
|
|
11
|
+
this.recognition = null;
|
|
11
12
|
}
|
|
12
13
|
async checkPermission() {
|
|
13
14
|
var _a;
|
|
@@ -66,11 +67,56 @@ var capacitorcapacitor_microphone = (function (exports, core) {
|
|
|
66
67
|
}
|
|
67
68
|
return this.requestPermission();
|
|
68
69
|
}
|
|
70
|
+
async startListening(options) {
|
|
71
|
+
var _a;
|
|
72
|
+
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
|
73
|
+
if (!SpeechRecognition) {
|
|
74
|
+
throw new Error('Speech recognition not supported in this browser');
|
|
75
|
+
}
|
|
76
|
+
if (this.recognition) {
|
|
77
|
+
throw new Error('Speech recognition already running');
|
|
78
|
+
}
|
|
79
|
+
const lang = (_a = options === null || options === void 0 ? void 0 : options.lang) !== null && _a !== void 0 ? _a : 'es-MX';
|
|
80
|
+
this.recognition = new SpeechRecognition();
|
|
81
|
+
this.recognition.lang = lang;
|
|
82
|
+
this.recognition.interimResults = true;
|
|
83
|
+
this.recognition.continuous = false;
|
|
84
|
+
return new Promise((resolve, reject) => {
|
|
85
|
+
this.recognition.onresult = (event) => {
|
|
86
|
+
const result = event.results[event.results.length - 1];
|
|
87
|
+
const text = result[0].transcript;
|
|
88
|
+
const isFinal = result.isFinal;
|
|
89
|
+
this.notifyListeners('partialResult', {
|
|
90
|
+
text,
|
|
91
|
+
isFinal,
|
|
92
|
+
});
|
|
93
|
+
if (isFinal) {
|
|
94
|
+
resolve({ text, isFinal: true });
|
|
95
|
+
this.stopRecognition();
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
this.recognition.onerror = (event) => {
|
|
99
|
+
reject(event.error || 'Speech recognition error');
|
|
100
|
+
this.stopRecognition();
|
|
101
|
+
};
|
|
102
|
+
this.recognition.start();
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
async stopListening() {
|
|
106
|
+
this.stopRecognition();
|
|
107
|
+
return { stopped: true };
|
|
108
|
+
}
|
|
109
|
+
stopRecognition() {
|
|
110
|
+
if (this.recognition) {
|
|
111
|
+
this.recognition.stop();
|
|
112
|
+
this.recognition = null;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
69
115
|
}
|
|
70
116
|
|
|
71
117
|
var web = /*#__PURE__*/Object.freeze({
|
|
72
118
|
__proto__: null,
|
|
73
|
-
|
|
119
|
+
CapacitorMicrophoneWeb: CapacitorMicrophoneWeb
|
|
74
120
|
});
|
|
75
121
|
|
|
76
122
|
exports.capacitor_microphone = capacitor_microphone;
|
package/dist/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from '@capacitor/core';\nconst capacitor_microphone = registerPlugin('CapacitorMicrophone', {\n web: () => import('./web').then((m) => new m.
|
|
1
|
+
{"version":3,"file":"plugin.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from '@capacitor/core';\nconst capacitor_microphone = registerPlugin('CapacitorMicrophone', {\n web: () => import('./web').then((m) => new m.CapacitorMicrophoneWeb()),\n});\nexport * from './definitions';\nexport { capacitor_microphone };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\nexport class CapacitorMicrophoneWeb extends WebPlugin {\n constructor() {\n super(...arguments);\n this.recognition = null;\n }\n async checkPermission() {\n var _a;\n if (!navigator.permissions) {\n return {\n granted: false,\n status: 'prompt',\n details: 'Permissions API not supported',\n errorMessage: '',\n };\n }\n try {\n const result = await navigator.permissions.query({\n name: 'microphone',\n });\n return {\n granted: result.state === 'granted',\n status: result.state,\n details: 'Web checkPermission',\n errorMessage: '',\n };\n }\n catch (err) {\n return {\n granted: false,\n status: 'denied',\n details: 'Web checkPermission error',\n errorMessage: (_a = err === null || err === void 0 ? void 0 : err.message) !== null && _a !== void 0 ? _a : 'Unknown error',\n };\n }\n }\n async requestPermission() {\n var _a;\n try {\n await navigator.mediaDevices.getUserMedia({ audio: true });\n return {\n granted: true,\n status: 'granted',\n details: 'Web requestPermission',\n errorMessage: '',\n };\n }\n catch (err) {\n return {\n granted: false,\n status: 'denied',\n details: 'Web requestPermission',\n errorMessage: (_a = err === null || err === void 0 ? void 0 : err.message) !== null && _a !== void 0 ? _a : 'User denied microphone permission',\n };\n }\n }\n async checkRequestPermission() {\n const permission = await this.checkPermission();\n if (permission.granted) {\n return permission;\n }\n return this.requestPermission();\n }\n async startListening(options) {\n var _a;\n const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;\n if (!SpeechRecognition) {\n throw new Error('Speech recognition not supported in this browser');\n }\n if (this.recognition) {\n throw new Error('Speech recognition already running');\n }\n const lang = (_a = options === null || options === void 0 ? void 0 : options.lang) !== null && _a !== void 0 ? _a : 'es-MX';\n this.recognition = new SpeechRecognition();\n this.recognition.lang = lang;\n this.recognition.interimResults = true;\n this.recognition.continuous = false;\n return new Promise((resolve, reject) => {\n this.recognition.onresult = (event) => {\n const result = event.results[event.results.length - 1];\n const text = result[0].transcript;\n const isFinal = result.isFinal;\n this.notifyListeners('partialResult', {\n text,\n isFinal,\n });\n if (isFinal) {\n resolve({ text, isFinal: true });\n this.stopRecognition();\n }\n };\n this.recognition.onerror = (event) => {\n reject(event.error || 'Speech recognition error');\n this.stopRecognition();\n };\n this.recognition.start();\n });\n }\n async stopListening() {\n this.stopRecognition();\n return { stopped: true };\n }\n stopRecognition() {\n if (this.recognition) {\n this.recognition.stop();\n this.recognition = null;\n }\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;AACK,UAAC,oBAAoB,GAAGA,mBAAc,CAAC,qBAAqB,EAAE;IACnE,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,sBAAsB,EAAE,CAAC;IAC1E,CAAC;;ICFM,MAAM,sBAAsB,SAASC,cAAS,CAAC;IACtD,IAAI,WAAW,GAAG;IAClB,QAAQ,KAAK,CAAC,GAAG,SAAS,CAAC;IAC3B,QAAQ,IAAI,CAAC,WAAW,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AAG;IAC1B,QAAQ,IAAI,CAAC,eAAe,EAAE;IAC9B,QAAQ,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE;IAChC,IAAI;IACJ,IAAI,eAAe,GAAG;IACtB,QAAQ,IAAI,IAAI,CAAC,WAAW,EAAE;IAC9B,YAAY,IAAI,CAAC,WAAW,CAAC,IAAI,EAAE;IACnC,YAAY,IAAI,CAAC,WAAW,GAAG,IAAI;IACnC,QAAQ;IACR,IAAI;IACJ;;;;;;;;;;;;;;;"}
|
|
@@ -1,17 +1,27 @@
|
|
|
1
1
|
import Foundation
|
|
2
2
|
import Capacitor
|
|
3
3
|
import AVFoundation
|
|
4
|
+
import Speech
|
|
5
|
+
|
|
4
6
|
|
|
5
7
|
@objc(CapacitorMicrophonePlugin)
|
|
6
8
|
public class CapacitorMicrophonePlugin: CAPPlugin, CAPBridgedPlugin {
|
|
7
9
|
|
|
10
|
+
private let audioEngine = AVAudioEngine()
|
|
11
|
+
private var recognitionRequest: SFSpeechAudioBufferRecognitionRequest?
|
|
12
|
+
private var recognitionTask: SFSpeechRecognitionTask?
|
|
13
|
+
private var speechRecognizer: SFSpeechRecognizer?
|
|
14
|
+
private var currentCall: CAPPluginCall?
|
|
15
|
+
|
|
8
16
|
public let identifier = "CapacitorMicrophonePlugin"
|
|
9
17
|
public let jsName = "CapacitorMicrophone"
|
|
10
18
|
|
|
11
19
|
public let pluginMethods: [CAPPluginMethod] = [
|
|
12
20
|
CAPPluginMethod(name: "checkPermission", returnType: CAPPluginReturnPromise),
|
|
13
21
|
CAPPluginMethod(name: "requestPermission", returnType: CAPPluginReturnPromise),
|
|
14
|
-
CAPPluginMethod(name: "checkRequestPermission", returnType: CAPPluginReturnPromise)
|
|
22
|
+
CAPPluginMethod(name: "checkRequestPermission", returnType: CAPPluginReturnPromise),
|
|
23
|
+
CAPPluginMethod(name: "startListening", returnType: CAPPluginReturnPromise),
|
|
24
|
+
CAPPluginMethod(name: "stopListening", returnType: CAPPluginReturnPromise)
|
|
15
25
|
]
|
|
16
26
|
|
|
17
27
|
@objc func checkPermission(_ call: CAPPluginCall) {
|
|
@@ -71,4 +81,116 @@ public class CapacitorMicrophonePlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
71
81
|
])
|
|
72
82
|
}
|
|
73
83
|
}
|
|
84
|
+
|
|
85
|
+
@objc func startListening(_ call: CAPPluginCall) {
|
|
86
|
+
|
|
87
|
+
if audioEngine.isRunning {
|
|
88
|
+
call.reject("Speech recognition already running")
|
|
89
|
+
return
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
self.currentCall = call
|
|
93
|
+
|
|
94
|
+
let lang = call.getString("lang") ?? "es-MX"
|
|
95
|
+
speechRecognizer = SFSpeechRecognizer(locale: Locale(identifier: lang))
|
|
96
|
+
|
|
97
|
+
SFSpeechRecognizer.requestAuthorization { authStatus in
|
|
98
|
+
if authStatus != .authorized {
|
|
99
|
+
self.currentCall?.reject("Speech recognition not authorized")
|
|
100
|
+
self.currentCall = nil
|
|
101
|
+
return
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
DispatchQueue.main.async {
|
|
105
|
+
self.startRecognition()
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private func startRecognition() {
|
|
112
|
+
|
|
113
|
+
recognitionRequest = SFSpeechAudioBufferRecognitionRequest()
|
|
114
|
+
guard let recognitionRequest = recognitionRequest else {
|
|
115
|
+
self.currentCall?.reject("Unable to create recognition request")
|
|
116
|
+
self.currentCall = nil
|
|
117
|
+
return
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
recognitionRequest.shouldReportPartialResults = true
|
|
121
|
+
|
|
122
|
+
let inputNode = audioEngine.inputNode
|
|
123
|
+
|
|
124
|
+
recognitionTask = speechRecognizer?.recognitionTask(with: recognitionRequest) { result, error in
|
|
125
|
+
if let result = result {
|
|
126
|
+
|
|
127
|
+
self.notifyListeners("partialResult", data: [
|
|
128
|
+
"text": result.bestTranscription.formattedString,
|
|
129
|
+
"isFinal": result.isFinal
|
|
130
|
+
])
|
|
131
|
+
|
|
132
|
+
if result.isFinal, let currentCall = self.currentCall {
|
|
133
|
+
currentCall.resolve([
|
|
134
|
+
"text": result.bestTranscription.formattedString,
|
|
135
|
+
"isFinal": true
|
|
136
|
+
])
|
|
137
|
+
self.currentCall = nil
|
|
138
|
+
self.stopAudio()
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if let error = error, let currentCall = self.currentCall {
|
|
143
|
+
currentCall.reject(error.localizedDescription)
|
|
144
|
+
self.currentCall = nil
|
|
145
|
+
self.stopAudio()
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
let recordingFormat = inputNode.outputFormat(forBus: 0)
|
|
150
|
+
inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) {
|
|
151
|
+
buffer, _ in
|
|
152
|
+
recognitionRequest.append(buffer)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
audioEngine.prepare()
|
|
156
|
+
|
|
157
|
+
do {
|
|
158
|
+
let audioSession = AVAudioSession.sharedInstance()
|
|
159
|
+
try audioSession.setCategory(.record, mode: .measurement, options: .duckOthers)
|
|
160
|
+
try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
|
|
161
|
+
try audioEngine.start()
|
|
162
|
+
} catch {
|
|
163
|
+
if let currentCall = self.currentCall {
|
|
164
|
+
currentCall.reject("Audio engine could not start")
|
|
165
|
+
self.currentCall = nil
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
@objc func stopListening(_ call: CAPPluginCall) {
|
|
172
|
+
stopAudio()
|
|
173
|
+
call.resolve(["stopped": true])
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
private func stopAudio() {
|
|
177
|
+
if audioEngine.isRunning {
|
|
178
|
+
audioEngine.stop()
|
|
179
|
+
}
|
|
180
|
+
if audioEngine.inputNode.numberOfInputs > 0 {
|
|
181
|
+
audioEngine.inputNode.removeTap(onBus: 0)
|
|
182
|
+
}
|
|
183
|
+
recognitionRequest?.endAudio()
|
|
184
|
+
|
|
185
|
+
recognitionTask?.cancel()
|
|
186
|
+
recognitionTask = nil
|
|
187
|
+
recognitionRequest = nil
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
override public func handleOnDestroy() {
|
|
191
|
+
stopAudio()
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
74
196
|
}
|