@react-native-ohos/react-native-audio-recorder-player 3.6.11 → 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 (36) 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 -68
  36. package/src/NativeAudioRecorderPlayerModule.ts +41 -41
@@ -1,310 +1,316 @@
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 '@kit.BasicServicesKit';
32
-
33
- export class RNAudioRecorderPlayerTurboModule extends TurboModule {
34
- public ctx: TurboModuleContext;
35
- private AVPlayerInstance: AudioPlayer;
36
- private AVRecorderInstance: Recorder;
37
- private subscriptionDuration: number = 500;
38
- private RecordBackListenerTimer;
39
- private PlayBackListenerTimer;
40
- private meteringEnabled: boolean = false;
41
-
42
- /**
43
- * Set listerner from native module for recorder.
44
- * @returns {callBack((e: RecordBackType): void)}
45
- */
46
- constructor(ctx: TurboModuleContext) {
47
- super(ctx);
48
- this.AVRecorderInstance = new Recorder(ctx.uiAbilityContext);
49
- this.AVPlayerInstance = new AudioPlayer(ctx.uiAbilityContext);
50
- this.ctx = ctx;
51
- }
52
-
53
- addListener(eventName: string): void {
54
- if (eventName === RecordBackName && !this.isRecording()) {
55
- this.addRecordBackListener();
56
- }
57
- if (eventName === playBackName) {
58
- this.addPlayBackListener();
59
- }
60
- }
61
-
62
- removeListeners(count: number): void {
63
- }
64
-
65
- /**
66
- * Remove listener for recorder.
67
- * @returns {void}
68
- */
69
- public addRecordBackListener(): void {
70
- if (this.RecordBackListenerTimer) {
71
- this.removeRecordBackListener();
72
- }
73
- this.AVRecorderInstance.onEvent(() => {
74
- this.RecordBackListenerTimer = setInterval(async () => {
75
- if (this.meteringEnabled) {
76
- let maxAmplitude: number = 0;
77
- let mediaRecorder: Recorder = this.AVRecorderInstance;
78
- if (mediaRecorder != null) {
79
- maxAmplitude = await mediaRecorder.getMaxAmplitude();
80
- }
81
- let dB: number = -160.0;
82
- let maxAudioSize: number = 32767.0;
83
- if (maxAmplitude > 0) {
84
- dB = 20 * Math.log10(maxAmplitude / maxAudioSize);
85
- dB = Math.floor(dB);
86
- }
87
- this.ctx.rnInstance.emitDeviceEvent(RecordBackName, {
88
- isRecording: this.AVRecorderInstance.isRecording,
89
- currentPosition: this.AVRecorderInstance.milliseconds,
90
- currentMetering: dB,
91
- });
92
- } else {
93
- this.ctx.rnInstance.emitDeviceEvent(RecordBackName, {
94
- isRecording: this.AVRecorderInstance.isRecording,
95
- currentPosition: this.AVRecorderInstance.milliseconds,
96
- });
97
- }
98
- }, this.subscriptionDuration);
99
- });
100
- }
101
-
102
- /**
103
- * Set listener from native module for player.
104
- * @returns {callBack((e: PlayBackType): void)}
105
- */
106
- public removeRecordBackListener(): void {
107
- clearInterval(this.RecordBackListenerTimer);
108
- this.RecordBackListenerTimer = null;
109
- }
110
-
111
- /**
112
- * remove listener for player.
113
- * @returns {void}
114
- */
115
- public addPlayBackListener(): void {
116
- if (this.PlayBackListenerTimer) {
117
- this.removePlayBackListener();
118
- }
119
-
120
- this.AVPlayerInstance.onEvent(EventTarget.timeUpdate, () => {
121
- this.PlayBackListenerTimer = setInterval(() => {
122
- this.ctx.rnInstance.emitDeviceEvent(playBackName, {
123
- isMuted: this.AVPlayerInstance.isMuted,
124
- isFinished: this.AVPlayerInstance.isFinished,
125
- currentPosition: this.AVPlayerInstance.currentPosition,
126
- duration: this.AVPlayerInstance.durationTime,
127
- });
128
- if (this.AVPlayerInstance.isFinished) {
129
- this.stopPlayer();
130
- }
131
- }, this.subscriptionDuration);
132
- });
133
- }
134
-
135
- public removePlayBackListener(): void {
136
- clearInterval(this.PlayBackListenerTimer);
137
- this.PlayBackListenerTimer = null;
138
- }
139
-
140
- /**
141
- * start recording with param.
142
- * @param {string} uri audio uri.
143
- * @returns {Promise<string>}
144
- */
145
- public async startRecorder(
146
- uri?: string,
147
- audioSets?: AudioSet,
148
- meteringEnabled?: boolean,
149
- ): Promise<string> {
150
- if (this.isRecording()) {
151
- return Promise.reject(processValue.play);
152
- }
153
- this.meteringEnabled = meteringEnabled === true ? true : false;
154
- const profile: media.AVRecorderProfile = this.getAudioSets(audioSets);
155
- this.AVRecorderInstance.setAVProfile(profile, audioSets?.AudioSourceHarmony as number as media.AudioSourceType);
156
- return this.AVRecorderInstance.startRecordingProcess(uri);
157
- }
158
-
159
- getAudioSets(audioSets?: AudioSet): media.AVRecorderProfile {
160
- let audioCodec: media.CodecMimeType = media.CodecMimeType.AUDIO_AAC;
161
- let fileFormat: media.ContainerFormatType =
162
- audioSets?.AudioFileFormatHarmony as string as media.ContainerFormatType ||
163
- media.ContainerFormatType.CFT_MPEG_4; // 封装格式,当前只支持m4a, 12开始支持CFT_MP3, 20开始支持aac
164
- if (audioSets?.AudioMimeHarmony) {
165
- audioCodec = audioSets.AudioMimeHarmony as string as media.CodecMimeType;
166
- }
167
-
168
- if (deviceInfo.sdkApiVersion < 20 && fileFormat === 'aac') {
169
- fileFormat = media.ContainerFormatType.CFT_MPEG_4;
170
- }
171
- const avProfile: media.AVRecorderProfile = {
172
- audioBitrate:
173
- audioSets?.AudioSamplingRateHarmony ||
174
- defaultBitrate, // 音频比特率
175
- audioChannels:
176
- audioSets?.AudioChannelsHarmony ||
177
- defaultChannels, // 音频声道数
178
- audioCodec: audioCodec, // 音频编码格式,当前只支持aac、vorbis、flac, 12开始支持AUDIO_MP3
179
- audioSampleRate:
180
- audioSets?.AudioSamplingRateHarmony ||
181
- defaultSampleRate, // 音频采样率
182
- fileFormat: fileFormat, // 音频文件封装格式
183
- };
184
- return avProfile;
185
- }
186
-
187
- /**
188
- * Pause recording.
189
- * @returns {Promise<string>}
190
- */
191
- public async pauseRecorder(): Promise<string> {
192
- if (this.AVRecorderInstance.recorderState === AVRecorderState.started) {
193
- this.removeRecordBackListener();
194
- return this.AVRecorderInstance.pauseRecordingProcess();
195
- }
196
- return Promise.reject(processValue.stateError);
197
- }
198
-
199
- /**
200
- * Resume recording.
201
- * @returns {Promise<string>}
202
- */
203
- public async resumeRecorder(): Promise<string> {
204
- if (this.AVRecorderInstance.recorderState === AVRecorderState.paused) {
205
- this.addRecordBackListener();
206
- return this.AVRecorderInstance.resumeRecordingProcess();
207
- }
208
- return Promise.reject(processValue.stateError);
209
- }
210
-
211
- /**
212
- * stop recording.
213
- * @returns {Promise<string>}
214
- */
215
- public async stopRecorder(): Promise<string> {
216
- if (
217
- [AVRecorderState.started, AVRecorderState.paused].includes(
218
- this.AVRecorderInstance.recorderState,
219
- )
220
- ) {
221
- // 仅在started或者paused状态下调用stop为合理状态切换
222
- this.removeRecordBackListener();
223
- return this.AVRecorderInstance.stopRecordingProcess();
224
- }
225
- return Promise.reject(processValue.stateError);
226
- }
227
-
228
- public async startPlayer(uri?: string, httpHeaders?: Record<string, string>): Promise<string> {
229
- if (this.AVPlayerInstance.PlayerState === AVPlayerState.paused) {
230
- return this.resumePlayer();
231
- }
232
- const url = await this.AVPlayerInstance.startPlayeringProcess(uri, httpHeaders);
233
- !!url && this.addPlayBackListener();
234
- return url;
235
- }
236
-
237
- /**
238
- * Start playing with param.
239
- * @param {string} uri audio uri.
240
- * @returns {Promise<string>}
241
- */
242
- public async resumePlayer(): Promise<string> {
243
- if (this.AVPlayerInstance.PlayerState === AVPlayerState.paused) {
244
- this.addPlayBackListener();
245
- return this.AVPlayerInstance.resumePlayeringProcess();
246
- }
247
- return Promise.reject(processValue.stateError);
248
- }
249
-
250
- /**
251
- * Resume playing.
252
- * @returns {Promise<string>}
253
- */
254
- public async stopPlayer(): Promise<string> {
255
- this.removePlayBackListener();
256
- return this.AVPlayerInstance.stopPlayeringProcess();
257
- }
258
-
259
- /**
260
- * Stop playing.
261
- * @returns {Promise<string>}
262
- */
263
- public async pausePlayer(): Promise<string> {
264
- if (this.AVPlayerInstance.PlayerState === AVPlayerState.playing) {
265
- this.removePlayBackListener();
266
- return this.AVPlayerInstance.pausePlayeringProcess();
267
- }
268
- return Promise.reject(processValue.stateError);
269
- }
270
-
271
- /**
272
- * Pause playing.
273
- * @returns {Promise<string>}
274
- */
275
- public async seekToPlayer(time: number): Promise<string> {
276
- if (!this.AVPlayerInstance.seekToPlayer) {
277
- return Promise.reject(processValue.stateError);
278
- }
279
- return this.AVPlayerInstance.seekToPlayer(time);
280
- }
281
-
282
- /**
283
- * Seek to.
284
- * @param {number} time position seek to in millisecond.
285
- * @returns {Promise<string>}
286
- */
287
- async setVolume(volume: number): Promise<string> {
288
- if (!this.AVPlayerInstance.setVolume) {
289
- return Promise.reject(processValue.stateError);
290
- }
291
- return this.AVPlayerInstance.setVolume(volume);
292
- }
293
-
294
- /**
295
- * Set volume.
296
- * @param {number} setVolume set volume.
297
- * @returns {Promise<string>}
298
- */
299
- async setSubscriptionDuration(sec: number): Promise<string> {
300
- const milliseconds = sec * 1000;
301
- this.subscriptionDuration = milliseconds;
302
- return sec.toString();
303
- }
304
-
305
- private isRecording(): boolean {
306
- return [AVRecorderState.started, AVRecorderState.paused].includes(
307
- this.AVRecorderInstance.recorderState,
308
- );
309
- }
310
- }
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 type { 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 '@kit.BasicServicesKit';
32
+
33
+ export class RNAudioRecorderPlayerTurboModule extends TurboModule {
34
+ public ctx: TurboModuleContext;
35
+ private AVPlayerInstance: AudioPlayer;
36
+ private AVRecorderInstance: Recorder;
37
+ private subscriptionDuration: number = 500;
38
+ private RecordBackListenerTimer;
39
+ private PlayBackListenerTimer;
40
+ private meteringEnabled: boolean = false;
41
+
42
+ constructor(ctx: TurboModuleContext) {
43
+ super(ctx);
44
+ this.AVRecorderInstance = new Recorder(ctx.uiAbilityContext);
45
+ this.AVPlayerInstance = new AudioPlayer(ctx.uiAbilityContext);
46
+ this.ctx = ctx;
47
+ }
48
+
49
+ addListener(eventName: string): void {
50
+ if (eventName === RecordBackName && !this.isRecording()) {
51
+ this.addRecordBackListener();
52
+ }
53
+ if (eventName === playBackName) {
54
+ this.addPlayBackListener();
55
+ }
56
+ }
57
+
58
+ removeListeners(count: number): void {
59
+ }
60
+
61
+ /**
62
+ * Set listerner from native module for recorder.
63
+ * @returns {callBack((e: RecordBackType): void)}
64
+ */
65
+ public addRecordBackListener(): void {
66
+ if (this.RecordBackListenerTimer) {
67
+ this.removeRecordBackListener();
68
+ }
69
+ this.AVRecorderInstance.onEvent(() => {
70
+ this.RecordBackListenerTimer = setInterval(async () => {
71
+ if (this.meteringEnabled) {
72
+ let maxAmplitude: number = 0;
73
+ let mediaRecorder: Recorder = this.AVRecorderInstance;
74
+ if (mediaRecorder != null) {
75
+ maxAmplitude = await mediaRecorder.getMaxAmplitude();
76
+ }
77
+ let dB: number = -160.0;
78
+ let maxAudioSize: number = 32767.0;
79
+ if (maxAmplitude > 0) {
80
+ dB = 20 * Math.log10(maxAmplitude / maxAudioSize);
81
+ dB = Math.floor(dB);
82
+ }
83
+ this.ctx.rnInstance.emitDeviceEvent(RecordBackName, {
84
+ isRecording: this.AVRecorderInstance.isRecording,
85
+ currentPosition: this.AVRecorderInstance.milliseconds,
86
+ currentMetering: dB,
87
+ });
88
+ } else {
89
+ this.ctx.rnInstance.emitDeviceEvent(RecordBackName, {
90
+ isRecording: this.AVRecorderInstance.isRecording,
91
+ currentPosition: this.AVRecorderInstance.milliseconds,
92
+ });
93
+ }
94
+ }, this.subscriptionDuration);
95
+ });
96
+ }
97
+
98
+ /**
99
+ * Remove listener for recorder.
100
+ * @returns {void}
101
+ */
102
+ public removeRecordBackListener(): void {
103
+ clearInterval(this.RecordBackListenerTimer);
104
+ this.RecordBackListenerTimer = null;
105
+ this.AVRecorderInstance.clearStartRecordCallback();
106
+ }
107
+
108
+ /**
109
+ * Set listener from native module for player.
110
+ * @returns {callBack((e: PlayBackType): void)}
111
+ */
112
+ public addPlayBackListener(): void {
113
+ if (this.PlayBackListenerTimer) {
114
+ this.removePlayBackListener();
115
+ }
116
+
117
+ this.AVPlayerInstance.onEvent(EventTarget.timeUpdate, () => {
118
+ this.PlayBackListenerTimer = setInterval(() => {
119
+ this.ctx.rnInstance.emitDeviceEvent(playBackName, {
120
+ isMuted: this.AVPlayerInstance.isMuted,
121
+ isFinished: this.AVPlayerInstance.isFinished,
122
+ currentPosition: this.AVPlayerInstance.currentPosition,
123
+ duration: this.AVPlayerInstance.durationTime,
124
+ });
125
+ if (this.AVPlayerInstance.isFinished) {
126
+ this.stopPlayer();
127
+ }
128
+ }, this.subscriptionDuration);
129
+ });
130
+ }
131
+
132
+ /**
133
+ * remove listener for player.
134
+ * @returns {void}
135
+ */
136
+ public removePlayBackListener(): void {
137
+ clearInterval(this.PlayBackListenerTimer);
138
+ this.PlayBackListenerTimer = null;
139
+ this.AVPlayerInstance.mapClear();
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
+
156
+ if (!!!uri) {
157
+ return Promise.reject('empty uri');
158
+ }
159
+ this.meteringEnabled = meteringEnabled === true ? true : false;
160
+ const profile: media.AVRecorderProfile = this.getAudioSets(audioSets);
161
+ this.AVRecorderInstance.setAVProfile(profile, audioSets?.AudioSourceHarmony as number as media.AudioSourceType);
162
+ return this.AVRecorderInstance.startRecordingProcess(uri);
163
+ }
164
+
165
+ getAudioSets(audioSets?: AudioSet): media.AVRecorderProfile {
166
+ let audioCodec: media.CodecMimeType = media.CodecMimeType.AUDIO_AAC;
167
+ let fileFormat: media.ContainerFormatType =
168
+ audioSets?.AudioFileFormatHarmony as string as media.ContainerFormatType ||
169
+ media.ContainerFormatType.CFT_MPEG_4A; // 封装格式,当前只支持m4a, 12开始支持CFT_MP3, 20开始支持aac
170
+ if (audioSets?.AudioMimeHarmony) {
171
+ audioCodec = audioSets.AudioMimeHarmony as string as media.CodecMimeType;
172
+ }
173
+
174
+ if (deviceInfo.sdkApiVersion < 20 && fileFormat === 'aac') {
175
+ fileFormat = media.ContainerFormatType.CFT_MPEG_4;
176
+ }
177
+ const avProfile: media.AVRecorderProfile = {
178
+ audioBitrate:
179
+ audioSets?.AudioSamplingRateHarmony ||
180
+ defaultBitrate, // 音频比特率
181
+ audioChannels:
182
+ audioSets?.AudioChannelsHarmony ||
183
+ defaultChannels, // 音频声道数
184
+ audioCodec: audioCodec, // 音频编码格式,当前只支持aac、vorbis、flac, 12开始支持AUDIO_MP3
185
+ audioSampleRate:
186
+ audioSets?.AudioSamplingRateHarmony ||
187
+ defaultSampleRate, // 音频采样率
188
+ fileFormat: fileFormat, // 音频文件封装格式
189
+ };
190
+ return avProfile;
191
+ }
192
+
193
+ /**
194
+ * Pause recording.
195
+ * @returns {Promise<string>}
196
+ */
197
+ public async pauseRecorder(): Promise<string> {
198
+ if (this.AVRecorderInstance.recorderState === AVRecorderState.started) {
199
+ this.removeRecordBackListener();
200
+ return this.AVRecorderInstance.pauseRecordingProcess();
201
+ }
202
+ return Promise.reject(processValue.stateError);
203
+ }
204
+
205
+ /**
206
+ * Resume recording.
207
+ * @returns {Promise<string>}
208
+ */
209
+ public async resumeRecorder(): Promise<string> {
210
+ if (this.AVRecorderInstance.recorderState === AVRecorderState.paused) {
211
+ this.addRecordBackListener();
212
+ return this.AVRecorderInstance.resumeRecordingProcess();
213
+ }
214
+ return Promise.reject(processValue.stateError);
215
+ }
216
+
217
+ /**
218
+ * stop recording.
219
+ * @returns {Promise<string>}
220
+ */
221
+ public async stopRecorder(): Promise<string> {
222
+ if (
223
+ [AVRecorderState.started, AVRecorderState.paused].includes(
224
+ this.AVRecorderInstance.recorderState,
225
+ )
226
+ ) {
227
+ // 仅在started或者paused状态下调用stop为合理状态切换
228
+ this.removeRecordBackListener();
229
+ return this.AVRecorderInstance.stopRecordingProcess();
230
+ }
231
+ return Promise.reject(processValue.stateError);
232
+ }
233
+
234
+ /**
235
+ * Start playing with param.
236
+ * @param {string} uri audio uri.
237
+ * @returns {Promise<string>}
238
+ */
239
+ public async startPlayer(uri?: string, httpHeaders?: Record<string, string>): Promise<string> {
240
+ if (this.AVPlayerInstance.PlayerState === AVPlayerState.paused) {
241
+ return this.resumePlayer();
242
+ }
243
+ const url = await this.AVPlayerInstance.startPlayeringProcess(uri, httpHeaders);
244
+ !!url && this.addPlayBackListener();
245
+ return url;
246
+ }
247
+
248
+ /**
249
+ * Resume playing.
250
+ * @returns {Promise<string>}
251
+ */
252
+ public async resumePlayer(): Promise<string> {
253
+ if (this.AVPlayerInstance.PlayerState === AVPlayerState.paused) {
254
+ this.addPlayBackListener();
255
+ return this.AVPlayerInstance.resumePlayeringProcess();
256
+ }
257
+ return Promise.reject(processValue.stateError);
258
+ }
259
+
260
+ /**
261
+ * Stop playing.
262
+ * @returns {Promise<string>}
263
+ */
264
+ public async stopPlayer(): Promise<string> {
265
+ this.removePlayBackListener();
266
+ return this.AVPlayerInstance.stopPlayeringProcess();
267
+ }
268
+
269
+ /**
270
+ * Pause playing.
271
+ * @returns {Promise<string>}
272
+ */
273
+ public async pausePlayer(): Promise<string> {
274
+ if (this.AVPlayerInstance.PlayerState === AVPlayerState.playing) {
275
+ this.removePlayBackListener();
276
+ return this.AVPlayerInstance.pausePlayeringProcess();
277
+ }
278
+ return Promise.reject(processValue.stateError);
279
+ }
280
+
281
+ /**
282
+ * Seek to.
283
+ * @param {number} time position seek to in millisecond.
284
+ * @returns {Promise<string>}
285
+ */
286
+ public async seekToPlayer(time: number): Promise<string> {
287
+ if (!this.AVPlayerInstance.seekToPlayer) {
288
+ return Promise.reject(processValue.stateError);
289
+ }
290
+ return this.AVPlayerInstance.seekToPlayer(time);
291
+ }
292
+
293
+ /**
294
+ * Set volume.
295
+ * @param {number} setVolume set volume.
296
+ * @returns {Promise<string>}
297
+ */
298
+ async setVolume(volume: number): Promise<string> {
299
+ if (!this.AVPlayerInstance.setVolume) {
300
+ return Promise.reject(processValue.stateError);
301
+ }
302
+ return this.AVPlayerInstance.setVolume(volume);
303
+ }
304
+
305
+ async setSubscriptionDuration(sec: number): Promise<string> {
306
+ const milliseconds = sec * 1000;
307
+ this.subscriptionDuration = milliseconds;
308
+ return sec.toString();
309
+ }
310
+
311
+ private isRecording(): boolean {
312
+ return [AVRecorderState.started, AVRecorderState.paused].includes(
313
+ this.AVRecorderInstance.recorderState,
314
+ );
315
+ }
316
+ }