@react-native-ohos/react-native-audio-recorder-player 3.7.0 → 3.7.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.
Files changed (33) hide show
  1. package/LICENSE +20 -20
  2. package/README.OpenSource +10 -10
  3. package/README.md +822 -14
  4. package/harmony/audio_recorder_player/BuildProfile.ets +16 -16
  5. package/harmony/audio_recorder_player/LICENSE +21 -21
  6. package/harmony/audio_recorder_player/build-profile.json5 +28 -28
  7. package/harmony/audio_recorder_player/index.ets +27 -25
  8. package/harmony/audio_recorder_player/oh-package-lock.json5 +18 -0
  9. package/harmony/audio_recorder_player/oh-package.json5 +20 -20
  10. package/harmony/audio_recorder_player/src/main/cpp/CMakeLists.txt +7 -7
  11. package/harmony/audio_recorder_player/src/main/cpp/RNAudioRecorderPlayerPackage.h +48 -48
  12. package/harmony/audio_recorder_player/src/main/cpp/RNAudioRecorderPlayerTurboModule.cpp +90 -90
  13. package/harmony/audio_recorder_player/src/main/cpp/RNAudioRecorderPlayerTurboModule.h +35 -35
  14. package/harmony/audio_recorder_player/src/main/ets/{RNAudioRecorderPlayerPackage.ts → RNAudioRecorderPlayerPackage.ets} +48 -47
  15. package/harmony/audio_recorder_player/src/main/ets/RNAudioRecorderPlayerTurboModule.ts +324 -323
  16. package/harmony/audio_recorder_player/src/main/ets/audioPlayer/AudioPlayer.ts +464 -464
  17. package/harmony/audio_recorder_player/src/main/ets/audioPlayer/types.ts +45 -45
  18. package/harmony/audio_recorder_player/src/main/ets/{index.d.ts → index.ts} +209 -209
  19. package/harmony/audio_recorder_player/src/main/ets/recorder/Recorder.ts +299 -299
  20. package/harmony/audio_recorder_player/src/main/ets/recorder/types.ts +61 -61
  21. package/harmony/audio_recorder_player/src/main/ets/utils/DateTimeUtils.ts +88 -88
  22. package/harmony/audio_recorder_player/src/main/ets/utils/Logger.ts +45 -45
  23. package/harmony/audio_recorder_player/src/main/ets/utils/SaveAudioAsset.ts +105 -105
  24. package/harmony/audio_recorder_player/src/main/module.json5 +9 -9
  25. package/harmony/audio_recorder_player/src/main/resources/base/element/string.json +8 -8
  26. package/harmony/audio_recorder_player/src/main/resources/base/media/ic_public_error.svg +9 -9
  27. package/harmony/audio_recorder_player/src/main/resources/en_US/element/string.json +8 -8
  28. package/harmony/audio_recorder_player/src/main/resources/zh_CN/element/string.json +8 -8
  29. package/harmony/audio_recorder_player/{ts.ts → ts.ets} +26 -26
  30. package/harmony/audio_recorder_player.har +0 -0
  31. package/index.ts +539 -538
  32. package/package.json +71 -59
  33. package/src/NativeAudioRecorderPlayerModule.ts +42 -42
@@ -1,323 +1,324 @@
1
- /*
2
- * MIT License
3
- *
4
- * Copyright (C) 2023 Huawei Device Co., Ltd.
5
- *
6
- * Permission is hereby granted, free of charge, to any person obtaining a copy
7
- * of this software and associated documentation files (the "Software"), to deal
8
- * in the Software without restriction, including without limitation the rights
9
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- * copies of the Software, and to permit persons to whom the Software is
11
- * furnished to do so, subject to the following conditions:
12
- *
13
- * The above copyright notice and this permission notice shall be included in all
14
- * copies or substantial portions of the Software.
15
- *
16
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
- * SOFTWARE.
23
- */
24
- import { TurboModule, TurboModuleContext, } from '@rnoh/react-native-openharmony/ts';
25
- import { media } from '@kit.MediaKit';
26
- import { AudioSet } from '.';
27
- import Recorder from './recorder/Recorder';
28
- import { AVRecorderState, defaultBitrate, defaultChannels, defaultSampleRate, RecordBackName, } from './recorder/types';
29
- import AudioPlayer from './audioPlayer/AudioPlayer';
30
- import { AVPlayerState, EventTarget, playBackName, processValue, } from './audioPlayer/types';
31
- import deviceinfo from '@ohos.deviceInfo';
32
- import Logger from './utils/Logger'
33
-
34
- export class RNAudioRecorderPlayerTurboModule extends TurboModule {
35
- public ctx: TurboModuleContext;
36
- private AVPlayerInstance: AudioPlayer;
37
- private AVRecorderInstance: Recorder;
38
- private subscriptionDuration: number = 500;
39
- private RecordBackListenerTimer;
40
- private PlayBackListenerTimer;
41
- private meteringEnabled: boolean = false;
42
-
43
- /**
44
- * Set listerner from native module for recorder.
45
- * @returns {callBack((e: RecordBackType): void)}
46
- */
47
- constructor(ctx: TurboModuleContext) {
48
- super(ctx);
49
- this.AVRecorderInstance = new Recorder(ctx.uiAbilityContext);
50
- this.AVPlayerInstance = new AudioPlayer(ctx.uiAbilityContext);
51
- this.ctx = ctx;
52
- }
53
-
54
- addListener(eventName: string): void {
55
- if (eventName === RecordBackName && !this.isRecording()) {
56
- this.addRecordBackListener();
57
- }
58
- if (eventName === playBackName) {
59
- this.addPlayBackListener();
60
- }
61
- }
62
-
63
- removeListeners(count: number): void {
64
- }
65
-
66
- /**
67
- * Remove listener for recorder.
68
- * @returns {void}
69
- */
70
- public addRecordBackListener(): void {
71
- if (this.RecordBackListenerTimer) {
72
- this.removeRecordBackListener();
73
- }
74
- this.AVRecorderInstance.onEvent(() => {
75
- this.RecordBackListenerTimer = setInterval(async () => {
76
- if (this.meteringEnabled) {
77
- let maxAmplitude: number = 0;
78
- let mediaRecorder: Recorder = this.AVRecorderInstance;
79
- if (mediaRecorder != null) {
80
- maxAmplitude = await mediaRecorder.getMaxAmplitude();
81
- }
82
- let dB: number = -160.0;
83
- let maxAudioSize: number = 32767.0;
84
- if (maxAmplitude > 0) {
85
- dB = 20 * Math.log10(maxAmplitude / maxAudioSize);
86
- dB = Math.floor(dB);
87
- }
88
- this.ctx.rnInstance.emitDeviceEvent(RecordBackName, {
89
- isRecording: this.AVRecorderInstance.isRecording,
90
- currentPosition: this.AVRecorderInstance.milliseconds,
91
- currentMetering: dB,
92
- });
93
- } else {
94
- this.ctx.rnInstance.emitDeviceEvent(RecordBackName, {
95
- isRecording: this.AVRecorderInstance.isRecording,
96
- currentPosition: this.AVRecorderInstance.milliseconds,
97
- });
98
- }
99
- }, this.subscriptionDuration);
100
- });
101
- }
102
-
103
- /**
104
- * Set listener from native module for player.
105
- * @returns {callBack((e: PlayBackType): void)}
106
- */
107
- public removeRecordBackListener(): void {
108
- clearInterval(this.RecordBackListenerTimer);
109
- this.RecordBackListenerTimer = null;
110
- }
111
-
112
- /**
113
- * remove listener for player.
114
- * @returns {void}
115
- */
116
- public addPlayBackListener(): void {
117
- if (this.PlayBackListenerTimer) {
118
- this.removePlayBackListener();
119
- }
120
-
121
- this.AVPlayerInstance.onEvent(EventTarget.timeUpdate, () => {
122
- this.PlayBackListenerTimer = setInterval(() => {
123
- this.ctx.rnInstance.emitDeviceEvent(playBackName, {
124
- isMuted: this.AVPlayerInstance.isMuted,
125
- isFinished: this.AVPlayerInstance.isFinished,
126
- currentPosition: this.AVPlayerInstance.currentPosition,
127
- duration: this.AVPlayerInstance.durationTime,
128
- });
129
- if (this.AVPlayerInstance.isFinished) {
130
- this.stopPlayer();
131
- }
132
- }, this.subscriptionDuration);
133
- });
134
- }
135
-
136
- public removePlayBackListener(): void {
137
- clearInterval(this.PlayBackListenerTimer);
138
- this.PlayBackListenerTimer = null;
139
- }
140
-
141
- /**
142
- * start recording with param.
143
- * @param {string} uri audio uri.
144
- * @returns {Promise<string>}
145
- */
146
- public async startRecorder(
147
- uri?: string,
148
- audioSets?: AudioSet,
149
- meteringEnabled?: boolean,
150
- ): Promise<string> {
151
- if (this.isRecording()) {
152
- return Promise.reject(processValue.play);
153
- }
154
- this.meteringEnabled = meteringEnabled === true ? true : false;
155
- const profile: media.AVRecorderProfile = this.getAudioSets(audioSets);
156
- this.AVRecorderInstance.setAVProfile(profile, audioSets?.AudioSourceHarmony as number as media.AudioSourceType);
157
- return this.AVRecorderInstance.startRecordingProcess(uri);
158
- }
159
-
160
- getAudioSets(audioSets?: AudioSet): media.AVRecorderProfile {
161
- let audioCodec: media.CodecMimeType = media.CodecMimeType.AUDIO_AAC;
162
- if (audioSets?.AudioMimeHarmony) {
163
- audioCodec = audioSets.AudioMimeHarmony as string as media.CodecMimeType;
164
- }
165
- const avProfile: media.AVRecorderProfile = {
166
- audioBitrate:
167
- audioSets?.AudioSamplingRateHarmony ||
168
- defaultBitrate, // 音频比特率
169
- audioChannels:
170
- audioSets?.AudioChannelsHarmony ||
171
- defaultChannels, // 音频声道数
172
- audioCodec: audioCodec, // 音频编码格式,当前只支持aac、vorbis、flac, 12开始支持AUDIO_MP3
173
- audioSampleRate:
174
- audioSets?.AudioSamplingRateHarmony ||
175
- defaultSampleRate, // 音频采样率
176
- fileFormat: (audioSets?.AudioFileFormatHarmony as string as media.ContainerFormatType) ||
177
- media.ContainerFormatType.CFT_MPEG_4A, // 封装格式,当前只支持m4a, 12开始支持CFT_MP3
178
- };
179
- return avProfile;
180
- }
181
-
182
- /**
183
- * Pause recording.
184
- * @returns {Promise<string>}
185
- */
186
- public async pauseRecorder(): Promise<string> {
187
- if (this.AVRecorderInstance.recorderState === AVRecorderState.started) {
188
- this.removeRecordBackListener();
189
- return this.AVRecorderInstance.pauseRecordingProcess();
190
- }
191
- return Promise.reject(processValue.stateError);
192
- }
193
-
194
- /**
195
- * Resume recording.
196
- * @returns {Promise<string>}
197
- */
198
- public async resumeRecorder(): Promise<string> {
199
- if (this.AVRecorderInstance.recorderState === AVRecorderState.paused) {
200
- this.addRecordBackListener();
201
- return this.AVRecorderInstance.resumeRecordingProcess();
202
- }
203
- return Promise.reject(processValue.stateError);
204
- }
205
-
206
- /**
207
- * stop recording.
208
- * @returns {Promise<string>}
209
- */
210
- public async stopRecorder(): Promise<string> {
211
- if (
212
- [AVRecorderState.started, AVRecorderState.paused].includes(
213
- this.AVRecorderInstance.recorderState,
214
- )
215
- ) {
216
- // 仅在started或者paused状态下调用stop为合理状态切换
217
- this.removeRecordBackListener();
218
- return this.AVRecorderInstance.stopRecordingProcess();
219
- }
220
- return Promise.reject(processValue.stateError);
221
- }
222
-
223
- public async startPlayer(uri?: string, httpHeaders?: Record<string, string>): Promise<string> {
224
- if (this.AVPlayerInstance.PlayerState === AVPlayerState.paused) {
225
- return this.resumePlayer();
226
- }
227
- const url = await this.AVPlayerInstance.startPlayeringProcess(uri, httpHeaders);
228
- !!url && this.addPlayBackListener();
229
- return url;
230
- }
231
-
232
- /**
233
- * Start playing with param.
234
- * @param {string} uri audio uri.
235
- * @returns {Promise<string>}
236
- */
237
- public async resumePlayer(): Promise<string> {
238
- if (this.AVPlayerInstance.PlayerState === AVPlayerState.paused) {
239
- this.addPlayBackListener();
240
- return this.AVPlayerInstance.resumePlayeringProcess();
241
- }
242
- return Promise.reject(processValue.stateError);
243
- }
244
-
245
- /**
246
- * Resume playing.
247
- * @returns {Promise<string>}
248
- */
249
- public async stopPlayer(): Promise<string> {
250
- this.removePlayBackListener();
251
- return this.AVPlayerInstance.stopPlayeringProcess();
252
- }
253
-
254
- /**
255
- * Stop playing.
256
- * @returns {Promise<string>}
257
- */
258
- public async pausePlayer(): Promise<string> {
259
- if (this.AVPlayerInstance.PlayerState === AVPlayerState.playing) {
260
- this.removePlayBackListener();
261
- return this.AVPlayerInstance.pausePlayeringProcess();
262
- }
263
- return Promise.reject(processValue.stateError);
264
- }
265
-
266
- /**
267
- * Pause playing.
268
- * @returns {Promise<string>}
269
- */
270
- public async seekToPlayer(time: number): Promise<string> {
271
- if (!this.AVPlayerInstance.seekToPlayer) {
272
- return Promise.reject(processValue.stateError);
273
- }
274
- return this.AVPlayerInstance.seekToPlayer(time);
275
- }
276
-
277
- /**
278
- * Seek to.
279
- * @param {number} time position seek to in millisecond.
280
- * @returns {Promise<string>}
281
- */
282
- async setVolume(volume: number): Promise<string> {
283
- if (!this.AVPlayerInstance.setVolume) {
284
- return Promise.reject(processValue.stateError);
285
- }
286
- return this.AVPlayerInstance.setVolume(volume);
287
- }
288
-
289
- /**
290
- * Set playback speed.
291
- * @param {number} playbackSpeed set playback speed.
292
- * @returns {Promise<string>}
293
- */
294
- async setPlaybackSpeed(playbackSpeed: number): Promise<string> {
295
- if (!this.AVPlayerInstance.setPlaybackSpeed) {
296
- return Promise.reject(processValue.stateError);
297
- }
298
- let sdkApiVersionInfo: number = deviceinfo.sdkApiVersion;
299
- Logger.debug("RNAudioRecorderPlayerTurboModule", `playbackSpeed:${playbackSpeed}---apiversion:${sdkApiVersionInfo}`);
300
- if (playbackSpeed == 3 && sdkApiVersionInfo < 13) {
301
- Logger.debug("RNAudioRecorderPlayerTurboModule", `apiversion < 13 not supported speed 3`);
302
- return Promise.reject(processValue.stateError);
303
- }
304
- return this.AVPlayerInstance.setPlaybackSpeed(playbackSpeed);
305
- }
306
-
307
- /**
308
- * Set volume.
309
- * @param {number} setVolume set volume.
310
- * @returns {Promise<string>}
311
- */
312
- async setSubscriptionDuration(sec: number): Promise<string> {
313
- const milliseconds = sec * 1000;
314
- this.subscriptionDuration = milliseconds;
315
- return sec.toString();
316
- }
317
-
318
- private isRecording(): boolean {
319
- return [AVRecorderState.started, AVRecorderState.paused].includes(
320
- this.AVRecorderInstance.recorderState,
321
- );
322
- }
323
- }
1
+ /*
2
+ * MIT License
3
+ *
4
+ * Copyright (C) 2023 Huawei Device Co., Ltd.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import { AnyThreadTurboModule, AnyThreadTurboModuleContext } from '@rnoh/react-native-openharmony/ts';
26
+ import { media } from '@kit.MediaKit';
27
+ import type { AudioSet } from '.';
28
+ import Recorder from './recorder/Recorder';
29
+ import { AVRecorderState, defaultBitrate, defaultChannels, defaultSampleRate, RecordBackName, } from './recorder/types';
30
+ import AudioPlayer from './audioPlayer/AudioPlayer';
31
+ import { AVPlayerState, EventTarget, playBackName, processValue, } from './audioPlayer/types';
32
+ import deviceinfo from '@ohos.deviceInfo';
33
+ import Logger from './utils/Logger'
34
+
35
+ export class RNAudioRecorderPlayerTurboModule extends AnyThreadTurboModule {
36
+ public ctx: AnyThreadTurboModuleContext;
37
+ private AVPlayerInstance: AudioPlayer;
38
+ private AVRecorderInstance: Recorder;
39
+ private subscriptionDuration: number = 500;
40
+ private RecordBackListenerTimer;
41
+ private PlayBackListenerTimer;
42
+ private meteringEnabled: boolean = false;
43
+
44
+ /**
45
+ * Set listerner from native module for recorder.
46
+ * @returns {callBack((e: RecordBackType): void)}
47
+ */
48
+ constructor(ctx: AnyThreadTurboModuleContext) {
49
+ super(ctx);
50
+ this.AVRecorderInstance = new Recorder(ctx.uiAbilityContext);
51
+ this.AVPlayerInstance = new AudioPlayer(ctx.uiAbilityContext);
52
+ this.ctx = ctx;
53
+ }
54
+
55
+ addListener(eventName: string): void {
56
+ if (eventName === RecordBackName && !this.isRecording()) {
57
+ this.addRecordBackListener();
58
+ }
59
+ if (eventName === playBackName) {
60
+ this.addPlayBackListener();
61
+ }
62
+ }
63
+
64
+ removeListeners(count: number): void {
65
+ }
66
+
67
+ /**
68
+ * Remove listener for recorder.
69
+ * @returns {void}
70
+ */
71
+ public addRecordBackListener(): void {
72
+ if (this.RecordBackListenerTimer) {
73
+ this.removeRecordBackListener();
74
+ }
75
+ this.AVRecorderInstance.onEvent(() => {
76
+ this.RecordBackListenerTimer = setInterval(async () => {
77
+ if (this.meteringEnabled) {
78
+ let maxAmplitude: number = 0;
79
+ let mediaRecorder: Recorder = this.AVRecorderInstance;
80
+ if (mediaRecorder != null) {
81
+ maxAmplitude = await mediaRecorder.getMaxAmplitude();
82
+ }
83
+ let dB: number = -160.0;
84
+ let maxAudioSize: number = 32767.0;
85
+ if (maxAmplitude > 0) {
86
+ dB = 20 * Math.log10(maxAmplitude / maxAudioSize);
87
+ dB = Math.floor(dB);
88
+ }
89
+ this.ctx.rnInstance.emitDeviceEvent(RecordBackName, {
90
+ isRecording: this.AVRecorderInstance.isRecording,
91
+ currentPosition: this.AVRecorderInstance.milliseconds,
92
+ currentMetering: dB,
93
+ });
94
+ } else {
95
+ this.ctx.rnInstance.emitDeviceEvent(RecordBackName, {
96
+ isRecording: this.AVRecorderInstance.isRecording,
97
+ currentPosition: this.AVRecorderInstance.milliseconds,
98
+ });
99
+ }
100
+ }, this.subscriptionDuration);
101
+ });
102
+ }
103
+
104
+ /**
105
+ * Set listener from native module for player.
106
+ * @returns {callBack((e: PlayBackType): void)}
107
+ */
108
+ public removeRecordBackListener(): void {
109
+ clearInterval(this.RecordBackListenerTimer);
110
+ this.RecordBackListenerTimer = null;
111
+ }
112
+
113
+ /**
114
+ * remove listener for player.
115
+ * @returns {void}
116
+ */
117
+ public addPlayBackListener(): void {
118
+ if (this.PlayBackListenerTimer) {
119
+ this.removePlayBackListener();
120
+ }
121
+
122
+ this.AVPlayerInstance.onEvent(EventTarget.timeUpdate, () => {
123
+ this.PlayBackListenerTimer = setInterval(() => {
124
+ this.ctx.rnInstance.emitDeviceEvent(playBackName, {
125
+ isMuted: this.AVPlayerInstance.isMuted,
126
+ isFinished: this.AVPlayerInstance.isFinished,
127
+ currentPosition: this.AVPlayerInstance.currentPosition,
128
+ duration: this.AVPlayerInstance.durationTime,
129
+ });
130
+ if (this.AVPlayerInstance.isFinished) {
131
+ this.stopPlayer();
132
+ }
133
+ }, this.subscriptionDuration);
134
+ });
135
+ }
136
+
137
+ public removePlayBackListener(): void {
138
+ clearInterval(this.PlayBackListenerTimer);
139
+ this.PlayBackListenerTimer = null;
140
+ }
141
+
142
+ /**
143
+ * start recording with param.
144
+ * @param {string} uri audio uri.
145
+ * @returns {Promise<string>}
146
+ */
147
+ public async startRecorder(
148
+ uri?: string,
149
+ audioSets?: AudioSet,
150
+ meteringEnabled?: boolean,
151
+ ): Promise<string> {
152
+ if (this.isRecording()) {
153
+ return Promise.reject(processValue.play);
154
+ }
155
+ this.meteringEnabled = meteringEnabled === true ? true : false;
156
+ const profile: media.AVRecorderProfile = this.getAudioSets(audioSets);
157
+ this.AVRecorderInstance.setAVProfile(profile, audioSets?.AudioSourceHarmony as number as media.AudioSourceType);
158
+ return this.AVRecorderInstance.startRecordingProcess(uri);
159
+ }
160
+
161
+ getAudioSets(audioSets?: AudioSet): media.AVRecorderProfile {
162
+ let audioCodec: media.CodecMimeType = media.CodecMimeType.AUDIO_AAC;
163
+ if (audioSets?.AudioMimeHarmony) {
164
+ audioCodec = audioSets.AudioMimeHarmony as string as media.CodecMimeType;
165
+ }
166
+ const avProfile: media.AVRecorderProfile = {
167
+ audioBitrate:
168
+ audioSets?.AudioSamplingRateHarmony ||
169
+ defaultBitrate, // 音频比特率
170
+ audioChannels:
171
+ audioSets?.AudioChannelsHarmony ||
172
+ defaultChannels, // 音频声道数
173
+ audioCodec: audioCodec, // 音频编码格式,当前只支持aac、vorbis、flac, 12开始支持AUDIO_MP3
174
+ audioSampleRate:
175
+ audioSets?.AudioSamplingRateHarmony ||
176
+ defaultSampleRate, // 音频采样率
177
+ fileFormat: (audioSets?.AudioFileFormatHarmony as string as media.ContainerFormatType) ||
178
+ media.ContainerFormatType.CFT_MPEG_4A, // 封装格式,当前只支持m4a, 12开始支持CFT_MP3
179
+ };
180
+ return avProfile;
181
+ }
182
+
183
+ /**
184
+ * Pause recording.
185
+ * @returns {Promise<string>}
186
+ */
187
+ public async pauseRecorder(): Promise<string> {
188
+ if (this.AVRecorderInstance.recorderState === AVRecorderState.started) {
189
+ this.removeRecordBackListener();
190
+ return this.AVRecorderInstance.pauseRecordingProcess();
191
+ }
192
+ return Promise.reject(processValue.stateError);
193
+ }
194
+
195
+ /**
196
+ * Resume recording.
197
+ * @returns {Promise<string>}
198
+ */
199
+ public async resumeRecorder(): Promise<string> {
200
+ if (this.AVRecorderInstance.recorderState === AVRecorderState.paused) {
201
+ this.addRecordBackListener();
202
+ return this.AVRecorderInstance.resumeRecordingProcess();
203
+ }
204
+ return Promise.reject(processValue.stateError);
205
+ }
206
+
207
+ /**
208
+ * stop recording.
209
+ * @returns {Promise<string>}
210
+ */
211
+ public async stopRecorder(): Promise<string> {
212
+ if (
213
+ [AVRecorderState.started, AVRecorderState.paused].includes(
214
+ this.AVRecorderInstance.recorderState,
215
+ )
216
+ ) {
217
+ // 仅在started或者paused状态下调用stop为合理状态切换
218
+ this.removeRecordBackListener();
219
+ return this.AVRecorderInstance.stopRecordingProcess();
220
+ }
221
+ return Promise.reject(processValue.stateError);
222
+ }
223
+
224
+ public async startPlayer(uri?: string, httpHeaders?: Record<string, string>): Promise<string> {
225
+ if (this.AVPlayerInstance.PlayerState === AVPlayerState.paused) {
226
+ return this.resumePlayer();
227
+ }
228
+ const url = await this.AVPlayerInstance.startPlayeringProcess(uri, httpHeaders);
229
+ !!url && this.addPlayBackListener();
230
+ return url;
231
+ }
232
+
233
+ /**
234
+ * Start playing with param.
235
+ * @param {string} uri audio uri.
236
+ * @returns {Promise<string>}
237
+ */
238
+ public async resumePlayer(): Promise<string> {
239
+ if (this.AVPlayerInstance.PlayerState === AVPlayerState.paused) {
240
+ this.addPlayBackListener();
241
+ return this.AVPlayerInstance.resumePlayeringProcess();
242
+ }
243
+ return Promise.reject(processValue.stateError);
244
+ }
245
+
246
+ /**
247
+ * Resume playing.
248
+ * @returns {Promise<string>}
249
+ */
250
+ public async stopPlayer(): Promise<string> {
251
+ this.removePlayBackListener();
252
+ return this.AVPlayerInstance.stopPlayeringProcess();
253
+ }
254
+
255
+ /**
256
+ * Stop playing.
257
+ * @returns {Promise<string>}
258
+ */
259
+ public async pausePlayer(): Promise<string> {
260
+ if (this.AVPlayerInstance.PlayerState === AVPlayerState.playing) {
261
+ this.removePlayBackListener();
262
+ return this.AVPlayerInstance.pausePlayeringProcess();
263
+ }
264
+ return Promise.reject(processValue.stateError);
265
+ }
266
+
267
+ /**
268
+ * Pause playing.
269
+ * @returns {Promise<string>}
270
+ */
271
+ public async seekToPlayer(time: number): Promise<string> {
272
+ if (!this.AVPlayerInstance.seekToPlayer) {
273
+ return Promise.reject(processValue.stateError);
274
+ }
275
+ return this.AVPlayerInstance.seekToPlayer(time);
276
+ }
277
+
278
+ /**
279
+ * Seek to.
280
+ * @param {number} time position seek to in millisecond.
281
+ * @returns {Promise<string>}
282
+ */
283
+ async setVolume(volume: number): Promise<string> {
284
+ if (!this.AVPlayerInstance.setVolume) {
285
+ return Promise.reject(processValue.stateError);
286
+ }
287
+ return this.AVPlayerInstance.setVolume(volume);
288
+ }
289
+
290
+ /**
291
+ * Set playback speed.
292
+ * @param {number} playbackSpeed set playback speed.
293
+ * @returns {Promise<string>}
294
+ */
295
+ async setPlaybackSpeed(playbackSpeed: number): Promise<string> {
296
+ if (!this.AVPlayerInstance.setPlaybackSpeed) {
297
+ return Promise.reject(processValue.stateError);
298
+ }
299
+ let sdkApiVersionInfo: number = deviceinfo.sdkApiVersion;
300
+ Logger.debug("RNAudioRecorderPlayerTurboModule", `playbackSpeed:${playbackSpeed}---apiversion:${sdkApiVersionInfo}`);
301
+ if (playbackSpeed == 3 && sdkApiVersionInfo < 13) {
302
+ Logger.debug("RNAudioRecorderPlayerTurboModule", `apiversion < 13 not supported speed 3`);
303
+ return Promise.reject(processValue.stateError);
304
+ }
305
+ return this.AVPlayerInstance.setPlaybackSpeed(playbackSpeed);
306
+ }
307
+
308
+ /**
309
+ * Set volume.
310
+ * @param {number} setVolume set volume.
311
+ * @returns {Promise<string>}
312
+ */
313
+ async setSubscriptionDuration(sec: number): Promise<string> {
314
+ const milliseconds = sec * 1000;
315
+ this.subscriptionDuration = milliseconds;
316
+ return sec.toString();
317
+ }
318
+
319
+ private isRecording(): boolean {
320
+ return [AVRecorderState.started, AVRecorderState.paused].includes(
321
+ this.AVRecorderInstance.recorderState,
322
+ );
323
+ }
324
+ }