/
Recording.js
295 lines (295 loc) · 13.2 KB
/
Recording.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import { EventEmitter, Platform } from '@unimodules/core';
import { PermissionStatus } from 'unimodules-permissions-interface';
import { _DEFAULT_PROGRESS_UPDATE_INTERVAL_MILLIS, } from '../AV';
import ExponentAV from '../ExponentAV';
import { isAudioEnabled, throwIfAudioIsDisabled } from './AudioAvailability';
import { Sound } from './Sound';
// TODO: consider changing these to enums
export const RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_DEFAULT = 0;
export const RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_THREE_GPP = 1;
export const RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_MPEG_4 = 2;
export const RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_AMR_NB = 3;
export const RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_AMR_WB = 4;
export const RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_AAC_ADIF = 5;
export const RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_AAC_ADTS = 6;
export const RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_RTP_AVP = 7;
export const RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_MPEG2TS = 8;
export const RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_WEBM = 9;
export const RECORDING_OPTION_ANDROID_AUDIO_ENCODER_DEFAULT = 0;
export const RECORDING_OPTION_ANDROID_AUDIO_ENCODER_AMR_NB = 1;
export const RECORDING_OPTION_ANDROID_AUDIO_ENCODER_AMR_WB = 2;
export const RECORDING_OPTION_ANDROID_AUDIO_ENCODER_AAC = 3;
export const RECORDING_OPTION_ANDROID_AUDIO_ENCODER_HE_AAC = 4;
export const RECORDING_OPTION_ANDROID_AUDIO_ENCODER_AAC_ELD = 5;
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_LINEARPCM = 'lpcm';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_AC3 = 'ac-3';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_60958AC3 = 'cac3';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_APPLEIMA4 = 'ima4';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_MPEG4AAC = 'aac ';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_MPEG4CELP = 'celp';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_MPEG4HVXC = 'hvxc';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_MPEG4TWINVQ = 'twvq';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_MACE3 = 'MAC3';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_MACE6 = 'MAC6';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_ULAW = 'ulaw';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_ALAW = 'alaw';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_QDESIGN = 'QDMC';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_QDESIGN2 = 'QDM2';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_QUALCOMM = 'Qclp';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_MPEGLAYER1 = '.mp1';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_MPEGLAYER2 = '.mp2';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_MPEGLAYER3 = '.mp3';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_APPLELOSSLESS = 'alac';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_MPEG4AAC_HE = 'aach';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_MPEG4AAC_LD = 'aacl';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_MPEG4AAC_ELD = 'aace';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_MPEG4AAC_ELD_SBR = 'aacf';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_MPEG4AAC_ELD_V2 = 'aacg';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_MPEG4AAC_HE_V2 = 'aacp';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_MPEG4AAC_SPATIAL = 'aacs';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_AMR = 'samr';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_AMR_WB = 'sawb';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_AUDIBLE = 'AUDB';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_ILBC = 'ilbc';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_DVIINTELIMA = 0x6d730011;
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_MICROSOFTGSM = 0x6d730031;
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_AES3 = 'aes3';
export const RECORDING_OPTION_IOS_OUTPUT_FORMAT_ENHANCEDAC3 = 'ec-3';
export const RECORDING_OPTION_IOS_AUDIO_QUALITY_MIN = 0;
export const RECORDING_OPTION_IOS_AUDIO_QUALITY_LOW = 0x20;
export const RECORDING_OPTION_IOS_AUDIO_QUALITY_MEDIUM = 0x40;
export const RECORDING_OPTION_IOS_AUDIO_QUALITY_HIGH = 0x60;
export const RECORDING_OPTION_IOS_AUDIO_QUALITY_MAX = 0x7f;
export const RECORDING_OPTION_IOS_BIT_RATE_STRATEGY_CONSTANT = 0;
export const RECORDING_OPTION_IOS_BIT_RATE_STRATEGY_LONG_TERM_AVERAGE = 1;
export const RECORDING_OPTION_IOS_BIT_RATE_STRATEGY_VARIABLE_CONSTRAINED = 2;
export const RECORDING_OPTION_IOS_BIT_RATE_STRATEGY_VARIABLE = 3;
// TODO : maybe make presets for music and speech, or lossy / lossless.
export const RECORDING_OPTIONS_PRESET_HIGH_QUALITY = {
android: {
extension: '.m4a',
outputFormat: RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_MPEG_4,
audioEncoder: RECORDING_OPTION_ANDROID_AUDIO_ENCODER_AAC,
sampleRate: 44100,
numberOfChannels: 2,
bitRate: 128000,
},
ios: {
extension: '.caf',
audioQuality: RECORDING_OPTION_IOS_AUDIO_QUALITY_MAX,
sampleRate: 44100,
numberOfChannels: 2,
bitRate: 128000,
linearPCMBitDepth: 16,
linearPCMIsBigEndian: false,
linearPCMIsFloat: false,
},
};
export const RECORDING_OPTIONS_PRESET_LOW_QUALITY = {
android: {
extension: '.3gp',
outputFormat: RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_THREE_GPP,
audioEncoder: RECORDING_OPTION_ANDROID_AUDIO_ENCODER_AMR_NB,
sampleRate: 44100,
numberOfChannels: 2,
bitRate: 128000,
},
ios: {
extension: '.caf',
audioQuality: RECORDING_OPTION_IOS_AUDIO_QUALITY_MIN,
sampleRate: 44100,
numberOfChannels: 2,
bitRate: 128000,
linearPCMBitDepth: 16,
linearPCMIsBigEndian: false,
linearPCMIsFloat: false,
},
};
export { PermissionStatus };
let _recorderExists = false;
const eventEmitter = Platform.OS === 'android' ? new EventEmitter(ExponentAV) : null;
export async function getPermissionsAsync() {
return ExponentAV.getPermissionsAsync();
}
export async function requestPermissionsAsync() {
return ExponentAV.requestPermissionsAsync();
}
export class Recording {
constructor() {
this._subscription = null;
this._canRecord = false;
this._isDoneRecording = false;
this._finalDurationMillis = 0;
this._uri = null;
this._onRecordingStatusUpdate = null;
this._progressUpdateTimeoutVariable = null;
this._progressUpdateIntervalMillis = _DEFAULT_PROGRESS_UPDATE_INTERVAL_MILLIS;
this._options = null;
// Internal methods
this._cleanupForUnloadedRecorder = async (finalStatus) => {
this._canRecord = false;
this._isDoneRecording = true;
this._finalDurationMillis = finalStatus?.durationMillis || 0;
_recorderExists = false;
if (this._subscription) {
this._subscription.remove();
this._subscription = null;
}
this._disablePolling();
return await this.getStatusAsync(); // Automatically calls onRecordingStatusUpdate for the final state.
};
this._pollingLoop = async () => {
if (isAudioEnabled() && this._canRecord && this._onRecordingStatusUpdate != null) {
this._progressUpdateTimeoutVariable = setTimeout(this._pollingLoop, this._progressUpdateIntervalMillis);
try {
await this.getStatusAsync();
}
catch (error) {
this._disablePolling();
}
}
};
// Note that all calls automatically call onRecordingStatusUpdate as a side effect.
// Get status API
this.getStatusAsync = async () => {
// Automatically calls onRecordingStatusUpdate.
if (this._canRecord) {
return this._performOperationAndHandleStatusAsync(() => ExponentAV.getAudioRecordingStatus());
}
const status = {
canRecord: false,
isRecording: false,
isDoneRecording: this._isDoneRecording,
durationMillis: this._finalDurationMillis,
};
this._callOnRecordingStatusUpdateForNewStatus(status);
return status;
};
}
_disablePolling() {
if (this._progressUpdateTimeoutVariable != null) {
clearTimeout(this._progressUpdateTimeoutVariable);
this._progressUpdateTimeoutVariable = null;
}
}
_enablePollingIfNecessaryAndPossible() {
if (isAudioEnabled() && this._canRecord && this._onRecordingStatusUpdate != null) {
this._disablePolling();
this._pollingLoop();
}
}
_callOnRecordingStatusUpdateForNewStatus(status) {
if (this._onRecordingStatusUpdate != null) {
this._onRecordingStatusUpdate(status);
}
}
async _performOperationAndHandleStatusAsync(operation) {
throwIfAudioIsDisabled();
if (this._canRecord) {
const status = await operation();
this._callOnRecordingStatusUpdateForNewStatus(status);
return status;
}
else {
throw new Error('Cannot complete operation because this recorder is not ready to record.');
}
}
setOnRecordingStatusUpdate(onRecordingStatusUpdate) {
this._onRecordingStatusUpdate = onRecordingStatusUpdate;
if (onRecordingStatusUpdate == null) {
this._disablePolling();
}
else {
this._enablePollingIfNecessaryAndPossible();
}
this.getStatusAsync();
}
setProgressUpdateInterval(progressUpdateIntervalMillis) {
this._progressUpdateIntervalMillis = progressUpdateIntervalMillis;
this.getStatusAsync();
}
// Record API
async prepareToRecordAsync(options = RECORDING_OPTIONS_PRESET_LOW_QUALITY) {
throwIfAudioIsDisabled();
if (_recorderExists) {
throw new Error('Only one Recording object can be prepared at a given time.');
}
if (this._isDoneRecording) {
throw new Error('This Recording object is done recording; you must make a new one.');
}
if (!options || !options.android || !options.ios) {
throw new Error('You must provide recording options for android and ios in order to prepare to record.');
}
const extensionRegex = /^\.\w+$/;
if (!options.android.extension ||
!options.ios.extension ||
!extensionRegex.test(options.android.extension) ||
!extensionRegex.test(options.ios.extension)) {
throw new Error(`Your file extensions must match ${extensionRegex.toString()}.`);
}
if (!this._canRecord) {
if (eventEmitter) {
this._subscription = eventEmitter.addListener('Expo.Recording.recorderUnloaded', this._cleanupForUnloadedRecorder);
}
const { uri, status, } = await ExponentAV.prepareAudioRecorder(options);
_recorderExists = true;
this._uri = uri;
this._options = options;
this._canRecord = true;
const currentStatus = { ...status, canRecord: true };
this._callOnRecordingStatusUpdateForNewStatus(currentStatus);
this._enablePollingIfNecessaryAndPossible();
return currentStatus;
}
else {
throw new Error('This Recording object is already prepared to record.');
}
}
async startAsync() {
return this._performOperationAndHandleStatusAsync(() => ExponentAV.startAudioRecording());
}
async pauseAsync() {
return this._performOperationAndHandleStatusAsync(() => ExponentAV.pauseAudioRecording());
}
async stopAndUnloadAsync() {
if (!this._canRecord) {
if (this._isDoneRecording) {
throw new Error('Cannot unload a Recording that has already been unloaded.');
}
else {
throw new Error('Cannot unload a Recording that has not been prepared.');
}
}
// We perform a separate native API call so that the state of the Recording can be updated with
// the final duration of the recording. (We cast stopStatus as Object to appease Flow)
let stopResult;
let stopError;
try {
stopResult = await ExponentAV.stopAudioRecording();
}
catch (err) {
stopError = err;
}
// Clean-up and return status
await ExponentAV.unloadAudioRecorder();
const status = await this._cleanupForUnloadedRecorder(stopResult);
return stopError ? Promise.reject(stopError) : status;
}
// Read API
getURI() {
return this._uri;
}
async createNewLoadedSound(initialStatus = {}, onPlaybackStatusUpdate = null) {
console.warn(`createNewLoadedSound is deprecated in favor of createNewLoadedSoundAsync, which has the same API aside from the method name`);
return this.createNewLoadedSoundAsync(initialStatus, onPlaybackStatusUpdate);
}
async createNewLoadedSoundAsync(initialStatus = {}, onPlaybackStatusUpdate = null) {
if (this._uri == null || !this._isDoneRecording) {
throw new Error('Cannot create sound when the Recording has not finished!');
}
return Sound.createAsync(
// $FlowFixMe: Flow can't distinguish between this literal and Asset
{ uri: this._uri }, initialStatus, onPlaybackStatusUpdate, false);
}
}
//# sourceMappingURL=Recording.js.map