@independo/capacitor-voice-recorder 8.1.9-dev.1 → 8.2.0-dev.1
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 +15 -12
- package/android/src/main/java/app/independo/capacitorvoicerecorder/adapters/RecordDataMapper.java +1 -0
- package/android/src/main/java/app/independo/capacitorvoicerecorder/core/RecordData.java +35 -0
- package/android/src/main/java/app/independo/capacitorvoicerecorder/service/VoiceRecorderService.java +1 -1
- package/dist/docs.json +1 -1
- package/dist/esm/core/recording-contract.js +2 -2
- package/dist/esm/core/recording-contract.js.map +1 -1
- package/dist/esm/definitions.d.ts +4 -0
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/platform/web/VoiceRecorderImpl.d.ts +0 -2
- package/dist/esm/platform/web/VoiceRecorderImpl.js +8 -15
- package/dist/esm/platform/web/VoiceRecorderImpl.js.map +1 -1
- package/dist/plugin.cjs.js +10 -17
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +10 -17
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/VoiceRecorder/Adapters/RecordDataMapper.swift +1 -0
- package/ios/Sources/VoiceRecorder/Core/RecordData.swift +3 -0
- package/ios/Sources/VoiceRecorder/Platform/CustomMediaRecorder.swift +12 -4
- package/ios/Sources/VoiceRecorder/Service/VoiceRecorderService.swift +6 -1
- package/package.json +32 -31
package/README.md
CHANGED
|
@@ -21,8 +21,8 @@ The `@independo/capacitor-voice-recorder` plugin allows you to record audio on A
|
|
|
21
21
|
## Installation
|
|
22
22
|
|
|
23
23
|
```
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
pnpm add @independo/capacitor-voice-recorder
|
|
25
|
+
pnpm exec cap sync
|
|
26
26
|
```
|
|
27
27
|
|
|
28
28
|
### Configuration
|
|
@@ -48,7 +48,7 @@ Add the following to your `Info.plist`:
|
|
|
48
48
|
|
|
49
49
|
- Capacitor 8+
|
|
50
50
|
- iOS 15+
|
|
51
|
-
- Android minSdk 24+; builds require Java 21 (recommended). `
|
|
51
|
+
- Android minSdk 24+; builds require Java 21 (recommended). `pnpm verify:android` requires a Java version supported
|
|
52
52
|
by the bundled Gradle wrapper (currently Java 21–24, with Java 21 recommended).
|
|
53
53
|
|
|
54
54
|
### Compatibility
|
|
@@ -93,7 +93,7 @@ export const stopRecording = async () => {
|
|
|
93
93
|
|
|
94
94
|
## API
|
|
95
95
|
|
|
96
|
-
Below is an index of all available methods. Run `
|
|
96
|
+
Below is an index of all available methods. Run `pnpm docgen` after updating any JSDoc comments to refresh this
|
|
97
97
|
section.
|
|
98
98
|
|
|
99
99
|
<docgen-index>
|
|
@@ -333,9 +333,9 @@ Can be used to specify options for the recording.
|
|
|
333
333
|
|
|
334
334
|
Interface representing the data of a recording.
|
|
335
335
|
|
|
336
|
-
| Prop | Type
|
|
337
|
-
| ----------- |
|
|
338
|
-
| **`value`** | <code>{ recordDataBase64: string; msDuration: number; mimeType: string; uri?: string; }</code> | The value containing the recording details. |
|
|
336
|
+
| Prop | Type | Description |
|
|
337
|
+
| ----------- | --------------------------------------------------------------------------------------------------------------------- | ------------------------------------------- |
|
|
338
|
+
| **`value`** | <code>{ recordDataBase64: string; msDuration: number; mimeType: string; fileExtension: string; uri?: string; }</code> | The value containing the recording details. |
|
|
339
339
|
|
|
340
340
|
|
|
341
341
|
#### CurrentRecordingStatus
|
|
@@ -413,8 +413,8 @@ an interruption begins, the recording is paused, the status becomes `INTERRUPTED
|
|
|
413
413
|
event fires. When the interruption ends, the `voiceRecordingInterruptionEnded` event fires, and the status stays
|
|
414
414
|
`INTERRUPTED` until you call `resumeRecording()` or `stopRecording()`. Web does not provide interruption handling.
|
|
415
415
|
|
|
416
|
-
If interruptions occur on iOS, recordings are segmented and merged when you stop.
|
|
417
|
-
`audio/mp4
|
|
416
|
+
If interruptions occur on iOS, recordings are segmented and merged when you stop. iOS recordings are normalized to an
|
|
417
|
+
M4A container with MIME type `audio/mp4` for consistent output across interrupted and non-interrupted sessions.
|
|
418
418
|
|
|
419
419
|
### Web constraints
|
|
420
420
|
|
|
@@ -423,6 +423,9 @@ If interruptions occur on iOS, recordings are segmented and merged when you stop
|
|
|
423
423
|
- The Permissions API is not consistently supported; `hasAudioRecordingPermission()` can reject with
|
|
424
424
|
`COULD_NOT_QUERY_PERMISSION_STATUS`. In that case, use `requestAudioRecordingPermission()` or `startRecording()` and
|
|
425
425
|
handle errors.
|
|
426
|
+
- By default, the web implementation picks a MIME type that is supported for both recording (`MediaRecorder`) and
|
|
427
|
+
playback (`<audio>`). You can disable the playback probe with `RecordingOptions.requirePlaybackSupport = false` if
|
|
428
|
+
you prefer recorder-only MIME selection.
|
|
426
429
|
|
|
427
430
|
## Recording options and storage
|
|
428
431
|
|
|
@@ -448,9 +451,9 @@ The plugin returns the recording in one of several possible formats. The actual
|
|
|
448
451
|
browser capabilities.
|
|
449
452
|
|
|
450
453
|
- Android: `audio/aac`
|
|
451
|
-
- iOS: `audio/
|
|
452
|
-
- Web: first supported type from
|
|
453
|
-
|
|
454
|
+
- iOS: `audio/mp4` (M4A container)
|
|
455
|
+
- Web: first supported MIME type from the plugin's ordered list, with a default preference for formats that are
|
|
456
|
+
reported as playable by the browser `<audio>` element (in addition to `MediaRecorder` support)
|
|
454
457
|
|
|
455
458
|
Because not all devices and browsers support the same formats, recordings may not be playable everywhere. If you need
|
|
456
459
|
consistent playback across targets, convert recordings to a single format outside this plugin. The plugin focuses on
|
package/android/src/main/java/app/independo/capacitorvoicerecorder/adapters/RecordDataMapper.java
CHANGED
|
@@ -18,6 +18,7 @@ public final class RecordDataMapper {
|
|
|
18
18
|
JSObject normalized = new JSObject();
|
|
19
19
|
normalized.put("msDuration", recordData.getMsDuration());
|
|
20
20
|
normalized.put("mimeType", recordData.getMimeType());
|
|
21
|
+
normalized.put("fileExtension", recordData.getFileExtension());
|
|
21
22
|
|
|
22
23
|
String uri = recordData.getUri();
|
|
23
24
|
String recordDataBase64 = recordData.getRecordDataBase64();
|
|
@@ -11,15 +11,22 @@ public class RecordData {
|
|
|
11
11
|
private String recordDataBase64;
|
|
12
12
|
/** MIME type of the audio payload. */
|
|
13
13
|
private String mimeType;
|
|
14
|
+
/** File extension / format without a leading dot (for example: aac, m4a, mp3). */
|
|
15
|
+
private String fileExtension;
|
|
14
16
|
/** Recording duration in milliseconds. */
|
|
15
17
|
private int msDuration;
|
|
16
18
|
|
|
17
19
|
public RecordData() {}
|
|
18
20
|
|
|
19
21
|
public RecordData(String recordDataBase64, int msDuration, String mimeType, String uri) {
|
|
22
|
+
this(recordDataBase64, msDuration, mimeType, inferFileExtension(mimeType, uri), uri);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public RecordData(String recordDataBase64, int msDuration, String mimeType, String fileExtension, String uri) {
|
|
20
26
|
this.recordDataBase64 = recordDataBase64;
|
|
21
27
|
this.msDuration = msDuration;
|
|
22
28
|
this.mimeType = mimeType;
|
|
29
|
+
this.fileExtension = fileExtension;
|
|
23
30
|
this.uri = uri;
|
|
24
31
|
}
|
|
25
32
|
|
|
@@ -50,6 +57,15 @@ public class RecordData {
|
|
|
50
57
|
this.mimeType = mimeType;
|
|
51
58
|
}
|
|
52
59
|
|
|
60
|
+
/** Returns the file extension / format of the recording. */
|
|
61
|
+
public String getFileExtension() {
|
|
62
|
+
return fileExtension;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public void setFileExtension(String fileExtension) {
|
|
66
|
+
this.fileExtension = fileExtension;
|
|
67
|
+
}
|
|
68
|
+
|
|
53
69
|
/** Returns the file URI, if present. */
|
|
54
70
|
public String getUri() {
|
|
55
71
|
return uri;
|
|
@@ -61,7 +77,26 @@ public class RecordData {
|
|
|
61
77
|
toReturn.put("recordDataBase64", recordDataBase64);
|
|
62
78
|
toReturn.put("msDuration", msDuration);
|
|
63
79
|
toReturn.put("mimeType", mimeType);
|
|
80
|
+
toReturn.put("fileExtension", fileExtension);
|
|
64
81
|
toReturn.put("uri", uri);
|
|
65
82
|
return toReturn;
|
|
66
83
|
}
|
|
84
|
+
|
|
85
|
+
private static String inferFileExtension(String mimeType, String uri) {
|
|
86
|
+
if (uri != null) {
|
|
87
|
+
int dotIndex = uri.lastIndexOf('.');
|
|
88
|
+
if (dotIndex >= 0 && dotIndex < uri.length() - 1) {
|
|
89
|
+
return uri.substring(dotIndex + 1).toLowerCase();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if ("audio/mp4".equals(mimeType)) return "m4a";
|
|
94
|
+
if ("audio/mpeg".equals(mimeType)) return "mp3";
|
|
95
|
+
if ("audio/wav".equals(mimeType)) return "wav";
|
|
96
|
+
if ("audio/webm".equals(mimeType) || "audio/webm;codecs=opus".equals(mimeType)) return "webm";
|
|
97
|
+
if ("audio/ogg;codecs=opus".equals(mimeType) || "audio/ogg;codecs=vorbis".equals(mimeType)) return "ogg";
|
|
98
|
+
if ("audio/aac".equals(mimeType)) return "aac";
|
|
99
|
+
|
|
100
|
+
return "";
|
|
101
|
+
}
|
|
67
102
|
}
|
package/android/src/main/java/app/independo/capacitorvoicerecorder/service/VoiceRecorderService.java
CHANGED
|
@@ -92,7 +92,7 @@ public class VoiceRecorderService {
|
|
|
92
92
|
}
|
|
93
93
|
|
|
94
94
|
int duration = platform.getDurationMs(recordedFile);
|
|
95
|
-
RecordData recordData = new RecordData(recordDataBase64, duration, "audio/aac", uri);
|
|
95
|
+
RecordData recordData = new RecordData(recordDataBase64, duration, "audio/aac", "aac", uri);
|
|
96
96
|
if ((recordDataBase64 == null && uri == null) || recordData.getMsDuration() < 0) {
|
|
97
97
|
throw new VoiceRecorderServiceException(ErrorCodes.EMPTY_RECORDING);
|
|
98
98
|
}
|
package/dist/docs.json
CHANGED
|
@@ -339,7 +339,7 @@
|
|
|
339
339
|
"complexTypes": [
|
|
340
340
|
"Base64String"
|
|
341
341
|
],
|
|
342
|
-
"type": "{ recordDataBase64: string; msDuration: number; mimeType: string; uri?: string | undefined; }"
|
|
342
|
+
"type": "{ recordDataBase64: string; msDuration: number; mimeType: string; fileExtension: string; uri?: string | undefined; }"
|
|
343
343
|
}
|
|
344
344
|
]
|
|
345
345
|
},
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/** Normalizes recording payloads into a stable contract shape. */
|
|
2
2
|
export const normalizeRecordingData = (data) => {
|
|
3
|
-
const { recordDataBase64, uri, msDuration, mimeType } = data.value;
|
|
4
|
-
const normalizedValue = { msDuration, mimeType };
|
|
3
|
+
const { recordDataBase64, uri, msDuration, mimeType, fileExtension } = data.value;
|
|
4
|
+
const normalizedValue = { msDuration, mimeType, fileExtension };
|
|
5
5
|
const trimmedUri = typeof uri === 'string' && uri.length > 0 ? uri : undefined;
|
|
6
6
|
const trimmedBase64 = typeof recordDataBase64 === 'string' && recordDataBase64.length > 0 ? recordDataBase64 : undefined;
|
|
7
7
|
if (trimmedUri) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"recording-contract.js","sourceRoot":"","sources":["../../../src/core/recording-contract.ts"],"names":[],"mappings":"AAEA,kEAAkE;AAClE,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,IAAmB,EAAiB,EAAE;IACzE,MAAM,EAAE,gBAAgB,EAAE,GAAG,EAAE,UAAU,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC;
|
|
1
|
+
{"version":3,"file":"recording-contract.js","sourceRoot":"","sources":["../../../src/core/recording-contract.ts"],"names":[],"mappings":"AAEA,kEAAkE;AAClE,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,IAAmB,EAAiB,EAAE;IACzE,MAAM,EAAE,gBAAgB,EAAE,GAAG,EAAE,UAAU,EAAE,QAAQ,EAAE,aAAa,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC;IAClF,MAAM,eAAe,GAA4B,EAAE,UAAU,EAAE,QAAQ,EAAE,aAAa,EAAE,CAAC;IACzF,MAAM,UAAU,GAAG,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,SAAS,CAAC;IAC/E,MAAM,aAAa,GAAG,OAAO,gBAAgB,KAAK,QAAQ,IAAI,gBAAgB,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,SAAS,CAAC;IAEzH,IAAI,UAAU,EAAE,CAAC;QACb,eAAe,CAAC,GAAG,GAAG,UAAU,CAAC;IACrC,CAAC;SAAM,IAAI,aAAa,EAAE,CAAC;QACvB,eAAe,CAAC,gBAAgB,GAAG,aAAa,CAAC;IACrD,CAAC;IAED,OAAO,EAAE,KAAK,EAAE,eAAe,EAAmB,CAAC;AACvD,CAAC,CAAC","sourcesContent":["import type { RecordingData } from '../definitions';\n\n/** Normalizes recording payloads into a stable contract shape. */\nexport const normalizeRecordingData = (data: RecordingData): RecordingData => {\n const { recordDataBase64, uri, msDuration, mimeType, fileExtension } = data.value;\n const normalizedValue: Record<string, unknown> = { msDuration, mimeType, fileExtension };\n const trimmedUri = typeof uri === 'string' && uri.length > 0 ? uri : undefined;\n const trimmedBase64 = typeof recordDataBase64 === 'string' && recordDataBase64.length > 0 ? recordDataBase64 : undefined;\n\n if (trimmedUri) {\n normalizedValue.uri = trimmedUri;\n } else if (trimmedBase64) {\n normalizedValue.recordDataBase64 = trimmedBase64;\n }\n\n return { value: normalizedValue } as RecordingData;\n};\n"]}
|
|
@@ -51,6 +51,10 @@ export interface RecordingData {
|
|
|
51
51
|
* The MIME type of the recorded file.
|
|
52
52
|
*/
|
|
53
53
|
mimeType: string;
|
|
54
|
+
/**
|
|
55
|
+
* The recorded file extension / format without a leading dot (for example: `m4a`, `aac`, `mp3`, `webm`).
|
|
56
|
+
*/
|
|
57
|
+
fileExtension: string;
|
|
54
58
|
/**
|
|
55
59
|
* The URI of the recording file.
|
|
56
60
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"","sourcesContent":["import type {PluginListenerHandle} from \"@capacitor/core\";\nimport type {Directory} from \"@capacitor/filesystem\";\n\n/**\n * Represents a Base64 encoded string.\n */\nexport type Base64String = string;\n\n/**\n * Can be used to specify options for the recording.\n */\nexport interface RecordingOptions {\n /**\n * The capacitor filesystem directory where the recording should be saved.\n *\n * If not specified, the recording will be stored in a base64 string and returned in the `RecordingData` object.\n * @see RecordingData\n */\n directory?: Directory;\n\n /**\n * An optional subdirectory in the specified directory where the recording should be saved.\n */\n subDirectory?: string;\n\n /**\n * Whether the web implementation should require the selected recording MIME type\n * to also be playable by the browser's native HTML `<audio>` element.\n *\n * Defaults to `true` on web to reduce cases where `MediaRecorder` reports support\n * for a format but the recorded file cannot be played back in the same browser\n * (observed on some Safari/iOS/WKWebView combinations).\n *\n * Native platforms ignore this option.\n */\n requirePlaybackSupport?: boolean;\n}\n\n/**\n * Interface representing the data of a recording.\n */\nexport interface RecordingData {\n /**\n * The value containing the recording details.\n */\n value: {\n /**\n * The recorded data as a Base64 encoded string.\n */\n recordDataBase64: Base64String;\n /**\n * The duration of the recording in milliseconds.\n */\n msDuration: number;\n /**\n * The MIME type of the recorded file.\n */\n mimeType: string;\n\n /**\n * The URI of the recording file.\n */\n uri?: string;\n };\n}\n\n/**\n * Interface representing a generic response with a boolean value.\n */\nexport interface GenericResponse {\n /**\n * The result of the operation as a boolean value.\n */\n value: boolean;\n}\n\n/**\n * Interface representing the current status of the voice recorder.\n */\nexport interface CurrentRecordingStatus {\n /**\n * The current status of the recorder, which can be one of the following values: 'RECORDING', 'PAUSED', 'INTERRUPTED', 'NONE'.\n */\n status: 'RECORDING' | 'PAUSED' | 'INTERRUPTED' | 'NONE';\n}\n\n/**\n * Event payload for voiceRecordingInterrupted event (empty - no data).\n */\nexport type VoiceRecordingInterruptedEvent = Record<string, never>;\n\n/**\n * Event payload for voiceRecordingInterruptionEnded event (empty - no data).\n */\nexport type VoiceRecordingInterruptionEndedEvent = Record<string, never>;\n\n/**\n * Interface for the VoiceRecorderPlugin which provides methods to record audio.\n */\nexport interface VoiceRecorderPlugin {\n /**\n * Checks if the current device can record audio.\n * On mobile, this function will always resolve to `{ value: true }`.\n * In a browser, it will resolve to `{ value: true }` or `{ value: false }` based on the browser's ability to record.\n * This method does not take into account the permission status, only if the browser itself is capable of recording at all.\n * @returns A promise that resolves to a GenericResponse.\n * @throws Error with code \"COULD_NOT_QUERY_PERMISSION_STATUS\" if the device cannot query the permission status.\n */\n canDeviceVoiceRecord(): Promise<GenericResponse>;\n\n /**\n * Requests audio recording permission from the user.\n * If the permission has already been provided, the promise will resolve with `{ value: true }`.\n * Otherwise, the promise will resolve to `{ value: true }` or `{ value: false }` based on the user's response.\n * @returns A promise that resolves to a GenericResponse.\n * @throws Error if the permission request fails.\n */\n requestAudioRecordingPermission(): Promise<GenericResponse>;\n\n /**\n * Checks if audio recording permission has been granted.\n * Will resolve to `{ value: true }` or `{ value: false }` based on the status of the permission.\n * The web implementation of this plugin uses the Permissions API, which is not widespread.\n * If the status of the permission cannot be checked, the promise will reject with `COULD_NOT_QUERY_PERMISSION_STATUS`.\n * In that case, use `requestAudioRecordingPermission` or `startRecording` and capture any exception that is thrown.\n * @returns A promise that resolves to a GenericResponse.\n * @throws Error with code \"COULD_NOT_QUERY_PERMISSION_STATUS\" if the device cannot query the permission status.\n */\n hasAudioRecordingPermission(): Promise<GenericResponse>;\n\n /**\n * Starts audio recording.\n * On success, the promise will resolve to { value: true }.\n * On error, the promise will reject with one of the following error codes:\n * \"MISSING_PERMISSION\", \"ALREADY_RECORDING\", \"MICROPHONE_BEING_USED\", \"DEVICE_CANNOT_VOICE_RECORD\", or \"FAILED_TO_RECORD\".\n * @param options The options for the recording.\n * @returns A promise that resolves to a GenericResponse.\n * @throws Error with one of the specified error codes if the recording cannot be started.\n */\n startRecording(options?: RecordingOptions): Promise<GenericResponse>;\n\n /**\n * Stops audio recording.\n * Will stop the recording that has been previously started.\n * If the function `startRecording` has not been called beforehand, the promise will reject with `RECORDING_HAS_NOT_STARTED`.\n * If the recording has been stopped immediately after it has been started, the promise will reject with `EMPTY_RECORDING`.\n * In a case of unknown error, the promise will reject with `FAILED_TO_FETCH_RECORDING`.\n * On iOS, if a recording interrupted by the system cannot be merged, the promise will reject with `FAILED_TO_MERGE_RECORDING`.\n * In case of success, the promise resolves to RecordingData containing the recording in base-64, the duration of the recording in milliseconds, and the MIME type.\n * @returns A promise that resolves to RecordingData.\n * @throws Error with one of the specified error codes if the recording cannot be stopped.\n */\n stopRecording(): Promise<RecordingData>;\n\n /**\n * Pauses the ongoing audio recording.\n * If the recording has not started yet, the promise will reject with an error code `RECORDING_HAS_NOT_STARTED`.\n * On success, the promise will resolve to { value: true } if the pause was successful or { value: false } if the recording is already paused.\n * On certain mobile OS versions, this function is not supported and will reject with `NOT_SUPPORTED_OS_VERSION`.\n * @returns A promise that resolves to a GenericResponse.\n * @throws Error with one of the specified error codes if the recording cannot be paused.\n */\n pauseRecording(): Promise<GenericResponse>;\n\n /**\n * Resumes a paused or interrupted audio recording.\n * If the recording has not started yet, the promise will reject with an error code `RECORDING_HAS_NOT_STARTED`.\n * On success, the promise will resolve to { value: true } if the resume was successful or { value: false } if the recording is already running.\n * On certain mobile OS versions, this function is not supported and will reject with `NOT_SUPPORTED_OS_VERSION`.\n * @returns A promise that resolves to a GenericResponse.\n * @throws Error with one of the specified error codes if the recording cannot be resumed.\n */\n resumeRecording(): Promise<GenericResponse>;\n\n /**\n * Gets the current status of the voice recorder.\n * Will resolve with one of the following values:\n * `{ status: \"NONE\" }` if the plugin is idle and waiting to start a new recording.\n * `{ status: \"RECORDING\" }` if the plugin is in the middle of recording.\n * `{ status: \"PAUSED\" }` if the recording is paused.\n * `{ status: \"INTERRUPTED\" }` if the recording was paused due to a system interruption.\n * @returns A promise that resolves to a CurrentRecordingStatus.\n * @throws Error if the status cannot be fetched.\n */\n getCurrentStatus(): Promise<CurrentRecordingStatus>;\n\n /**\n * Listen for audio recording interruptions (e.g., phone calls, other apps using microphone).\n * Available on iOS and Android only.\n *\n * @param eventName The name of the event to listen for.\n * @param listenerFunc The callback function to invoke when the event occurs.\n * @returns A promise that resolves to a PluginListenerHandle.\n */\n addListener(\n eventName: 'voiceRecordingInterrupted',\n listenerFunc: (event: VoiceRecordingInterruptedEvent) => void,\n ): Promise<PluginListenerHandle>;\n\n /**\n * Listen for audio recording interruption end events.\n * Available on iOS and Android only.\n *\n * @param eventName The name of the event to listen for.\n * @param listenerFunc The callback function to invoke when the event occurs.\n * @returns A promise that resolves to a PluginListenerHandle.\n */\n addListener(\n eventName: 'voiceRecordingInterruptionEnded',\n listenerFunc: (event: VoiceRecordingInterruptionEndedEvent) => void,\n ): Promise<PluginListenerHandle>;\n\n /**\n * Remove all listeners for this plugin.\n */\n removeAllListeners(): Promise<void>;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"","sourcesContent":["import type {PluginListenerHandle} from \"@capacitor/core\";\nimport type {Directory} from \"@capacitor/filesystem\";\n\n/**\n * Represents a Base64 encoded string.\n */\nexport type Base64String = string;\n\n/**\n * Can be used to specify options for the recording.\n */\nexport interface RecordingOptions {\n /**\n * The capacitor filesystem directory where the recording should be saved.\n *\n * If not specified, the recording will be stored in a base64 string and returned in the `RecordingData` object.\n * @see RecordingData\n */\n directory?: Directory;\n\n /**\n * An optional subdirectory in the specified directory where the recording should be saved.\n */\n subDirectory?: string;\n\n /**\n * Whether the web implementation should require the selected recording MIME type\n * to also be playable by the browser's native HTML `<audio>` element.\n *\n * Defaults to `true` on web to reduce cases where `MediaRecorder` reports support\n * for a format but the recorded file cannot be played back in the same browser\n * (observed on some Safari/iOS/WKWebView combinations).\n *\n * Native platforms ignore this option.\n */\n requirePlaybackSupport?: boolean;\n}\n\n/**\n * Interface representing the data of a recording.\n */\nexport interface RecordingData {\n /**\n * The value containing the recording details.\n */\n value: {\n /**\n * The recorded data as a Base64 encoded string.\n */\n recordDataBase64: Base64String;\n /**\n * The duration of the recording in milliseconds.\n */\n msDuration: number;\n /**\n * The MIME type of the recorded file.\n */\n mimeType: string;\n /**\n * The recorded file extension / format without a leading dot (for example: `m4a`, `aac`, `mp3`, `webm`).\n */\n fileExtension: string;\n\n /**\n * The URI of the recording file.\n */\n uri?: string;\n };\n}\n\n/**\n * Interface representing a generic response with a boolean value.\n */\nexport interface GenericResponse {\n /**\n * The result of the operation as a boolean value.\n */\n value: boolean;\n}\n\n/**\n * Interface representing the current status of the voice recorder.\n */\nexport interface CurrentRecordingStatus {\n /**\n * The current status of the recorder, which can be one of the following values: 'RECORDING', 'PAUSED', 'INTERRUPTED', 'NONE'.\n */\n status: 'RECORDING' | 'PAUSED' | 'INTERRUPTED' | 'NONE';\n}\n\n/**\n * Event payload for voiceRecordingInterrupted event (empty - no data).\n */\nexport type VoiceRecordingInterruptedEvent = Record<string, never>;\n\n/**\n * Event payload for voiceRecordingInterruptionEnded event (empty - no data).\n */\nexport type VoiceRecordingInterruptionEndedEvent = Record<string, never>;\n\n/**\n * Interface for the VoiceRecorderPlugin which provides methods to record audio.\n */\nexport interface VoiceRecorderPlugin {\n /**\n * Checks if the current device can record audio.\n * On mobile, this function will always resolve to `{ value: true }`.\n * In a browser, it will resolve to `{ value: true }` or `{ value: false }` based on the browser's ability to record.\n * This method does not take into account the permission status, only if the browser itself is capable of recording at all.\n * @returns A promise that resolves to a GenericResponse.\n * @throws Error with code \"COULD_NOT_QUERY_PERMISSION_STATUS\" if the device cannot query the permission status.\n */\n canDeviceVoiceRecord(): Promise<GenericResponse>;\n\n /**\n * Requests audio recording permission from the user.\n * If the permission has already been provided, the promise will resolve with `{ value: true }`.\n * Otherwise, the promise will resolve to `{ value: true }` or `{ value: false }` based on the user's response.\n * @returns A promise that resolves to a GenericResponse.\n * @throws Error if the permission request fails.\n */\n requestAudioRecordingPermission(): Promise<GenericResponse>;\n\n /**\n * Checks if audio recording permission has been granted.\n * Will resolve to `{ value: true }` or `{ value: false }` based on the status of the permission.\n * The web implementation of this plugin uses the Permissions API, which is not widespread.\n * If the status of the permission cannot be checked, the promise will reject with `COULD_NOT_QUERY_PERMISSION_STATUS`.\n * In that case, use `requestAudioRecordingPermission` or `startRecording` and capture any exception that is thrown.\n * @returns A promise that resolves to a GenericResponse.\n * @throws Error with code \"COULD_NOT_QUERY_PERMISSION_STATUS\" if the device cannot query the permission status.\n */\n hasAudioRecordingPermission(): Promise<GenericResponse>;\n\n /**\n * Starts audio recording.\n * On success, the promise will resolve to { value: true }.\n * On error, the promise will reject with one of the following error codes:\n * \"MISSING_PERMISSION\", \"ALREADY_RECORDING\", \"MICROPHONE_BEING_USED\", \"DEVICE_CANNOT_VOICE_RECORD\", or \"FAILED_TO_RECORD\".\n * @param options The options for the recording.\n * @returns A promise that resolves to a GenericResponse.\n * @throws Error with one of the specified error codes if the recording cannot be started.\n */\n startRecording(options?: RecordingOptions): Promise<GenericResponse>;\n\n /**\n * Stops audio recording.\n * Will stop the recording that has been previously started.\n * If the function `startRecording` has not been called beforehand, the promise will reject with `RECORDING_HAS_NOT_STARTED`.\n * If the recording has been stopped immediately after it has been started, the promise will reject with `EMPTY_RECORDING`.\n * In a case of unknown error, the promise will reject with `FAILED_TO_FETCH_RECORDING`.\n * On iOS, if a recording interrupted by the system cannot be merged, the promise will reject with `FAILED_TO_MERGE_RECORDING`.\n * In case of success, the promise resolves to RecordingData containing the recording in base-64, the duration of the recording in milliseconds, and the MIME type.\n * @returns A promise that resolves to RecordingData.\n * @throws Error with one of the specified error codes if the recording cannot be stopped.\n */\n stopRecording(): Promise<RecordingData>;\n\n /**\n * Pauses the ongoing audio recording.\n * If the recording has not started yet, the promise will reject with an error code `RECORDING_HAS_NOT_STARTED`.\n * On success, the promise will resolve to { value: true } if the pause was successful or { value: false } if the recording is already paused.\n * On certain mobile OS versions, this function is not supported and will reject with `NOT_SUPPORTED_OS_VERSION`.\n * @returns A promise that resolves to a GenericResponse.\n * @throws Error with one of the specified error codes if the recording cannot be paused.\n */\n pauseRecording(): Promise<GenericResponse>;\n\n /**\n * Resumes a paused or interrupted audio recording.\n * If the recording has not started yet, the promise will reject with an error code `RECORDING_HAS_NOT_STARTED`.\n * On success, the promise will resolve to { value: true } if the resume was successful or { value: false } if the recording is already running.\n * On certain mobile OS versions, this function is not supported and will reject with `NOT_SUPPORTED_OS_VERSION`.\n * @returns A promise that resolves to a GenericResponse.\n * @throws Error with one of the specified error codes if the recording cannot be resumed.\n */\n resumeRecording(): Promise<GenericResponse>;\n\n /**\n * Gets the current status of the voice recorder.\n * Will resolve with one of the following values:\n * `{ status: \"NONE\" }` if the plugin is idle and waiting to start a new recording.\n * `{ status: \"RECORDING\" }` if the plugin is in the middle of recording.\n * `{ status: \"PAUSED\" }` if the recording is paused.\n * `{ status: \"INTERRUPTED\" }` if the recording was paused due to a system interruption.\n * @returns A promise that resolves to a CurrentRecordingStatus.\n * @throws Error if the status cannot be fetched.\n */\n getCurrentStatus(): Promise<CurrentRecordingStatus>;\n\n /**\n * Listen for audio recording interruptions (e.g., phone calls, other apps using microphone).\n * Available on iOS and Android only.\n *\n * @param eventName The name of the event to listen for.\n * @param listenerFunc The callback function to invoke when the event occurs.\n * @returns A promise that resolves to a PluginListenerHandle.\n */\n addListener(\n eventName: 'voiceRecordingInterrupted',\n listenerFunc: (event: VoiceRecordingInterruptedEvent) => void,\n ): Promise<PluginListenerHandle>;\n\n /**\n * Listen for audio recording interruption end events.\n * Available on iOS and Android only.\n *\n * @param eventName The name of the event to listen for.\n * @param listenerFunc The callback function to invoke when the event occurs.\n * @returns A promise that resolves to a PluginListenerHandle.\n */\n addListener(\n eventName: 'voiceRecordingInterruptionEnded',\n listenerFunc: (event: VoiceRecordingInterruptionEndedEvent) => void,\n ): Promise<PluginListenerHandle>;\n\n /**\n * Remove all listeners for this plugin.\n */\n removeAllListeners(): Promise<void>;\n}\n"]}
|
|
@@ -37,8 +37,6 @@ export declare class VoiceRecorderImpl {
|
|
|
37
37
|
private static readonly DEFAULT_REQUIRE_PLAYBACK_SUPPORT;
|
|
38
38
|
/** Active MediaRecorder instance, if recording. */
|
|
39
39
|
private mediaRecorder;
|
|
40
|
-
/** MIME type selected at start time for the active recording. */
|
|
41
|
-
private activeMimeType;
|
|
42
40
|
/** Collected data chunks from MediaRecorder. */
|
|
43
41
|
private chunks;
|
|
44
42
|
/** Promise resolved when the recorder stops and payload is ready. */
|
|
@@ -55,8 +55,6 @@ export class VoiceRecorderImpl {
|
|
|
55
55
|
constructor() {
|
|
56
56
|
/** Active MediaRecorder instance, if recording. */
|
|
57
57
|
this.mediaRecorder = null;
|
|
58
|
-
/** MIME type selected at start time for the active recording. */
|
|
59
|
-
this.activeMimeType = null;
|
|
60
58
|
/** Collected data chunks from MediaRecorder. */
|
|
61
59
|
this.chunks = [];
|
|
62
60
|
/** Promise resolved when the recorder stops and payload is ready. */
|
|
@@ -273,21 +271,15 @@ export class VoiceRecorderImpl {
|
|
|
273
271
|
reject(failedToRecordError());
|
|
274
272
|
return;
|
|
275
273
|
}
|
|
276
|
-
this.activeMimeType = mimeType;
|
|
277
274
|
this.mediaRecorder = new MediaRecorder(stream, { mimeType });
|
|
278
275
|
this.mediaRecorder.onerror = () => {
|
|
279
276
|
this.prepareInstanceForNextOperation();
|
|
280
277
|
reject(failedToRecordError());
|
|
281
278
|
};
|
|
282
279
|
this.mediaRecorder.onstop = async () => {
|
|
283
|
-
var _a, _b, _c;
|
|
284
|
-
const
|
|
285
|
-
|
|
286
|
-
this.prepareInstanceForNextOperation();
|
|
287
|
-
reject(failedToFetchRecordingError());
|
|
288
|
-
return;
|
|
289
|
-
}
|
|
290
|
-
const blobVoiceRecording = new Blob(this.chunks, { type: finalMimeType });
|
|
280
|
+
var _a, _b, _c, _d, _e, _f;
|
|
281
|
+
const mt = (_b = (_a = this.mediaRecorder) === null || _a === void 0 ? void 0 : _a.mimeType) !== null && _b !== void 0 ? _b : mimeType;
|
|
282
|
+
const blobVoiceRecording = new Blob(this.chunks, { type: mt });
|
|
291
283
|
if (blobVoiceRecording.size <= 0) {
|
|
292
284
|
this.prepareInstanceForNextOperation();
|
|
293
285
|
reject(emptyRecordingError());
|
|
@@ -295,9 +287,10 @@ export class VoiceRecorderImpl {
|
|
|
295
287
|
}
|
|
296
288
|
let uri = undefined;
|
|
297
289
|
let recordDataBase64 = '';
|
|
290
|
+
const fileExtension = ((_c = POSSIBLE_MIME_TYPES[mimeType]) !== null && _c !== void 0 ? _c : '').replace(/^\./, '');
|
|
298
291
|
if (options === null || options === void 0 ? void 0 : options.directory) {
|
|
299
|
-
const subDirectory = (
|
|
300
|
-
const path = `${subDirectory}/recording-${new Date().getTime()}${POSSIBLE_MIME_TYPES[
|
|
292
|
+
const subDirectory = (_f = (_e = (_d = options.subDirectory) === null || _d === void 0 ? void 0 : _d.match(/^\/?(.+[^/])\/?$/)) === null || _e === void 0 ? void 0 : _e[1]) !== null && _f !== void 0 ? _f : '';
|
|
293
|
+
const path = `${subDirectory}/recording-${new Date().getTime()}${POSSIBLE_MIME_TYPES[mt]}`;
|
|
301
294
|
await write_blob({
|
|
302
295
|
blob: blobVoiceRecording,
|
|
303
296
|
directory: options.directory,
|
|
@@ -315,7 +308,8 @@ export class VoiceRecorderImpl {
|
|
|
315
308
|
resolve({
|
|
316
309
|
value: {
|
|
317
310
|
recordDataBase64,
|
|
318
|
-
mimeType:
|
|
311
|
+
mimeType: mt,
|
|
312
|
+
fileExtension,
|
|
319
313
|
msDuration: recordingDuration * 1000,
|
|
320
314
|
uri
|
|
321
315
|
}
|
|
@@ -356,7 +350,6 @@ export class VoiceRecorderImpl {
|
|
|
356
350
|
}
|
|
357
351
|
this.pendingResult = neverResolvingPromise();
|
|
358
352
|
this.mediaRecorder = null;
|
|
359
|
-
this.activeMimeType = null;
|
|
360
353
|
this.chunks = [];
|
|
361
354
|
}
|
|
362
355
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"VoiceRecorderImpl.js","s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C;QAC3B,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC;IACrB,CAAC;;AAnUD,oFAAoF;AAC5D,kDAAgC,GAAG,IAAI,AAAP,CAAQ","sourcesContent":["import {Filesystem} from '@capacitor/filesystem';\nimport write_blob from 'capacitor-blob-writer';\n\nimport type {\n Base64String,\n CurrentRecordingStatus,\n GenericResponse,\n RecordingData,\n RecordingOptions,\n} from '../../definitions';\n\nimport getBlobDuration from './get-blob-duration';\nimport {\n alreadyRecordingError,\n couldNotQueryPermissionStatusError,\n deviceCannotVoiceRecordError,\n emptyRecordingError,\n failedToFetchRecordingError,\n failedToRecordError,\n failureResponse,\n missingPermissionError,\n recordingHasNotStartedError,\n successResponse,\n} from './predefined-web-responses';\n\n/**\n * Ordered MIME types to probe for audio recording via `MediaRecorder.isTypeSupported()`.\n *\n * ⚠️ The order is intentional and MUST remain stable unless you also update the\n * selection policy in code and test on Safari/iOS + WebViews.\n *\n * ✅ What this list is used for\n * - Selecting a `mimeType` for `new MediaRecorder(stream, { mimeType })`.\n *\n * ❌ What this list does NOT guarantee\n * - It does NOT guarantee that the recorded output will be playable via the\n * HTML `<audio>` element in the same browser.\n *\n * Real-world caveat (important):\n * - We have observed cases where `MediaRecorder.isTypeSupported('audio/webm;codecs=opus')`\n * returned `true`, the recorder produced a Blob, but `<audio>` could not play it.\n * This can happen due to container/codec playback support differences, platform\n * quirks (especially Safari/iOS / WKWebView), or incomplete WebM playback support.\n *\n * Current selection behavior in this implementation:\n * - By default, MIME selection treats recorder support and playback support as separate\n * capabilities and probes both:\n * - Recorder capability: `MediaRecorder.isTypeSupported(type)`\n * - Playback capability: `audio.canPlayType(type)`\n * - This default can be disabled via `RecordingOptions.requirePlaybackSupport = false`\n * to fall back to recorder-only probing.\n *\n * Keeping legacy keys:\n * - Some entries are kept even if they overlap (e.g. `audio/mp4` and explicit codec),\n * to maximize compatibility across differing browser implementations.\n */\nconst POSSIBLE_MIME_TYPES: Record<string, string> = {\n // ✅ Most universal\n 'audio/mp4;codecs=\"mp4a.40.2\"': '.m4a', // AAC in MP4 (explicit codec helps detection)\n 'audio/mp4': '.m4a', // (legacy key kept; broad support)\n 'audio/aac': '.aac', // (legacy key kept; less common in the wild)\n 'audio/mpeg': '.mp3', // MP3 (universal)\n 'audio/wav': '.wav', // WAV (universal, big files)\n\n // ✅ Modern high-quality (very widely supported, but slightly less “universal” than MP3/AAC)\n 'audio/webm;codecs=\"opus\"': '.webm', // Opus in WebM (explicit codec helps detection)\n 'audio/webm;codecs=opus': '.webm', // (legacy key kept)\n 'audio/webm': '.webm', // (legacy key kept; container-only, codec-dependent)\n\n // ⚠️ Least universal (Safari/iOS historically the limiting factor)\n 'audio/ogg;codecs=opus': '.ogg', // (legacy key kept)\n 'audio/ogg;codecs=vorbis': '.ogg', // Ogg Vorbis (weakest mainstream support)\n};\n\n/** Creates a promise that never resolves. */\nconst neverResolvingPromise = (): Promise<any> => new Promise(() => undefined);\n\n/** Browser implementation backed by MediaRecorder and Capacitor Filesystem. */\nexport class VoiceRecorderImpl {\n /** Default behavior for web MIME selection: require recorder + playback support. */\n private static readonly DEFAULT_REQUIRE_PLAYBACK_SUPPORT = true;\n /** Active MediaRecorder instance, if recording. */\n private mediaRecorder: MediaRecorder | null = null;\n /** MIME type selected at start time for the active recording. */\n private activeMimeType: keyof typeof POSSIBLE_MIME_TYPES | null = null;\n /** Collected data chunks from MediaRecorder. */\n private chunks: any[] = [];\n /** Promise resolved when the recorder stops and payload is ready. */\n private pendingResult: Promise<RecordingData> = neverResolvingPromise();\n\n /**\n * Returns whether the browser can start a recording session.\n *\n * On web this checks:\n * - `navigator.mediaDevices.getUserMedia`\n * - at least one supported recording MIME type using {@link getSupportedMimeType}\n *\n * The optional `requirePlaybackSupport` flag is forwarded to MIME selection and defaults\n * to `true` when omitted.\n */\n public static async canDeviceVoiceRecord(\n options?: Pick<RecordingOptions, 'requirePlaybackSupport'>,\n ): Promise<GenericResponse> {\n if (\n navigator?.mediaDevices?.getUserMedia == null ||\n VoiceRecorderImpl.getSupportedMimeType({\n requirePlaybackSupport: options?.requirePlaybackSupport,\n }) == null\n ) {\n return failureResponse();\n } else {\n return successResponse();\n }\n }\n\n /**\n * Starts a recording session using `MediaRecorder`.\n *\n * The selected MIME type is resolved once at start time (using the optional\n * `requirePlaybackSupport` flag from `RecordingOptions`) and reused for the final Blob\n * and file extension to keep the recording payload internally consistent.\n */\n public async startRecording(options?: RecordingOptions): Promise<GenericResponse> {\n if (this.mediaRecorder != null) {\n throw alreadyRecordingError();\n }\n const deviceCanRecord = await VoiceRecorderImpl.canDeviceVoiceRecord(options);\n if (!deviceCanRecord.value) {\n throw deviceCannotVoiceRecordError();\n }\n const havingPermission = await VoiceRecorderImpl.hasAudioRecordingPermission().catch(() => successResponse());\n if (!havingPermission.value) {\n throw missingPermissionError();\n }\n\n return navigator.mediaDevices\n .getUserMedia({audio: true})\n .then((stream) => this.onSuccessfullyStartedRecording(stream, options))\n .catch(this.onFailedToStartRecording.bind(this));\n }\n\n /** Stops the current recording and resolves the pending payload. */\n public async stopRecording(): Promise<RecordingData> {\n if (this.mediaRecorder == null) {\n throw recordingHasNotStartedError();\n }\n try {\n this.mediaRecorder.stop();\n this.mediaRecorder.stream.getTracks().forEach((track) => track.stop());\n return this.pendingResult;\n } catch (ignore) {\n throw failedToFetchRecordingError();\n } finally {\n this.prepareInstanceForNextOperation();\n }\n }\n\n /** Returns whether the browser has microphone permission. */\n public static async hasAudioRecordingPermission(): Promise<GenericResponse> {\n // Safari does not support navigator.permissions.query\n if (!navigator.permissions.query) {\n if (navigator.mediaDevices !== undefined) {\n return navigator.mediaDevices\n .getUserMedia({audio: true})\n .then(() => successResponse())\n .catch(() => {\n throw couldNotQueryPermissionStatusError();\n });\n }\n }\n return navigator.permissions\n .query({name: 'microphone' as any})\n .then((result) => ({value: result.state === 'granted'}))\n .catch(() => {\n throw couldNotQueryPermissionStatusError();\n });\n }\n\n /** Requests microphone permission from the browser. */\n public static async requestAudioRecordingPermission(): Promise<GenericResponse> {\n const havingPermission = await VoiceRecorderImpl.hasAudioRecordingPermission().catch(() => failureResponse());\n if (havingPermission.value) {\n return successResponse();\n }\n\n return navigator.mediaDevices\n .getUserMedia({audio: true})\n .then(() => successResponse())\n .catch(() => failureResponse());\n }\n\n /** Pauses the recording session when supported. */\n public pauseRecording(): Promise<GenericResponse> {\n if (this.mediaRecorder == null) {\n throw recordingHasNotStartedError();\n } else if (this.mediaRecorder.state === 'recording') {\n this.mediaRecorder.pause();\n return Promise.resolve(successResponse());\n } else {\n return Promise.resolve(failureResponse());\n }\n }\n\n /** Resumes a paused recording session when supported. */\n public resumeRecording(): Promise<GenericResponse> {\n if (this.mediaRecorder == null) {\n throw recordingHasNotStartedError();\n } else if (this.mediaRecorder.state === 'paused') {\n this.mediaRecorder.resume();\n return Promise.resolve(successResponse());\n } else {\n return Promise.resolve(failureResponse());\n }\n }\n\n /** Returns the current recording status from MediaRecorder. */\n public getCurrentStatus(): Promise<CurrentRecordingStatus> {\n if (this.mediaRecorder == null) {\n return Promise.resolve({status: 'NONE'});\n } else if (this.mediaRecorder.state === 'recording') {\n return Promise.resolve({status: 'RECORDING'});\n } else if (this.mediaRecorder.state === 'paused') {\n return Promise.resolve({status: 'PAUSED'});\n } else {\n return Promise.resolve({status: 'NONE'});\n }\n }\n\n /**\n * Returns the first MIME type (key of {@link POSSIBLE_MIME_TYPES}) that the current\n * environment reports as supported for recording via `MediaRecorder.isTypeSupported()`,\n * optionally requiring native HTML `<audio>` playback support too.\n *\n * The search order is the iteration order of {@link POSSIBLE_MIME_TYPES}.\n *\n * @typeParam T - A MIME type string that exists as a key in {@link POSSIBLE_MIME_TYPES}.\n *\n * @returns The first supported MIME type for `MediaRecorder`, or `null` if:\n * - `MediaRecorder` is unavailable, or\n * - no configured MIME types are supported.\n *\n * ⚠️ Important: `MediaRecorder` support ≠ `<audio>` playback support\n *\n * Some browsers/platforms can claim support for recording a format (notably WebM/Opus)\n * but still fail to play the resulting Blob through the native HTML audio pipeline.\n * This mismatch is especially likely on Safari/iOS / WKWebView variants, so the default\n * behavior also probes `HTMLAudioElement.canPlayType(type)` when available.\n *\n * Selection policy when playback probing is enabled:\n * - keep the global priority order from {@link POSSIBLE_MIME_TYPES}\n * - among recordable types, prefer the first `\"probably\"` playable candidate\n * - otherwise return the first `\"maybe\"` playable candidate\n * - treat `\"\"` as not playable\n *\n * Note: The <audio> element is never attached to the DOM, so it won't appear to users or assistive tech.\n *\n * Fallback behavior:\n * - If `document` / `audio.canPlayType` is unavailable (e.g. SSR-like environments),\n * this falls back to record-only probing.\n */\n public static getSupportedMimeType<T extends keyof typeof POSSIBLE_MIME_TYPES>(\n options?: { requirePlaybackSupport?: boolean },\n ): T | null {\n if (MediaRecorder?.isTypeSupported == null) return null;\n\n const orderedTypes = Object.keys(POSSIBLE_MIME_TYPES) as T[];\n const recordSupportedTypes = orderedTypes.filter((type) => MediaRecorder.isTypeSupported(type));\n if (recordSupportedTypes.length === 0) return null;\n\n const requirePlaybackSupport =\n options?.requirePlaybackSupport ?? VoiceRecorderImpl.DEFAULT_REQUIRE_PLAYBACK_SUPPORT;\n if (!requirePlaybackSupport) {\n return recordSupportedTypes[0] ?? null;\n }\n\n if (typeof document === 'undefined' || typeof document.createElement !== 'function') {\n return recordSupportedTypes[0] ?? null;\n }\n\n const audioElement = document.createElement('audio') as Partial<HTMLAudioElement>;\n if (typeof audioElement.canPlayType !== 'function') {\n return recordSupportedTypes[0] ?? null;\n }\n\n let firstProbably: T | null = null;\n let firstMaybe: T | null = null;\n\n for (const type of recordSupportedTypes) {\n const playbackSupport = audioElement.canPlayType(type);\n if (playbackSupport === 'probably') {\n firstProbably = type;\n break;\n }\n if (playbackSupport === 'maybe' && firstMaybe == null) {\n firstMaybe = type;\n }\n }\n\n return firstProbably ?? firstMaybe ?? null;\n }\n\n /** Initializes MediaRecorder and wires up handlers. */\n private onSuccessfullyStartedRecording(stream: MediaStream, options?: RecordingOptions): GenericResponse {\n this.pendingResult = new Promise((resolve, reject) => {\n const mimeType = VoiceRecorderImpl.getSupportedMimeType({\n requirePlaybackSupport: options?.requirePlaybackSupport,\n });\n if (mimeType == null) {\n this.prepareInstanceForNextOperation();\n reject(failedToRecordError());\n return;\n }\n\n this.activeMimeType = mimeType;\n this.mediaRecorder = new MediaRecorder(stream, {mimeType});\n this.mediaRecorder.onerror = () => {\n this.prepareInstanceForNextOperation();\n reject(failedToRecordError());\n };\n this.mediaRecorder.onstop = async () => {\n const finalMimeType = this.activeMimeType;\n if (finalMimeType == null) {\n this.prepareInstanceForNextOperation();\n reject(failedToFetchRecordingError());\n return;\n }\n const blobVoiceRecording = new Blob(this.chunks, {type: finalMimeType});\n if (blobVoiceRecording.size <= 0) {\n this.prepareInstanceForNextOperation();\n reject(emptyRecordingError());\n return;\n }\n\n let uri: string | undefined = undefined;\n let recordDataBase64 = '';\n if (options?.directory) {\n const subDirectory = options.subDirectory?.match(/^\\/?(.+[^/])\\/?$/)?.[1] ?? '';\n const path = `${subDirectory}/recording-${new Date().getTime()}${POSSIBLE_MIME_TYPES[finalMimeType]}`;\n\n await write_blob({\n blob: blobVoiceRecording,\n directory: options.directory,\n fast_mode: true,\n path,\n recursive: true,\n });\n\n ({uri} = await Filesystem.getUri({directory: options.directory, path}));\n } else {\n recordDataBase64 = await VoiceRecorderImpl.blobToBase64(blobVoiceRecording);\n }\n\n const recordingDuration = await getBlobDuration(blobVoiceRecording);\n this.prepareInstanceForNextOperation();\n resolve({\n value: {\n recordDataBase64,\n mimeType: finalMimeType,\n msDuration: recordingDuration * 1000,\n uri\n }\n });\n };\n this.mediaRecorder.ondataavailable = (event: any) => this.chunks.push(event.data);\n this.mediaRecorder.start();\n });\n return successResponse();\n }\n\n /** Handles failures from getUserMedia. */\n private onFailedToStartRecording(): GenericResponse {\n this.prepareInstanceForNextOperation();\n throw failedToRecordError();\n }\n\n /** Converts a Blob payload into a base64 string. */\n private static blobToBase64(blob: Blob): Promise<Base64String> {\n return new Promise((resolve) => {\n const reader = new FileReader();\n reader.onloadend = () => {\n const recordingResult = String(reader.result);\n const splitResult = recordingResult.split('base64,');\n const toResolve = splitResult.length > 1 ? splitResult[1] : recordingResult;\n resolve(toResolve.trim());\n };\n reader.readAsDataURL(blob);\n });\n }\n\n /** Resets state for the next recording attempt. */\n private prepareInstanceForNextOperation(): void {\n if (this.mediaRecorder != null && this.mediaRecorder.state === 'recording') {\n try {\n this.mediaRecorder.stop();\n } catch (ignore) {\n console.warn('Failed to stop recording during cleanup');\n }\n }\n this.pendingResult = neverResolvingPromise();\n this.mediaRecorder = null;\n this.activeMimeType = null;\n this.chunks = [];\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"VoiceRecorderImpl.js","sourceRoot":"","sources":["../../../../src/platform/web/VoiceRecorderImpl.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,uBAAuB,CAAC;AACjD,OAAO,UAAU,MAAM,uBAAuB,CAAC;AAU/C,OAAO,eAAe,MAAM,qBAAqB,CAAC;AAClD,OAAO,EACH,qBAAqB,EACrB,kCAAkC,EAClC,4BAA4B,EAC5B,mBAAmB,EACnB,2BAA2B,EAC3B,mBAAmB,EACnB,eAAe,EACf,sBAAsB,EACtB,2BAA2B,EAC3B,eAAe,GAClB,MAAM,4BAA4B,CAAC;AAEpC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,mBAAmB,GAA2B;IAChD,mBAAmB;IACnB,8BAA8B,EAAE,MAAM,EAAO,8CAA8C;IAC3F,WAAW,EAAE,MAAM,EAA0B,mCAAmC;IAChF,WAAW,EAAE,MAAM,EAA0B,6CAA6C;IAC1F,YAAY,EAAE,MAAM,EAAyB,kBAAkB;IAC/D,WAAW,EAAE,MAAM,EAA0B,6BAA6B;IAE1E,4FAA4F;IAC5F,0BAA0B,EAAE,OAAO,EAAU,gDAAgD;IAC7F,wBAAwB,EAAE,OAAO,EAAY,oBAAoB;IACjE,YAAY,EAAE,OAAO,EAAwB,qDA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AAI,IAAI,CAAC,aAAa,CAAC,KAAK,KAAK,WAAW,EAAE,CAAC;YACzE,IAAI,CAAC;gBACD,IAAI,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC;YAC9B,CAAC;YAAC,OAAO,MAAM,EAAE,CAAC;gBACd,OAAO,CAAC,IAAI,CAAC,yCAAyC,CAAC,CAAC;YAC5D,CAAC;QACL,CAAC;QACD,IAAI,CAAC,aAAa,GAAG,qBAAqB,EAAE,CAAC;QAC7C,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC;QAC1B,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC;IACrB,CAAC;;AA5TD,oFAAoF;AAC5D,kDAAgC,GAAG,IAAI,AAAP,CAAQ","sourcesContent":["import {Filesystem} from '@capacitor/filesystem';\nimport write_blob from 'capacitor-blob-writer';\n\nimport type {\n Base64String,\n CurrentRecordingStatus,\n GenericResponse,\n RecordingData,\n RecordingOptions,\n} from '../../definitions';\n\nimport getBlobDuration from './get-blob-duration';\nimport {\n alreadyRecordingError,\n couldNotQueryPermissionStatusError,\n deviceCannotVoiceRecordError,\n emptyRecordingError,\n failedToFetchRecordingError,\n failedToRecordError,\n failureResponse,\n missingPermissionError,\n recordingHasNotStartedError,\n successResponse,\n} from './predefined-web-responses';\n\n/**\n * Ordered MIME types to probe for audio recording via `MediaRecorder.isTypeSupported()`.\n *\n * ⚠️ The order is intentional and MUST remain stable unless you also update the\n * selection policy in code and test on Safari/iOS + WebViews.\n *\n * ✅ What this list is used for\n * - Selecting a `mimeType` for `new MediaRecorder(stream, { mimeType })`.\n *\n * ❌ What this list does NOT guarantee\n * - It does NOT guarantee that the recorded output will be playable via the\n * HTML `<audio>` element in the same browser.\n *\n * Real-world caveat (important):\n * - We have observed cases where `MediaRecorder.isTypeSupported('audio/webm;codecs=opus')`\n * returned `true`, the recorder produced a Blob, but `<audio>` could not play it.\n * This can happen due to container/codec playback support differences, platform\n * quirks (especially Safari/iOS / WKWebView), or incomplete WebM playback support.\n *\n * Current selection behavior in this implementation:\n * - By default, MIME selection treats recorder support and playback support as separate\n * capabilities and probes both:\n * - Recorder capability: `MediaRecorder.isTypeSupported(type)`\n * - Playback capability: `audio.canPlayType(type)`\n * - This default can be disabled via `RecordingOptions.requirePlaybackSupport = false`\n * to fall back to recorder-only probing.\n *\n * Keeping legacy keys:\n * - Some entries are kept even if they overlap (e.g. `audio/mp4` and explicit codec),\n * to maximize compatibility across differing browser implementations.\n */\nconst POSSIBLE_MIME_TYPES: Record<string, string> = {\n // ✅ Most universal\n 'audio/mp4;codecs=\"mp4a.40.2\"': '.m4a', // AAC in MP4 (explicit codec helps detection)\n 'audio/mp4': '.m4a', // (legacy key kept; broad support)\n 'audio/aac': '.aac', // (legacy key kept; less common in the wild)\n 'audio/mpeg': '.mp3', // MP3 (universal)\n 'audio/wav': '.wav', // WAV (universal, big files)\n\n // ✅ Modern high-quality (very widely supported, but slightly less “universal” than MP3/AAC)\n 'audio/webm;codecs=\"opus\"': '.webm', // Opus in WebM (explicit codec helps detection)\n 'audio/webm;codecs=opus': '.webm', // (legacy key kept)\n 'audio/webm': '.webm', // (legacy key kept; container-only, codec-dependent)\n\n // ⚠️ Least universal (Safari/iOS historically the limiting factor)\n 'audio/ogg;codecs=opus': '.ogg', // (legacy key kept)\n 'audio/ogg;codecs=vorbis': '.ogg', // Ogg Vorbis (weakest mainstream support)\n};\n\n/** Creates a promise that never resolves. */\nconst neverResolvingPromise = (): Promise<any> => new Promise(() => undefined);\n\n/** Browser implementation backed by MediaRecorder and Capacitor Filesystem. */\nexport class VoiceRecorderImpl {\n /** Default behavior for web MIME selection: require recorder + playback support. */\n private static readonly DEFAULT_REQUIRE_PLAYBACK_SUPPORT = true;\n /** Active MediaRecorder instance, if recording. */\n private mediaRecorder: MediaRecorder | null = null;\n /** Collected data chunks from MediaRecorder. */\n private chunks: any[] = [];\n /** Promise resolved when the recorder stops and payload is ready. */\n private pendingResult: Promise<RecordingData> = neverResolvingPromise();\n\n /**\n * Returns whether the browser can start a recording session.\n *\n * On web this checks:\n * - `navigator.mediaDevices.getUserMedia`\n * - at least one supported recording MIME type using {@link getSupportedMimeType}\n *\n * The optional `requirePlaybackSupport` flag is forwarded to MIME selection and defaults\n * to `true` when omitted.\n */\n public static async canDeviceVoiceRecord(\n options?: Pick<RecordingOptions, 'requirePlaybackSupport'>,\n ): Promise<GenericResponse> {\n if (\n navigator?.mediaDevices?.getUserMedia == null ||\n VoiceRecorderImpl.getSupportedMimeType({\n requirePlaybackSupport: options?.requirePlaybackSupport,\n }) == null\n ) {\n return failureResponse();\n } else {\n return successResponse();\n }\n }\n\n /**\n * Starts a recording session using `MediaRecorder`.\n *\n * The selected MIME type is resolved once at start time (using the optional\n * `requirePlaybackSupport` flag from `RecordingOptions`) and reused for the final Blob\n * and file extension to keep the recording payload internally consistent.\n */\n public async startRecording(options?: RecordingOptions): Promise<GenericResponse> {\n if (this.mediaRecorder != null) {\n throw alreadyRecordingError();\n }\n const deviceCanRecord = await VoiceRecorderImpl.canDeviceVoiceRecord(options);\n if (!deviceCanRecord.value) {\n throw deviceCannotVoiceRecordError();\n }\n const havingPermission = await VoiceRecorderImpl.hasAudioRecordingPermission().catch(() => successResponse());\n if (!havingPermission.value) {\n throw missingPermissionError();\n }\n\n return navigator.mediaDevices\n .getUserMedia({audio: true})\n .then((stream) => this.onSuccessfullyStartedRecording(stream, options))\n .catch(this.onFailedToStartRecording.bind(this));\n }\n\n /** Stops the current recording and resolves the pending payload. */\n public async stopRecording(): Promise<RecordingData> {\n if (this.mediaRecorder == null) {\n throw recordingHasNotStartedError();\n }\n try {\n this.mediaRecorder.stop();\n this.mediaRecorder.stream.getTracks().forEach((track) => track.stop());\n return this.pendingResult;\n } catch (ignore) {\n throw failedToFetchRecordingError();\n } finally {\n this.prepareInstanceForNextOperation();\n }\n }\n\n /** Returns whether the browser has microphone permission. */\n public static async hasAudioRecordingPermission(): Promise<GenericResponse> {\n // Safari does not support navigator.permissions.query\n if (!navigator.permissions.query) {\n if (navigator.mediaDevices !== undefined) {\n return navigator.mediaDevices\n .getUserMedia({audio: true})\n .then(() => successResponse())\n .catch(() => {\n throw couldNotQueryPermissionStatusError();\n });\n }\n }\n return navigator.permissions\n .query({name: 'microphone' as any})\n .then((result) => ({value: result.state === 'granted'}))\n .catch(() => {\n throw couldNotQueryPermissionStatusError();\n });\n }\n\n /** Requests microphone permission from the browser. */\n public static async requestAudioRecordingPermission(): Promise<GenericResponse> {\n const havingPermission = await VoiceRecorderImpl.hasAudioRecordingPermission().catch(() => failureResponse());\n if (havingPermission.value) {\n return successResponse();\n }\n\n return navigator.mediaDevices\n .getUserMedia({audio: true})\n .then(() => successResponse())\n .catch(() => failureResponse());\n }\n\n /** Pauses the recording session when supported. */\n public pauseRecording(): Promise<GenericResponse> {\n if (this.mediaRecorder == null) {\n throw recordingHasNotStartedError();\n } else if (this.mediaRecorder.state === 'recording') {\n this.mediaRecorder.pause();\n return Promise.resolve(successResponse());\n } else {\n return Promise.resolve(failureResponse());\n }\n }\n\n /** Resumes a paused recording session when supported. */\n public resumeRecording(): Promise<GenericResponse> {\n if (this.mediaRecorder == null) {\n throw recordingHasNotStartedError();\n } else if (this.mediaRecorder.state === 'paused') {\n this.mediaRecorder.resume();\n return Promise.resolve(successResponse());\n } else {\n return Promise.resolve(failureResponse());\n }\n }\n\n /** Returns the current recording status from MediaRecorder. */\n public getCurrentStatus(): Promise<CurrentRecordingStatus> {\n if (this.mediaRecorder == null) {\n return Promise.resolve({status: 'NONE'});\n } else if (this.mediaRecorder.state === 'recording') {\n return Promise.resolve({status: 'RECORDING'});\n } else if (this.mediaRecorder.state === 'paused') {\n return Promise.resolve({status: 'PAUSED'});\n } else {\n return Promise.resolve({status: 'NONE'});\n }\n }\n\n /**\n * Returns the first MIME type (key of {@link POSSIBLE_MIME_TYPES}) that the current\n * environment reports as supported for recording via `MediaRecorder.isTypeSupported()`,\n * optionally requiring native HTML `<audio>` playback support too.\n *\n * The search order is the iteration order of {@link POSSIBLE_MIME_TYPES}.\n *\n * @typeParam T - A MIME type string that exists as a key in {@link POSSIBLE_MIME_TYPES}.\n *\n * @returns The first supported MIME type for `MediaRecorder`, or `null` if:\n * - `MediaRecorder` is unavailable, or\n * - no configured MIME types are supported.\n *\n * ⚠️ Important: `MediaRecorder` support ≠ `<audio>` playback support\n *\n * Some browsers/platforms can claim support for recording a format (notably WebM/Opus)\n * but still fail to play the resulting Blob through the native HTML audio pipeline.\n * This mismatch is especially likely on Safari/iOS / WKWebView variants, so the default\n * behavior also probes `HTMLAudioElement.canPlayType(type)` when available.\n *\n * Selection policy when playback probing is enabled:\n * - keep the global priority order from {@link POSSIBLE_MIME_TYPES}\n * - among recordable types, prefer the first `\"probably\"` playable candidate\n * - otherwise return the first `\"maybe\"` playable candidate\n * - treat `\"\"` as not playable\n *\n * Note: The <audio> element is never attached to the DOM, so it won't appear to users or assistive tech.\n *\n * Fallback behavior:\n * - If `document` / `audio.canPlayType` is unavailable (e.g. SSR-like environments),\n * this falls back to record-only probing.\n */\n public static getSupportedMimeType<T extends keyof typeof POSSIBLE_MIME_TYPES>(\n options?: { requirePlaybackSupport?: boolean },\n ): T | null {\n if (MediaRecorder?.isTypeSupported == null) return null;\n\n const orderedTypes = Object.keys(POSSIBLE_MIME_TYPES) as T[];\n const recordSupportedTypes = orderedTypes.filter((type) => MediaRecorder.isTypeSupported(type));\n if (recordSupportedTypes.length === 0) return null;\n\n const requirePlaybackSupport =\n options?.requirePlaybackSupport ?? VoiceRecorderImpl.DEFAULT_REQUIRE_PLAYBACK_SUPPORT;\n if (!requirePlaybackSupport) {\n return recordSupportedTypes[0] ?? null;\n }\n\n if (typeof document === 'undefined' || typeof document.createElement !== 'function') {\n return recordSupportedTypes[0] ?? null;\n }\n\n const audioElement = document.createElement('audio') as Partial<HTMLAudioElement>;\n if (typeof audioElement.canPlayType !== 'function') {\n return recordSupportedTypes[0] ?? null;\n }\n\n let firstProbably: T | null = null;\n let firstMaybe: T | null = null;\n\n for (const type of recordSupportedTypes) {\n const playbackSupport = audioElement.canPlayType(type);\n if (playbackSupport === 'probably') {\n firstProbably = type;\n break;\n }\n if (playbackSupport === 'maybe' && firstMaybe == null) {\n firstMaybe = type;\n }\n }\n\n return firstProbably ?? firstMaybe ?? null;\n }\n\n /** Initializes MediaRecorder and wires up handlers. */\n private onSuccessfullyStartedRecording(stream: MediaStream, options?: RecordingOptions): GenericResponse {\n this.pendingResult = new Promise((resolve, reject) => {\n const mimeType = VoiceRecorderImpl.getSupportedMimeType({\n requirePlaybackSupport: options?.requirePlaybackSupport,\n });\n if (mimeType == null) {\n this.prepareInstanceForNextOperation();\n reject(failedToRecordError());\n return;\n }\n\n this.mediaRecorder = new MediaRecorder(stream, {mimeType});\n this.mediaRecorder.onerror = () => {\n this.prepareInstanceForNextOperation();\n reject(failedToRecordError());\n };\n this.mediaRecorder.onstop = async () => {\n const mt = this.mediaRecorder?.mimeType ?? mimeType;\n const blobVoiceRecording = new Blob(this.chunks, {type: mt});\n if (blobVoiceRecording.size <= 0) {\n this.prepareInstanceForNextOperation();\n reject(emptyRecordingError());\n return;\n }\n\n let uri: string | undefined = undefined;\n let recordDataBase64 = '';\n const fileExtension = (POSSIBLE_MIME_TYPES[mimeType] ?? '').replace(/^\\./, '');\n if (options?.directory) {\n const subDirectory = options.subDirectory?.match(/^\\/?(.+[^/])\\/?$/)?.[1] ?? '';\n const path = `${subDirectory}/recording-${new Date().getTime()}${POSSIBLE_MIME_TYPES[mt]}`;\n\n await write_blob({\n blob: blobVoiceRecording,\n directory: options.directory,\n fast_mode: true,\n path,\n recursive: true,\n });\n\n ({uri} = await Filesystem.getUri({directory: options.directory, path}));\n } else {\n recordDataBase64 = await VoiceRecorderImpl.blobToBase64(blobVoiceRecording);\n }\n\n const recordingDuration = await getBlobDuration(blobVoiceRecording);\n this.prepareInstanceForNextOperation();\n resolve({\n value: {\n recordDataBase64,\n mimeType: mt,\n fileExtension,\n msDuration: recordingDuration * 1000,\n uri\n }\n });\n };\n this.mediaRecorder.ondataavailable = (event: any) => this.chunks.push(event.data);\n this.mediaRecorder.start();\n });\n return successResponse();\n }\n\n /** Handles failures from getUserMedia. */\n private onFailedToStartRecording(): GenericResponse {\n this.prepareInstanceForNextOperation();\n throw failedToRecordError();\n }\n\n /** Converts a Blob payload into a base64 string. */\n private static blobToBase64(blob: Blob): Promise<Base64String> {\n return new Promise((resolve) => {\n const reader = new FileReader();\n reader.onloadend = () => {\n const recordingResult = String(reader.result);\n const splitResult = recordingResult.split('base64,');\n const toResolve = splitResult.length > 1 ? splitResult[1] : recordingResult;\n resolve(toResolve.trim());\n };\n reader.readAsDataURL(blob);\n });\n }\n\n /** Resets state for the next recording attempt. */\n private prepareInstanceForNextOperation(): void {\n if (this.mediaRecorder != null && this.mediaRecorder.state === 'recording') {\n try {\n this.mediaRecorder.stop();\n } catch (ignore) {\n console.warn('Failed to stop recording during cleanup');\n }\n }\n this.pendingResult = neverResolvingPromise();\n this.mediaRecorder = null;\n this.chunks = [];\n }\n}\n"]}
|
package/dist/plugin.cjs.js
CHANGED
|
@@ -130,8 +130,6 @@ class VoiceRecorderImpl {
|
|
|
130
130
|
constructor() {
|
|
131
131
|
/** Active MediaRecorder instance, if recording. */
|
|
132
132
|
this.mediaRecorder = null;
|
|
133
|
-
/** MIME type selected at start time for the active recording. */
|
|
134
|
-
this.activeMimeType = null;
|
|
135
133
|
/** Collected data chunks from MediaRecorder. */
|
|
136
134
|
this.chunks = [];
|
|
137
135
|
/** Promise resolved when the recorder stops and payload is ready. */
|
|
@@ -348,21 +346,15 @@ class VoiceRecorderImpl {
|
|
|
348
346
|
reject(failedToRecordError());
|
|
349
347
|
return;
|
|
350
348
|
}
|
|
351
|
-
this.activeMimeType = mimeType;
|
|
352
349
|
this.mediaRecorder = new MediaRecorder(stream, { mimeType });
|
|
353
350
|
this.mediaRecorder.onerror = () => {
|
|
354
351
|
this.prepareInstanceForNextOperation();
|
|
355
352
|
reject(failedToRecordError());
|
|
356
353
|
};
|
|
357
354
|
this.mediaRecorder.onstop = async () => {
|
|
358
|
-
var _a, _b, _c;
|
|
359
|
-
const
|
|
360
|
-
|
|
361
|
-
this.prepareInstanceForNextOperation();
|
|
362
|
-
reject(failedToFetchRecordingError());
|
|
363
|
-
return;
|
|
364
|
-
}
|
|
365
|
-
const blobVoiceRecording = new Blob(this.chunks, { type: finalMimeType });
|
|
355
|
+
var _a, _b, _c, _d, _e, _f;
|
|
356
|
+
const mt = (_b = (_a = this.mediaRecorder) === null || _a === void 0 ? void 0 : _a.mimeType) !== null && _b !== void 0 ? _b : mimeType;
|
|
357
|
+
const blobVoiceRecording = new Blob(this.chunks, { type: mt });
|
|
366
358
|
if (blobVoiceRecording.size <= 0) {
|
|
367
359
|
this.prepareInstanceForNextOperation();
|
|
368
360
|
reject(emptyRecordingError());
|
|
@@ -370,9 +362,10 @@ class VoiceRecorderImpl {
|
|
|
370
362
|
}
|
|
371
363
|
let uri = undefined;
|
|
372
364
|
let recordDataBase64 = '';
|
|
365
|
+
const fileExtension = ((_c = POSSIBLE_MIME_TYPES[mimeType]) !== null && _c !== void 0 ? _c : '').replace(/^\./, '');
|
|
373
366
|
if (options === null || options === void 0 ? void 0 : options.directory) {
|
|
374
|
-
const subDirectory = (
|
|
375
|
-
const path = `${subDirectory}/recording-${new Date().getTime()}${POSSIBLE_MIME_TYPES[
|
|
367
|
+
const subDirectory = (_f = (_e = (_d = options.subDirectory) === null || _d === void 0 ? void 0 : _d.match(/^\/?(.+[^/])\/?$/)) === null || _e === void 0 ? void 0 : _e[1]) !== null && _f !== void 0 ? _f : '';
|
|
368
|
+
const path = `${subDirectory}/recording-${new Date().getTime()}${POSSIBLE_MIME_TYPES[mt]}`;
|
|
376
369
|
await write_blob({
|
|
377
370
|
blob: blobVoiceRecording,
|
|
378
371
|
directory: options.directory,
|
|
@@ -390,7 +383,8 @@ class VoiceRecorderImpl {
|
|
|
390
383
|
resolve({
|
|
391
384
|
value: {
|
|
392
385
|
recordDataBase64,
|
|
393
|
-
mimeType:
|
|
386
|
+
mimeType: mt,
|
|
387
|
+
fileExtension,
|
|
394
388
|
msDuration: recordingDuration * 1000,
|
|
395
389
|
uri
|
|
396
390
|
}
|
|
@@ -431,7 +425,6 @@ class VoiceRecorderImpl {
|
|
|
431
425
|
}
|
|
432
426
|
this.pendingResult = neverResolvingPromise();
|
|
433
427
|
this.mediaRecorder = null;
|
|
434
|
-
this.activeMimeType = null;
|
|
435
428
|
this.chunks = [];
|
|
436
429
|
}
|
|
437
430
|
}
|
|
@@ -518,8 +511,8 @@ const attachCanonicalErrorCode = (error) => {
|
|
|
518
511
|
|
|
519
512
|
/** Normalizes recording payloads into a stable contract shape. */
|
|
520
513
|
const normalizeRecordingData = (data) => {
|
|
521
|
-
const { recordDataBase64, uri, msDuration, mimeType } = data.value;
|
|
522
|
-
const normalizedValue = { msDuration, mimeType };
|
|
514
|
+
const { recordDataBase64, uri, msDuration, mimeType, fileExtension } = data.value;
|
|
515
|
+
const normalizedValue = { msDuration, mimeType, fileExtension };
|
|
523
516
|
const trimmedUri = typeof uri === 'string' && uri.length > 0 ? uri : undefined;
|
|
524
517
|
const trimmedBase64 = typeof recordDataBase64 === 'string' && recordDataBase64.length > 0 ? recordDataBase64 : undefined;
|
|
525
518
|
if (trimmedUri) {
|