@react-native-ohos/react-native-audio-recorder-player 3.6.11-rc.2 → 3.6.12

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.
Files changed (37) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/LICENSE +21 -21
  3. package/README.OpenSource +10 -10
  4. package/README.md +899 -14
  5. package/harmony/audio_recorder_player/BuildProfile.ets +17 -0
  6. package/harmony/audio_recorder_player/LICENSE +21 -21
  7. package/harmony/audio_recorder_player/ResourceTable.txt +1 -1
  8. package/harmony/audio_recorder_player/build-profile.json5 +28 -28
  9. package/harmony/audio_recorder_player/index.ets +27 -26
  10. package/harmony/audio_recorder_player/oh-package-lock.json5 +29 -0
  11. package/harmony/audio_recorder_player/oh-package.json5 +20 -20
  12. package/harmony/audio_recorder_player/src/main/cpp/CMakeLists.txt +7 -7
  13. package/harmony/audio_recorder_player/src/main/cpp/RNAudioRecorderPlayerPackage.h +52 -52
  14. package/harmony/audio_recorder_player/src/main/cpp/RNAudioRecorderPlayerTurboModule.cpp +92 -92
  15. package/harmony/audio_recorder_player/src/main/cpp/RNAudioRecorderPlayerTurboModule.h +37 -37
  16. package/harmony/audio_recorder_player/src/main/ets/RNAudioRecorderPlayerPackage.ets +48 -48
  17. package/harmony/audio_recorder_player/src/main/ets/RNAudioRecorderPlayerTurboModule.ts +316 -310
  18. package/harmony/audio_recorder_player/src/main/ets/audioPlayer/AudioPlayer.ts +419 -414
  19. package/harmony/audio_recorder_player/src/main/ets/audioPlayer/types.ts +43 -43
  20. package/harmony/audio_recorder_player/src/main/ets/index.d.ts +209 -209
  21. package/harmony/audio_recorder_player/src/main/ets/recorder/Recorder.ts +305 -299
  22. package/harmony/audio_recorder_player/src/main/ets/recorder/types.ts +61 -61
  23. package/harmony/audio_recorder_player/src/main/ets/utils/DateTimeUtils.ts +88 -88
  24. package/harmony/audio_recorder_player/src/main/ets/utils/Logger.ts +45 -45
  25. package/harmony/audio_recorder_player/src/main/ets/utils/SaveAudioAsset.ts +101 -105
  26. package/harmony/audio_recorder_player/src/main/module.json5 +9 -9
  27. package/harmony/audio_recorder_player/src/main/resources/base/element/string.json +8 -8
  28. package/harmony/audio_recorder_player/src/main/resources/base/media/ic_public_error.svg +9 -9
  29. package/harmony/audio_recorder_player/src/main/resources/en_US/element/string.json +8 -8
  30. package/harmony/audio_recorder_player/src/main/resources/zh_CN/element/string.json +8 -8
  31. package/harmony/audio_recorder_player/ts.ets +26 -26
  32. package/harmony/audio_recorder_player.har +0 -0
  33. package/index.d.ts +227 -227
  34. package/index.ts +530 -525
  35. package/package.json +77 -69
  36. package/src/NativeAudioRecorderPlayerModule.ts +41 -41
  37. package/RNAudioRecorderPlayer.podspec +0 -21
package/index.ts CHANGED
@@ -1,525 +1,530 @@
1
- /*
2
- * Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
3
- * Use of this source code is governed by a MIT license that can be
4
- * found in the LICENSE file.
5
- */
6
-
7
- import type { EmitterSubscription } from 'react-native';
8
- import {
9
- DeviceEventEmitter,
10
- NativeEventEmitter,
11
- NativeModules,
12
- Platform,
13
- TurboModuleRegistry
14
- } from 'react-native';
15
-
16
-
17
- const RNAudioRecorderPlayer = TurboModuleRegistry ? require('./src/NativeAudioRecorderPlayerModule').default : NativeModules.RNAudioRecorderPlayer;
18
-
19
- export enum AudioSourceHarmonyType {
20
- DEFAULT = 0,
21
- MIC,
22
- VOICE_RECOGNITION,
23
- VOICE_COMMUNICATION = 7,
24
- VOICE_MESSAGE = 10,
25
- CAMCORDER = 12,
26
- }
27
-
28
- export enum AudioFormatHarmonyType {
29
- MPEG_4 = 'mp4',
30
- MPEG_4A = 'm4a',
31
- MP3 = 'mp3',
32
- WAV = 'wav',
33
- AAC = 'aac',
34
- }
35
-
36
- export enum AudioMimeHarmonyType {
37
- AUDIO_AAC = 'audio/mp4a-latm', // 表示音频/mp4a-latm类型。
38
-
39
- AUDIO_VORBIS = 'audio/vorbis', // 表示音频/vorbis类型。
40
- AUDIO_FLAC = 'audio/flac', // 表示音频/flac类型。
41
- AUDIO_MP3 = 'audio/mpeg', // 表示音频/mpeg类型。
42
- AUDIO_G711MU = 'audio/g711mu', // 表示音频/G711-mulaw类型。
43
- }
44
-
45
- export enum AudioSourceAndroidType {
46
- DEFAULT = 0,
47
- MIC,
48
- VOICE_UPLINK,
49
- VOICE_DOWNLINK,
50
- VOICE_CALL,
51
- CAMCORDER,
52
- VOICE_RECOGNITION,
53
- VOICE_COMMUNICATION,
54
- REMOTE_SUBMIX,
55
- UNPROCESSED,
56
- RADIO_TUNER = 1998,
57
- HOTWORD,
58
- }
59
-
60
- export enum OutputFormatAndroidType {
61
- DEFAULT = 0,
62
- THREE_GPP,
63
- MPEG_4,
64
- AMR_NB,
65
- AMR_WB,
66
- AAC_ADIF,
67
- AAC_ADTS,
68
- OUTPUT_FORMAT_RTP_AVP,
69
- MPEG_2_TS,
70
- WEBM,
71
- UNUSED,
72
- OGG,
73
- }
74
-
75
- export enum AudioEncoderAndroidType {
76
- DEFAULT = 0,
77
- AMR_NB,
78
- AMR_WB,
79
- AAC,
80
- HE_AAC,
81
- AAC_ELD,
82
- VORBIS,
83
- OPUS,
84
- }
85
-
86
- export enum AVEncodingOption {
87
- lpcm = 'lpcm',
88
- ima4 = 'ima4',
89
- aac = 'aac',
90
- MAC3 = 'MAC3',
91
- MAC6 = 'MAC6',
92
- ulaw = 'ulaw',
93
- alaw = 'alaw',
94
- mp1 = 'mp1',
95
- mp2 = 'mp2',
96
- mp4 = 'mp4',
97
- alac = 'alac',
98
- amr = 'amr',
99
- flac = 'flac',
100
- opus = 'opus',
101
- wav = 'wav',
102
- }
103
-
104
- type AVEncodingType =
105
- | AVEncodingOption.lpcm
106
- | AVEncodingOption.ima4
107
- | AVEncodingOption.aac
108
- | AVEncodingOption.MAC3
109
- | AVEncodingOption.MAC6
110
- | AVEncodingOption.ulaw
111
- | AVEncodingOption.alaw
112
- | AVEncodingOption.mp1
113
- | AVEncodingOption.mp2
114
- | AVEncodingOption.mp4
115
- | AVEncodingOption.alac
116
- | AVEncodingOption.amr
117
- | AVEncodingOption.flac
118
- | AVEncodingOption.opus
119
- | AVEncodingOption.wav;
120
-
121
- export enum AVModeIOSOption {
122
- gamechat = 'gamechat',
123
- measurement = 'measurement',
124
- movieplayback = 'movieplayback',
125
- spokenaudio = 'spokenaudio',
126
- videochat = 'videochat',
127
- videorecording = 'videorecording',
128
- voicechat = 'voicechat',
129
- voiceprompt = 'voiceprompt',
130
- }
131
-
132
- export type AVModeIOSType =
133
- | AVModeIOSOption.gamechat
134
- | AVModeIOSOption.measurement
135
- | AVModeIOSOption.movieplayback
136
- | AVModeIOSOption.spokenaudio
137
- | AVModeIOSOption.videochat
138
- | AVModeIOSOption.videorecording
139
- | AVModeIOSOption.voicechat
140
- | AVModeIOSOption.voiceprompt;
141
-
142
- export enum AVEncoderAudioQualityIOSType {
143
- min = 0,
144
- low = 32,
145
- medium = 64,
146
- high = 96,
147
- max = 127,
148
- }
149
-
150
- export enum AVLinearPCMBitDepthKeyIOSType {
151
- 'bit8' = 8,
152
- 'bit16' = 16,
153
- 'bit24' = 24,
154
- 'bit32' = 32,
155
- }
156
-
157
- export interface AudioSet {
158
- AVSampleRateKeyIOS?: number;
159
- AVFormatIDKeyIOS?: AVEncodingType;
160
- AVModeIOS?: AVModeIOSType;
161
- AVNumberOfChannelsKeyIOS?: number;
162
- AVEncoderAudioQualityKeyIOS?: AVEncoderAudioQualityIOSType;
163
- AudioSourceAndroid?: AudioSourceAndroidType;
164
- AVLinearPCMBitDepthKeyIOS?: AVLinearPCMBitDepthKeyIOSType;
165
- AVLinearPCMIsBigEndianKeyIOS?: boolean;
166
- AVLinearPCMIsFloatKeyIOS?: boolean;
167
- AVLinearPCMIsNonInterleavedIOS?: boolean;
168
- AVEncoderBitRateKeyIOS?: number;
169
- OutputFormatAndroid?: OutputFormatAndroidType;
170
- AudioEncoderAndroid?: AudioEncoderAndroidType;
171
- AudioEncodingBitRateAndroid?: number;
172
- AudioSamplingRateAndroid?: number;
173
- AudioChannelsAndroid?: number;
174
- AudioSourceHarmony?: AudioSourceHarmonyType;
175
- AudioMimeHarmony?: AudioMimeHarmonyType;
176
- AudioFileFormatHarmony?: AudioFormatHarmonyType;
177
- /**
178
- * 音频编码比特率,选择音频录制时必填。
179
-
180
- 支持范围:
181
-
182
- - AAC编码格式支持比特率范围[32000 - 500000]。
183
-
184
- - G711-mulaw编码格式支持比特率范围[64000 - 64000]。
185
-
186
- - MP3编码格式支持范围[8000, 16000, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000]。
187
-
188
- 当使用MP3编码格式时,采样率和比特率的映射关系:
189
-
190
- - 采样率使用16K以下时,对应比特率范围为[8kbps - 64kbps]。
191
-
192
- - 采样率使用16K~32K时对应的比特率范围为[8kbps - 160kbps]。
193
-
194
- - 采样率使用32K以上时对应的比特率范围为[32kbps - 320kbps]。
195
- */
196
- AudioEncodingBitRateHarmony?: number;
197
- /**
198
- * 支持范围:
199
-
200
- - AAC编码支持采样率范围[8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000]。
201
-
202
- - G711-mulaw编码支持采样率范围[8000 - 8000]。
203
-
204
- - MP3编码支持采样率范围[8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000]。
205
- */
206
- AudioSamplingRateHarmony?: number;
207
- /**
208
- * 音频采集声道数
209
-
210
- - AAC编码格式支持范围[1 - 8]。
211
-
212
- - G711-mulaw编码格式支持范围[1 - 1]。
213
-
214
- - MP3编码格式支持范围[1 - 2]
215
- */
216
- AudioChannelsHarmony?: number;
217
- }
218
-
219
- const pad = (num: number): string => {
220
- return ('0' + num).slice(-2);
221
- };
222
-
223
- export type RecordBackType = {
224
- isRecording?: boolean;
225
- currentPosition: number;
226
- currentMetering?: number;
227
- };
228
-
229
- export type PlayBackType = {
230
- isMuted?: boolean;
231
- currentPosition: number;
232
- duration: number;
233
- isFinished: boolean;
234
- };
235
-
236
- class AudioRecorderPlayer {
237
- private _isRecording: boolean;
238
- private _isPlaying: boolean;
239
- private _hasPaused: boolean;
240
- private _hasPausedRecord: boolean;
241
- private _recorderSubscription: null | EmitterSubscription;
242
- private _playerSubscription: EmitterSubscription;
243
- private _playerCallback: null | ((event: PlayBackType) => void);
244
-
245
- mmss = (secs: number): string => {
246
- let minutes = Math.floor(secs / 60);
247
-
248
- secs = secs % 60;
249
- minutes = minutes % 60;
250
-
251
- return pad(minutes) + ':' + pad(secs);
252
- };
253
-
254
- mmssss = (milisecs: number): string => {
255
- const secs = Math.floor(milisecs / 1000);
256
- const minutes = Math.floor(secs / 60);
257
- const seconds = secs % 60;
258
- const miliseconds = Math.floor((milisecs % 1000) / 10);
259
-
260
- return pad(minutes) + ':' + pad(seconds) + ':' + pad(miliseconds);
261
- };
262
-
263
- /**
264
- * Set listerner from native module for recorder.
265
- * @returns {callBack((e: RecordBackType): void)}
266
- */
267
-
268
- addRecordBackListener = (
269
- callback: (recordingMeta: RecordBackType) => void,
270
- ): void => {
271
- if (Platform.OS === 'android') {
272
- this._recorderSubscription = DeviceEventEmitter.addListener(
273
- 'rn-recordback',
274
- callback,
275
- );
276
- } else {
277
- const myModuleEvt = new NativeEventEmitter(RNAudioRecorderPlayer);
278
-
279
- this._recorderSubscription = myModuleEvt.addListener(
280
- 'rn-recordback',
281
- callback,
282
- );
283
- }
284
- };
285
-
286
- addListener(name: string, callback: (data: any) => void) {
287
- if (Platform.OS === 'android') {
288
- this._recorderSubscription = DeviceEventEmitter.addListener(
289
- name,
290
- callback,
291
- );
292
- } else {
293
- const myModuleEvt = new NativeEventEmitter(RNAudioRecorderPlayer);
294
-
295
- this._recorderSubscription = myModuleEvt.addListener(
296
- name,
297
- callback,
298
- );
299
- }
300
- }
301
- /**
302
- * Remove listener for recorder.
303
- * @returns {void}
304
- */
305
- removeRecordBackListener = (): void => {
306
- if (this._recorderSubscription) {
307
- this._recorderSubscription.remove();
308
- this._recorderSubscription = null;
309
- }
310
- };
311
-
312
- /**
313
- * Set listener from native module for player.
314
- * @returns {callBack((e: PlayBackType): void)}
315
- */
316
- addPlayBackListener = (
317
- callback: (playbackMeta: PlayBackType) => void,
318
- ): void => {
319
- this._playerCallback = callback;
320
- };
321
-
322
- /**
323
- * remove listener for player.
324
- * @returns {void}
325
- */
326
- removePlayBackListener = (): void => {
327
- this._playerCallback = null;
328
- };
329
-
330
- /**
331
- * start recording with param.
332
- * @param {string} uri audio uri.
333
- * @returns {Promise<string>}
334
- */
335
- startRecorder = async (
336
- uri?: string,
337
- audioSets?: AudioSet,
338
- meteringEnabled?: boolean,
339
- ): Promise<string> => {
340
- if (!this._isRecording) {
341
- this._isRecording = true;
342
-
343
- return RNAudioRecorderPlayer.startRecorder(
344
- uri ?? 'DEFAULT',
345
- audioSets,
346
- meteringEnabled ?? false,
347
- );
348
- }
349
-
350
- return 'Already recording';
351
- };
352
-
353
- /**
354
- * Pause recording.
355
- * @returns {Promise<string>}
356
- */
357
- pauseRecorder = async (): Promise<string> => {
358
- if (!this._hasPausedRecord) {
359
- this._hasPausedRecord = true;
360
-
361
- return RNAudioRecorderPlayer.pauseRecorder();
362
- }
363
-
364
- return 'Already paused recording.';
365
- };
366
-
367
- /**
368
- * Resume recording.
369
- * @returns {Promise<string>}
370
- */
371
- resumeRecorder = async (): Promise<string> => {
372
- if (this._hasPausedRecord) {
373
- this._hasPausedRecord = false;
374
-
375
- return RNAudioRecorderPlayer.resumeRecorder();
376
- }
377
-
378
- return 'Currently recording.';
379
- };
380
-
381
- /**
382
- * stop recording.
383
- * @returns {Promise<string>}
384
- */
385
- stopRecorder = async (): Promise<string> => {
386
- if (this._isRecording) {
387
- this._isRecording = false;
388
- this._hasPausedRecord = false;
389
-
390
- return RNAudioRecorderPlayer.stopRecorder();
391
- }
392
-
393
- return 'Already stopped';
394
- };
395
-
396
- /**
397
- * Resume playing.
398
- * @returns {Promise<string>}
399
- */
400
- resumePlayer = async (): Promise<string> => {
401
- if (!this._isPlaying) {
402
- return 'No audio playing';
403
- }
404
-
405
- if (this._hasPaused) {
406
- this._hasPaused = false;
407
-
408
- return RNAudioRecorderPlayer.resumePlayer();
409
- }
410
-
411
- return 'Already playing';
412
- };
413
-
414
- playerCallback = (event: PlayBackType): void => {
415
- if (this._playerCallback) {
416
- this._playerCallback(event);
417
- }
418
-
419
- if (event.isFinished) {
420
- this.stopPlayer();
421
- }
422
- };
423
-
424
- /**
425
- * Start playing with param.
426
- * @param {string} uri audio uri.
427
- * @param {Record<string, string>} httpHeaders Set of http headers.
428
- * @returns {Promise<string>}
429
- */
430
- startPlayer = async (
431
- uri?: string,
432
- httpHeaders?: Record<string, string>,
433
- ): Promise<string | undefined> => {
434
- if (!uri) {
435
- uri = 'DEFAULT';
436
- }
437
-
438
- if (!this._playerSubscription) {
439
- if (Platform.OS === 'android') {
440
- this._playerSubscription = DeviceEventEmitter.addListener(
441
- 'rn-playback',
442
- this.playerCallback,
443
- );
444
- } else {
445
- const myModuleEvt = new NativeEventEmitter(RNAudioRecorderPlayer);
446
-
447
- this._playerSubscription = myModuleEvt.addListener(
448
- 'rn-playback',
449
- this.playerCallback,
450
- );
451
- }
452
- }
453
-
454
- if (!this._isPlaying || this._hasPaused) {
455
- this._isPlaying = true;
456
- this._hasPaused = false;
457
-
458
- return RNAudioRecorderPlayer.startPlayer(uri, httpHeaders);
459
- }
460
- };
461
-
462
- /**
463
- * Stop playing.
464
- * @returns {Promise<string>}
465
- */
466
- stopPlayer = async (): Promise<string> => {
467
- if (this._isPlaying) {
468
- this._isPlaying = false;
469
- this._hasPaused = false;
470
-
471
- return RNAudioRecorderPlayer.stopPlayer();
472
- }
473
-
474
- return 'Already stopped playing';
475
- };
476
-
477
- /**
478
- * Pause playing.
479
- * @returns {Promise<string>}
480
- */
481
- pausePlayer = async (): Promise<string | undefined> => {
482
- if (!this._isPlaying) {
483
- return 'No audio playing';
484
- }
485
-
486
- if (!this._hasPaused) {
487
- this._hasPaused = true;
488
-
489
- return RNAudioRecorderPlayer.pausePlayer();
490
- }
491
- };
492
-
493
- /**
494
- * Seek to.
495
- * @param {number} time position seek to in millisecond.
496
- * @returns {Promise<string>}
497
- */
498
- seekToPlayer = async (time: number): Promise<string> => {
499
- return RNAudioRecorderPlayer.seekToPlayer(time);
500
- };
501
-
502
- /**
503
- * Set volume.
504
- * @param {number} setVolume set volume.
505
- * @returns {Promise<string>}
506
- */
507
- setVolume = async (volume: number): Promise<string> => {
508
- if (volume < 0 || volume > 1) {
509
- throw new Error('Value of volume should be between 0.0 to 1.0');
510
- }
511
-
512
- return RNAudioRecorderPlayer.setVolume(volume);
513
- };
514
-
515
- /**
516
- * Set subscription duration. Default is 0.5.
517
- * @param {number} sec subscription callback duration in seconds.
518
- * @returns {Promise<string>}
519
- */
520
- setSubscriptionDuration = async (sec: number): Promise<string> => {
521
- return RNAudioRecorderPlayer.setSubscriptionDuration(sec);
522
- };
523
- }
524
-
525
- export default AudioRecorderPlayer;
1
+ /*
2
+ * Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+
7
+ import type { EmitterSubscription } from 'react-native';
8
+ import {
9
+ DeviceEventEmitter,
10
+ NativeEventEmitter,
11
+ NativeModules,
12
+ Platform,
13
+ TurboModuleRegistry
14
+ } from 'react-native';
15
+
16
+
17
+ const RNAudioRecorderPlayer = TurboModuleRegistry ? require('./src/NativeAudioRecorderPlayerModule').default : NativeModules.RNAudioRecorderPlayer;
18
+
19
+ export enum AudioSourceHarmonyType {
20
+ DEFAULT = 0,
21
+ MIC,
22
+ VOICE_RECOGNITION,
23
+ VOICE_COMMUNICATION = 7,
24
+ VOICE_MESSAGE = 10,
25
+ CAMCORDER = 12,
26
+ }
27
+
28
+ export enum AudioFormatHarmonyType {
29
+ MPEG_4 = 'mp4',
30
+ MPEG_4A = 'm4a',
31
+ MP3 = 'mp3',
32
+ WAV = 'wav',
33
+ AAC = 'aac',
34
+ }
35
+
36
+ export enum AudioMimeHarmonyType {
37
+ AUDIO_AAC = 'audio/mp4a-latm', // 表示音频/mp4a-latm类型。
38
+
39
+ AUDIO_VORBIS = 'audio/vorbis', // 表示音频/vorbis类型。
40
+ AUDIO_FLAC = 'audio/flac', // 表示音频/flac类型。
41
+ AUDIO_MP3 = 'audio/mpeg', // 表示音频/mpeg类型。
42
+ AUDIO_G711MU = 'audio/g711mu', // 表示音频/G711-mulaw类型。
43
+ }
44
+
45
+ export enum AudioSourceAndroidType {
46
+ DEFAULT = 0,
47
+ MIC,
48
+ VOICE_UPLINK,
49
+ VOICE_DOWNLINK,
50
+ VOICE_CALL,
51
+ CAMCORDER,
52
+ VOICE_RECOGNITION,
53
+ VOICE_COMMUNICATION,
54
+ REMOTE_SUBMIX,
55
+ UNPROCESSED,
56
+ RADIO_TUNER = 1998,
57
+ HOTWORD,
58
+ }
59
+
60
+ export enum OutputFormatAndroidType {
61
+ DEFAULT = 0,
62
+ THREE_GPP,
63
+ MPEG_4,
64
+ AMR_NB,
65
+ AMR_WB,
66
+ AAC_ADIF,
67
+ AAC_ADTS,
68
+ OUTPUT_FORMAT_RTP_AVP,
69
+ MPEG_2_TS,
70
+ WEBM,
71
+ UNUSED,
72
+ OGG,
73
+ }
74
+
75
+ export enum AudioEncoderAndroidType {
76
+ DEFAULT = 0,
77
+ AMR_NB,
78
+ AMR_WB,
79
+ AAC,
80
+ HE_AAC,
81
+ AAC_ELD,
82
+ VORBIS,
83
+ OPUS,
84
+ }
85
+
86
+ export enum AVEncodingOption {
87
+ lpcm = 'lpcm',
88
+ ima4 = 'ima4',
89
+ aac = 'aac',
90
+ MAC3 = 'MAC3',
91
+ MAC6 = 'MAC6',
92
+ ulaw = 'ulaw',
93
+ alaw = 'alaw',
94
+ mp1 = 'mp1',
95
+ mp2 = 'mp2',
96
+ mp4 = 'mp4',
97
+ alac = 'alac',
98
+ amr = 'amr',
99
+ flac = 'flac',
100
+ opus = 'opus',
101
+ wav = 'wav',
102
+ }
103
+
104
+ type AVEncodingType =
105
+ | AVEncodingOption.lpcm
106
+ | AVEncodingOption.ima4
107
+ | AVEncodingOption.aac
108
+ | AVEncodingOption.MAC3
109
+ | AVEncodingOption.MAC6
110
+ | AVEncodingOption.ulaw
111
+ | AVEncodingOption.alaw
112
+ | AVEncodingOption.mp1
113
+ | AVEncodingOption.mp2
114
+ | AVEncodingOption.mp4
115
+ | AVEncodingOption.alac
116
+ | AVEncodingOption.amr
117
+ | AVEncodingOption.flac
118
+ | AVEncodingOption.opus
119
+ | AVEncodingOption.wav;
120
+
121
+ export enum AVModeIOSOption {
122
+ gamechat = 'gamechat',
123
+ measurement = 'measurement',
124
+ movieplayback = 'movieplayback',
125
+ spokenaudio = 'spokenaudio',
126
+ videochat = 'videochat',
127
+ videorecording = 'videorecording',
128
+ voicechat = 'voicechat',
129
+ voiceprompt = 'voiceprompt',
130
+ }
131
+
132
+ export type AVModeIOSType =
133
+ | AVModeIOSOption.gamechat
134
+ | AVModeIOSOption.measurement
135
+ | AVModeIOSOption.movieplayback
136
+ | AVModeIOSOption.spokenaudio
137
+ | AVModeIOSOption.videochat
138
+ | AVModeIOSOption.videorecording
139
+ | AVModeIOSOption.voicechat
140
+ | AVModeIOSOption.voiceprompt;
141
+
142
+ export enum AVEncoderAudioQualityIOSType {
143
+ min = 0,
144
+ low = 32,
145
+ medium = 64,
146
+ high = 96,
147
+ max = 127,
148
+ }
149
+
150
+ export enum AVLinearPCMBitDepthKeyIOSType {
151
+ 'bit8' = 8,
152
+ 'bit16' = 16,
153
+ 'bit24' = 24,
154
+ 'bit32' = 32,
155
+ }
156
+
157
+ export interface AudioSet {
158
+ AVSampleRateKeyIOS?: number;
159
+ AVFormatIDKeyIOS?: AVEncodingType;
160
+ AVModeIOS?: AVModeIOSType;
161
+ AVNumberOfChannelsKeyIOS?: number;
162
+ AVEncoderAudioQualityKeyIOS?: AVEncoderAudioQualityIOSType;
163
+ AudioSourceAndroid?: AudioSourceAndroidType;
164
+ AVLinearPCMBitDepthKeyIOS?: AVLinearPCMBitDepthKeyIOSType;
165
+ AVLinearPCMIsBigEndianKeyIOS?: boolean;
166
+ AVLinearPCMIsFloatKeyIOS?: boolean;
167
+ AVLinearPCMIsNonInterleavedIOS?: boolean;
168
+ AVEncoderBitRateKeyIOS?: number;
169
+ OutputFormatAndroid?: OutputFormatAndroidType;
170
+ AudioEncoderAndroid?: AudioEncoderAndroidType;
171
+ AudioEncodingBitRateAndroid?: number;
172
+ AudioSamplingRateAndroid?: number;
173
+ AudioChannelsAndroid?: number;
174
+ AudioSourceHarmony?: AudioSourceHarmonyType;
175
+ AudioMimeHarmony?: AudioMimeHarmonyType;
176
+ AudioFileFormatHarmony?: AudioFormatHarmonyType;
177
+ /**
178
+ * 音频编码比特率,选择音频录制时必填。
179
+
180
+ 支持范围:
181
+
182
+ - AAC编码格式支持比特率范围[32000 - 500000]。
183
+
184
+ - G711-mulaw编码格式支持比特率范围[64000 - 64000]。
185
+
186
+ - MP3编码格式支持范围[8000, 16000, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000]。
187
+
188
+ 当使用MP3编码格式时,采样率和比特率的映射关系:
189
+
190
+ - 采样率使用16K以下时,对应比特率范围为[8kbps - 64kbps]。
191
+
192
+ - 采样率使用16K~32K时对应的比特率范围为[8kbps - 160kbps]。
193
+
194
+ - 采样率使用32K以上时对应的比特率范围为[32kbps - 320kbps]。
195
+ */
196
+ AudioEncodingBitRateHarmony?: number;
197
+ /**
198
+ * 支持范围:
199
+
200
+ - AAC编码支持采样率范围[8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000]。
201
+
202
+ - G711-mulaw编码支持采样率范围[8000 - 8000]。
203
+
204
+ - MP3编码支持采样率范围[8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000]。
205
+ */
206
+ AudioSamplingRateHarmony?: number;
207
+ /**
208
+ * 音频采集声道数
209
+
210
+ - AAC编码格式支持范围[1 - 8]。
211
+
212
+ - G711-mulaw编码格式支持范围[1 - 1]。
213
+
214
+ - MP3编码格式支持范围[1 - 2]
215
+ */
216
+ AudioChannelsHarmony?: number;
217
+ }
218
+
219
+ const pad = (num: number): string => {
220
+ return ('0' + num).slice(-2);
221
+ };
222
+
223
+ export type RecordBackType = {
224
+ isRecording?: boolean;
225
+ currentPosition: number;
226
+ currentMetering?: number;
227
+ };
228
+
229
+ export type PlayBackType = {
230
+ isMuted?: boolean;
231
+ currentPosition: number;
232
+ duration: number;
233
+ isFinished: boolean;
234
+ };
235
+
236
+ class AudioRecorderPlayer {
237
+ private _isRecording: boolean;
238
+ private _isPlaying: boolean;
239
+ private _hasPaused: boolean;
240
+ private _hasPausedRecord: boolean;
241
+ private _recorderSubscription: null | EmitterSubscription;
242
+ private _playerSubscription: EmitterSubscription;
243
+ private _playerCallback: null | ((event: PlayBackType) => void);
244
+
245
+ mmss = (secs: number): string => {
246
+ let minutes = Math.floor(secs / 60);
247
+
248
+ secs = secs % 60;
249
+ minutes = minutes % 60;
250
+
251
+ return pad(minutes) + ':' + pad(secs);
252
+ };
253
+
254
+ mmssss = (milisecs: number): string => {
255
+ const secs = Math.floor(milisecs / 1000);
256
+ const minutes = Math.floor(secs / 60);
257
+ const seconds = secs % 60;
258
+ const miliseconds = Math.floor((milisecs % 1000) / 10);
259
+
260
+ return pad(minutes) + ':' + pad(seconds) + ':' + pad(miliseconds);
261
+ };
262
+
263
+ /**
264
+ * Set listerner from native module for recorder.
265
+ * @returns {callBack((e: RecordBackType): void)}
266
+ */
267
+
268
+ addRecordBackListener = (
269
+ callback: (recordingMeta: RecordBackType) => void,
270
+ ): void => {
271
+ if (Platform.OS === 'android') {
272
+ this._recorderSubscription = DeviceEventEmitter.addListener(
273
+ 'rn-recordback',
274
+ callback,
275
+ );
276
+ } else {
277
+ const myModuleEvt = new NativeEventEmitter(RNAudioRecorderPlayer);
278
+
279
+ this._recorderSubscription = myModuleEvt.addListener(
280
+ 'rn-recordback',
281
+ callback,
282
+ );
283
+ }
284
+ };
285
+
286
+ addListener(name: string, callback: (data: any) => void) {
287
+ if (Platform.OS === 'android') {
288
+ this._recorderSubscription = DeviceEventEmitter.addListener(
289
+ name,
290
+ callback,
291
+ );
292
+ } else {
293
+ const myModuleEvt = new NativeEventEmitter(RNAudioRecorderPlayer);
294
+
295
+ this._recorderSubscription = myModuleEvt.addListener(
296
+ name,
297
+ callback,
298
+ );
299
+ }
300
+ }
301
+ /**
302
+ * Remove listener for recorder.
303
+ * @returns {void}
304
+ */
305
+ removeRecordBackListener = (): void => {
306
+ if (this._recorderSubscription) {
307
+ this._recorderSubscription.remove();
308
+ this._recorderSubscription = null;
309
+ }
310
+ };
311
+
312
+ /**
313
+ * Set listener from native module for player.
314
+ * @returns {callBack((e: PlayBackType): void)}
315
+ */
316
+ addPlayBackListener = (
317
+ callback: (playbackMeta: PlayBackType) => void,
318
+ ): void => {
319
+ this._playerCallback = callback;
320
+ };
321
+
322
+ /**
323
+ * remove listener for player.
324
+ * @returns {void}
325
+ */
326
+ removePlayBackListener = (): void => {
327
+ this._playerCallback = null;
328
+ };
329
+
330
+ /**
331
+ * start recording with param.
332
+ * @param {string} uri audio uri.
333
+ * @returns {Promise<string>}
334
+ */
335
+ startRecorder = async (
336
+ uri?: string,
337
+ audioSets?: AudioSet,
338
+ meteringEnabled?: boolean,
339
+ ): Promise<string> => {
340
+ if (!this._isRecording) {
341
+ this._isRecording = true;
342
+
343
+ try {
344
+ return await RNAudioRecorderPlayer.startRecorder(
345
+ uri ?? 'DEFAULT',
346
+ audioSets,
347
+ meteringEnabled ?? false,
348
+ );
349
+ } catch (error: any) {
350
+ this._isRecording = false;
351
+ throw error;
352
+ }
353
+ }
354
+
355
+ return 'Already recording';
356
+ };
357
+
358
+ /**
359
+ * Pause recording.
360
+ * @returns {Promise<string>}
361
+ */
362
+ pauseRecorder = async (): Promise<string> => {
363
+ if (!this._hasPausedRecord) {
364
+ this._hasPausedRecord = true;
365
+
366
+ return RNAudioRecorderPlayer.pauseRecorder();
367
+ }
368
+
369
+ return 'Already paused recording.';
370
+ };
371
+
372
+ /**
373
+ * Resume recording.
374
+ * @returns {Promise<string>}
375
+ */
376
+ resumeRecorder = async (): Promise<string> => {
377
+ if (this._hasPausedRecord) {
378
+ this._hasPausedRecord = false;
379
+
380
+ return RNAudioRecorderPlayer.resumeRecorder();
381
+ }
382
+
383
+ return 'Currently recording.';
384
+ };
385
+
386
+ /**
387
+ * stop recording.
388
+ * @returns {Promise<string>}
389
+ */
390
+ stopRecorder = async (): Promise<string> => {
391
+ if (this._isRecording) {
392
+ this._isRecording = false;
393
+ this._hasPausedRecord = false;
394
+
395
+ return RNAudioRecorderPlayer.stopRecorder();
396
+ }
397
+
398
+ return 'Already stopped';
399
+ };
400
+
401
+ /**
402
+ * Resume playing.
403
+ * @returns {Promise<string>}
404
+ */
405
+ resumePlayer = async (): Promise<string> => {
406
+ if (!this._isPlaying) {
407
+ return 'No audio playing';
408
+ }
409
+
410
+ if (this._hasPaused) {
411
+ this._hasPaused = false;
412
+
413
+ return RNAudioRecorderPlayer.resumePlayer();
414
+ }
415
+
416
+ return 'Already playing';
417
+ };
418
+
419
+ playerCallback = (event: PlayBackType): void => {
420
+ if (this._playerCallback) {
421
+ this._playerCallback(event);
422
+ }
423
+
424
+ if (event.isFinished) {
425
+ this.stopPlayer();
426
+ }
427
+ };
428
+
429
+ /**
430
+ * Start playing with param.
431
+ * @param {string} uri audio uri.
432
+ * @param {Record<string, string>} httpHeaders Set of http headers.
433
+ * @returns {Promise<string>}
434
+ */
435
+ startPlayer = async (
436
+ uri?: string,
437
+ httpHeaders?: Record<string, string>,
438
+ ): Promise<string | undefined> => {
439
+ if (!uri) {
440
+ uri = 'DEFAULT';
441
+ }
442
+
443
+ if (!this._playerSubscription) {
444
+ if (Platform.OS === 'android') {
445
+ this._playerSubscription = DeviceEventEmitter.addListener(
446
+ 'rn-playback',
447
+ this.playerCallback,
448
+ );
449
+ } else {
450
+ const myModuleEvt = new NativeEventEmitter(RNAudioRecorderPlayer);
451
+
452
+ this._playerSubscription = myModuleEvt.addListener(
453
+ 'rn-playback',
454
+ this.playerCallback,
455
+ );
456
+ }
457
+ }
458
+
459
+ if (!this._isPlaying || this._hasPaused) {
460
+ this._isPlaying = true;
461
+ this._hasPaused = false;
462
+
463
+ return RNAudioRecorderPlayer.startPlayer(uri, httpHeaders);
464
+ }
465
+ };
466
+
467
+ /**
468
+ * Stop playing.
469
+ * @returns {Promise<string>}
470
+ */
471
+ stopPlayer = async (): Promise<string> => {
472
+ if (this._isPlaying) {
473
+ this._isPlaying = false;
474
+ this._hasPaused = false;
475
+
476
+ return RNAudioRecorderPlayer.stopPlayer();
477
+ }
478
+
479
+ return 'Already stopped playing';
480
+ };
481
+
482
+ /**
483
+ * Pause playing.
484
+ * @returns {Promise<string>}
485
+ */
486
+ pausePlayer = async (): Promise<string | undefined> => {
487
+ if (!this._isPlaying) {
488
+ return 'No audio playing';
489
+ }
490
+
491
+ if (!this._hasPaused) {
492
+ this._hasPaused = true;
493
+
494
+ return RNAudioRecorderPlayer.pausePlayer();
495
+ }
496
+ };
497
+
498
+ /**
499
+ * Seek to.
500
+ * @param {number} time position seek to in millisecond.
501
+ * @returns {Promise<string>}
502
+ */
503
+ seekToPlayer = async (time: number): Promise<string> => {
504
+ return RNAudioRecorderPlayer.seekToPlayer(time);
505
+ };
506
+
507
+ /**
508
+ * Set volume.
509
+ * @param {number} setVolume set volume.
510
+ * @returns {Promise<string>}
511
+ */
512
+ setVolume = async (volume: number): Promise<string> => {
513
+ if (volume < 0 || volume > 1) {
514
+ throw new Error('Value of volume should be between 0.0 to 1.0');
515
+ }
516
+
517
+ return RNAudioRecorderPlayer.setVolume(volume);
518
+ };
519
+
520
+ /**
521
+ * Set subscription duration. Default is 0.5.
522
+ * @param {number} sec subscription callback duration in seconds.
523
+ * @returns {Promise<string>}
524
+ */
525
+ setSubscriptionDuration = async (sec: number): Promise<string> => {
526
+ return RNAudioRecorderPlayer.setSubscriptionDuration(sec);
527
+ };
528
+ }
529
+
530
+ export default AudioRecorderPlayer;