@react-native-ohos/react-native-audio-recorder-player 3.6.14-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.OpenSource +11 -0
- package/README.md +14 -0
- package/RNAudioRecorderPlayer.podspec +21 -0
- package/harmony/audio_recorder_player/BuildProfile.ets +17 -0
- package/harmony/audio_recorder_player/LICENSE +21 -0
- package/harmony/audio_recorder_player/build-profile.json5 +28 -0
- package/harmony/audio_recorder_player/hvigorfile.ts +1 -0
- package/harmony/audio_recorder_player/index.ets +25 -0
- package/harmony/audio_recorder_player/oh-package-lock.json5 +18 -0
- package/harmony/audio_recorder_player/oh-package.json5 +20 -0
- package/harmony/audio_recorder_player/src/main/cpp/CMakeLists.txt +7 -0
- package/harmony/audio_recorder_player/src/main/cpp/RNAudioRecorderPlayerPackage.h +48 -0
- package/harmony/audio_recorder_player/src/main/cpp/RNAudioRecorderPlayerTurboModule.cpp +91 -0
- package/harmony/audio_recorder_player/src/main/cpp/RNAudioRecorderPlayerTurboModule.h +36 -0
- package/harmony/audio_recorder_player/src/main/ets/RNAudioRecorderPlayerPackage.ts +47 -0
- package/harmony/audio_recorder_player/src/main/ets/RNAudioRecorderPlayerTurboModule.ts +315 -0
- package/harmony/audio_recorder_player/src/main/ets/audioPlayer/AudioPlayer.ts +464 -0
- package/harmony/audio_recorder_player/src/main/ets/audioPlayer/types.ts +45 -0
- package/harmony/audio_recorder_player/src/main/ets/index.d.ts +209 -0
- package/harmony/audio_recorder_player/src/main/ets/recorder/Recorder.ts +299 -0
- package/harmony/audio_recorder_player/src/main/ets/recorder/types.ts +61 -0
- package/harmony/audio_recorder_player/src/main/ets/utils/DateTimeUtils.ts +88 -0
- package/harmony/audio_recorder_player/src/main/ets/utils/Logger.ts +45 -0
- package/harmony/audio_recorder_player/src/main/ets/utils/SaveAudioAsset.ts +105 -0
- package/harmony/audio_recorder_player/src/main/module.json5 +9 -0
- package/harmony/audio_recorder_player/src/main/resources/base/element/string.json +8 -0
- package/harmony/audio_recorder_player/src/main/resources/base/media/ic_public_error.svg +10 -0
- package/harmony/audio_recorder_player/src/main/resources/en_US/element/string.json +8 -0
- package/harmony/audio_recorder_player/src/main/resources/zh_CN/element/string.json +8 -0
- package/harmony/audio_recorder_player/ts.ts +27 -0
- package/harmony/audio_recorder_player.har +0 -0
- package/index.d.ts +226 -0
- package/index.ts +538 -0
- package/package.json +59 -0
- package/src/NativeAudioRecorderPlayerModule.ts +43 -0
|
@@ -0,0 +1,464 @@
|
|
|
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 media from '@ohos.multimedia.media';
|
|
8
|
+
import { BusinessError } from '@kit.BasicServicesKit';
|
|
9
|
+
import fs from '@ohos.file.fs';
|
|
10
|
+
import { common } from '@kit.AbilityKit';
|
|
11
|
+
import SaveAudioAsset from '../utils/SaveAudioAsset';
|
|
12
|
+
import Logger from '../utils/Logger';
|
|
13
|
+
import { AVPlayerState, EventTarget, pathStartWith, processValue, TAG, } from './types';
|
|
14
|
+
import { fdPath } from '../recorder/types';
|
|
15
|
+
|
|
16
|
+
const SPEED_MAPPING: Record<number, media.PlaybackSpeed> = {
|
|
17
|
+
0.125: media.PlaybackSpeed.SPEED_FORWARD_0_125_X,
|
|
18
|
+
0.25: media.PlaybackSpeed.SPEED_FORWARD_0_25_X,
|
|
19
|
+
0.5: media.PlaybackSpeed.SPEED_FORWARD_0_50_X,
|
|
20
|
+
0.75: media.PlaybackSpeed.SPEED_FORWARD_0_75_X,
|
|
21
|
+
1: media.PlaybackSpeed.SPEED_FORWARD_1_00_X,
|
|
22
|
+
1.25: media.PlaybackSpeed.SPEED_FORWARD_1_25_X,
|
|
23
|
+
1.5: media.PlaybackSpeed.SPEED_FORWARD_1_50_X,
|
|
24
|
+
1.75: media.PlaybackSpeed.SPEED_FORWARD_1_75_X,
|
|
25
|
+
2: media.PlaybackSpeed.SPEED_FORWARD_2_00_X,
|
|
26
|
+
3: media.PlaybackSpeed.SPEED_FORWARD_3_00_X,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export default class Player {
|
|
30
|
+
public isPlaying: boolean = false;
|
|
31
|
+
public currentPosition: number = 0;
|
|
32
|
+
public isMuted: boolean = false;
|
|
33
|
+
public isFinished: boolean = false;
|
|
34
|
+
public durationTime: number = 0;
|
|
35
|
+
public PlayerState: AVPlayerState;
|
|
36
|
+
private audioPlayer: media.AVPlayer;
|
|
37
|
+
private AudioAsset: SaveAudioAsset;
|
|
38
|
+
private playUrl: string;
|
|
39
|
+
private EventMap: Map<string, any> = new Map();
|
|
40
|
+
private context: common.UIAbilityContext;
|
|
41
|
+
|
|
42
|
+
constructor(context: common.UIAbilityContext) {
|
|
43
|
+
this.context = context;
|
|
44
|
+
this.AudioAsset = new SaveAudioAsset(TAG, context);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public onEvent(name: string, callback: () => void) {
|
|
48
|
+
if (this.EventMap.has(name)) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
this.EventMap.set(name, callback);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public async setPlaybackSpeed(playbackSpeed: number): Promise<string> {
|
|
55
|
+
if (this.isHandleState()) {
|
|
56
|
+
return this.setSpeed(playbackSpeed);
|
|
57
|
+
}
|
|
58
|
+
return new Promise((resolve, reject) => {
|
|
59
|
+
this.onEvent(EventTarget.setPlaybackSpeed, async () => {
|
|
60
|
+
try {
|
|
61
|
+
const speed = await this.setSpeed(playbackSpeed);
|
|
62
|
+
speed && resolve(speed);
|
|
63
|
+
} catch (e) {
|
|
64
|
+
reject(processValue.stateError);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
public async setVolume(volume: number): Promise<string> {
|
|
71
|
+
if (this.isHandleState()) {
|
|
72
|
+
return this.setPlayerVolume(volume);
|
|
73
|
+
}
|
|
74
|
+
return new Promise((resolve, reject) => {
|
|
75
|
+
this.onEvent(EventTarget.setVolume, async () => {
|
|
76
|
+
try {
|
|
77
|
+
const volumes = await this.setPlayerVolume(volume);
|
|
78
|
+
volumes && resolve(volumes);
|
|
79
|
+
} catch (e) {
|
|
80
|
+
reject(processValue.stateError);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public seekToPlayer(time: number): Promise<string> {
|
|
87
|
+
if (this.isHandleState()) {
|
|
88
|
+
return this.seekToPlayerTime(time);
|
|
89
|
+
}
|
|
90
|
+
return new Promise((resolve, reject) => {
|
|
91
|
+
this.onEvent(EventTarget.seekToPlayer, async () => {
|
|
92
|
+
try {
|
|
93
|
+
const times = await this.seekToPlayerTime(time);
|
|
94
|
+
times && resolve(times);
|
|
95
|
+
} catch (e) {
|
|
96
|
+
reject(processValue.stateError);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
public seekToPlayerTime(time: number): Promise<string> {
|
|
103
|
+
return new Promise((resolve) => {
|
|
104
|
+
this.audioPlayer.seek(time);
|
|
105
|
+
this.audioPlayer.on(EventTarget.seekDone, (seekDoneTime: number) => {
|
|
106
|
+
resolve(seekDoneTime.toString());
|
|
107
|
+
this.audioPlayer.off(EventTarget.seekDone);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
isHandleState() {
|
|
113
|
+
return [
|
|
114
|
+
AVPlayerState.playing,
|
|
115
|
+
AVPlayerState.prepared,
|
|
116
|
+
AVPlayerState.paused,
|
|
117
|
+
AVPlayerState.completed,
|
|
118
|
+
].includes(this.audioPlayer.state as AVPlayerState);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// start button process
|
|
122
|
+
async startPlayeringProcess(url?: string, httpHeaders?: Record<string, string>): Promise<string> {
|
|
123
|
+
Logger.debug(TAG, 'startPlayering called');
|
|
124
|
+
if (this.isPlaying || this.PlayerState === AVPlayerState.paused) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
try {
|
|
128
|
+
await this.createAudioPlayer();
|
|
129
|
+
await this.setAudioPlayerUrl(url, httpHeaders);
|
|
130
|
+
Logger.debug(TAG, 'startPlayering done');
|
|
131
|
+
return this.playUrl;
|
|
132
|
+
} catch (err) {
|
|
133
|
+
Logger.error(TAG, `startPlayering fail code:${(err as BusinessError).code}`);
|
|
134
|
+
this.stopPlayeringProcess();
|
|
135
|
+
return this.playUrl;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// pause button process
|
|
140
|
+
async pausePlayeringProcess(): Promise<string> {
|
|
141
|
+
Logger.debug(TAG, 'pausePlayering called');
|
|
142
|
+
if (this.PlayerState === AVPlayerState.playing) {
|
|
143
|
+
Logger.debug(TAG, 'current state is playing, to pause');
|
|
144
|
+
return this.handelPlayerFunc(
|
|
145
|
+
this.audioPlayer.pause(),
|
|
146
|
+
processValue.pause,
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
Logger.debug(TAG, 'pausePlayering done');
|
|
150
|
+
return processValue.stateError;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async resumePlayeringProcess(): Promise<string> {
|
|
154
|
+
Logger.debug(TAG, ' resumePlayering called');
|
|
155
|
+
if (this.PlayerState === AVPlayerState.paused) {
|
|
156
|
+
Logger.debug(TAG, 'current state is paused, to resume');
|
|
157
|
+
this.seekToPlayerTime(this.currentPosition).then(() => {
|
|
158
|
+
this.getCurrentPosition();
|
|
159
|
+
this.audioPlayer.play();
|
|
160
|
+
return processValue.resume;
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
Logger.debug(TAG, ' resumePlayering done');
|
|
164
|
+
return processValue.stateError;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// stop button process
|
|
168
|
+
async stopPlayeringProcess(): Promise<string> {
|
|
169
|
+
Logger.debug(TAG, 'stopPlayering called');
|
|
170
|
+
if (!this.isHandleState()) {
|
|
171
|
+
return processValue.stateError;
|
|
172
|
+
}
|
|
173
|
+
this.isPlaying = false;
|
|
174
|
+
this.currentPosition = 0;
|
|
175
|
+
this.durationTime = 0;
|
|
176
|
+
await this.resetAudioPlayering();
|
|
177
|
+
await this.stopAudioPlayering();
|
|
178
|
+
await this.releaseAudioPlayer();
|
|
179
|
+
Logger.debug(TAG, 'stopPlayering done');
|
|
180
|
+
return this.playUrl;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
private async failureCallback(error: BusinessError): Promise<void> {
|
|
184
|
+
Logger.debug(
|
|
185
|
+
TAG,
|
|
186
|
+
`failureCallback code:${error.code} message:${error.message}`,
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
private async catchCallback(error: BusinessError): Promise<void> {
|
|
191
|
+
Logger.debug(
|
|
192
|
+
TAG,
|
|
193
|
+
`catchCallback code:${error.code} message:${error.message}`,
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private getDurationTime() {
|
|
198
|
+
this.audioPlayer.on(EventTarget.durationUpdate, (duration: number) => {
|
|
199
|
+
Logger.debug(TAG, 'durationUpdate called,new duration is :' + duration);
|
|
200
|
+
this.durationTime = duration;
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
private getCurrentPosition() {
|
|
205
|
+
this.audioPlayer.on(EventTarget.timeUpdate, (time: number) => {
|
|
206
|
+
Logger.debug(TAG, 'timeUpdate called,and new time is :' + time);
|
|
207
|
+
this.currentPosition = time;
|
|
208
|
+
this.isFinished = this.currentPosition === this.durationTime;
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
private async setPlayerVolume(volume: number): Promise<string> {
|
|
213
|
+
if (volume < 0 || volume > 1) {
|
|
214
|
+
throw new Error('Value of volume should be between 0.0 to 1.0');
|
|
215
|
+
}
|
|
216
|
+
return new Promise((resolve) => {
|
|
217
|
+
this.audioPlayer.setVolume(volume);
|
|
218
|
+
this.audioPlayer.on(EventTarget.volumeChange, (vol: number) => {
|
|
219
|
+
if (vol >= 0) {
|
|
220
|
+
resolve(vol.toFixed(1));
|
|
221
|
+
} else {
|
|
222
|
+
resolve('');
|
|
223
|
+
}
|
|
224
|
+
this.audioPlayer.off(EventTarget.volumeChange);
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
private async setSpeed(playbackSpeed: number): Promise<string> {
|
|
230
|
+
let enumSpeed = this.convertToPlaybackSpeed(playbackSpeed);
|
|
231
|
+
if (enumSpeed === null) {
|
|
232
|
+
throw new Error('The set speed is not supported');
|
|
233
|
+
}
|
|
234
|
+
return new Promise((resolve) => {
|
|
235
|
+
this.audioPlayer.setSpeed(enumSpeed);
|
|
236
|
+
this.audioPlayer.on(EventTarget.speedDone, (speed: number) => {
|
|
237
|
+
resolve(speed.toString());
|
|
238
|
+
this.audioPlayer.off(EventTarget.speedDone);
|
|
239
|
+
});
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
private convertToPlaybackSpeed(speed: number): media.PlaybackSpeed | null {
|
|
244
|
+
if (SPEED_MAPPING.hasOwnProperty(speed)) {
|
|
245
|
+
return SPEED_MAPPING[speed];
|
|
246
|
+
}
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
private async createAudioPlayer(): Promise<void> {
|
|
251
|
+
if (this.audioPlayer) {
|
|
252
|
+
this.stopPlayeringProcess();
|
|
253
|
+
}
|
|
254
|
+
await media
|
|
255
|
+
.createAVPlayer()
|
|
256
|
+
.then((player) => {
|
|
257
|
+
Logger.debug(TAG, 'case createAVPlayer called');
|
|
258
|
+
if (!!Player) {
|
|
259
|
+
this.audioPlayer = player;
|
|
260
|
+
} else {
|
|
261
|
+
Logger.debug(TAG, 'case create avPlayer failed!!!');
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
}, this.failureCallback)
|
|
265
|
+
.catch(this.catchCallback);
|
|
266
|
+
this.setCallback();
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// set callback on
|
|
270
|
+
private setCallback(): void {
|
|
271
|
+
Logger.debug(TAG, 'case callback');
|
|
272
|
+
this.audioPlayer!.on(EventTarget.stateChange, async (state, reason) => {
|
|
273
|
+
Logger.debug(
|
|
274
|
+
TAG,
|
|
275
|
+
'case state has changed, new state is ' + state + 'reason: ' + reason,
|
|
276
|
+
);
|
|
277
|
+
this.isMuted = this.AudioAsset.mediaIsMute();
|
|
278
|
+
if (this.isHandleState()) {
|
|
279
|
+
for (const [key, value] of this.EventMap) {
|
|
280
|
+
if (key === EventTarget.timeUpdate) {
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
await value();
|
|
284
|
+
this.EventMap.delete(key);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
switch (state) {
|
|
288
|
+
case AVPlayerState.idle: {
|
|
289
|
+
this.PlayerState = AVPlayerState.idle;
|
|
290
|
+
break;
|
|
291
|
+
}
|
|
292
|
+
case AVPlayerState.initialized: {
|
|
293
|
+
this.getDurationTime();
|
|
294
|
+
this.prepareAudioPlayer();
|
|
295
|
+
this.getCurrentPosition();
|
|
296
|
+
}
|
|
297
|
+
case AVPlayerState.prepared: {
|
|
298
|
+
this.PlayerState = AVPlayerState.prepared;
|
|
299
|
+
this.isPlaying = false;
|
|
300
|
+
break;
|
|
301
|
+
}
|
|
302
|
+
case AVPlayerState.playing: {
|
|
303
|
+
this.PlayerState = AVPlayerState.playing;
|
|
304
|
+
this.isPlaying = true;
|
|
305
|
+
const callbackTimeChange = this.EventMap.get(EventTarget.timeUpdate);
|
|
306
|
+
callbackTimeChange && callbackTimeChange();
|
|
307
|
+
this.EventMap.delete(EventTarget.timeUpdate);
|
|
308
|
+
this.audioPlayer.off(EventTarget.durationUpdate);
|
|
309
|
+
break;
|
|
310
|
+
}
|
|
311
|
+
case AVPlayerState.paused: {
|
|
312
|
+
this.PlayerState = AVPlayerState.paused;
|
|
313
|
+
this.audioPlayer.off(EventTarget.timeUpdate);
|
|
314
|
+
this.isPlaying = false;
|
|
315
|
+
break;
|
|
316
|
+
}
|
|
317
|
+
case AVPlayerState.completed: {
|
|
318
|
+
this.isPlaying = false;
|
|
319
|
+
this.audioPlayer.off(EventTarget.timeUpdate);
|
|
320
|
+
// /
|
|
321
|
+
}
|
|
322
|
+
case AVPlayerState.stopped: {
|
|
323
|
+
this.PlayerState = AVPlayerState.stopped;
|
|
324
|
+
this.isPlaying = false;
|
|
325
|
+
break;
|
|
326
|
+
}
|
|
327
|
+
case AVPlayerState.released: {
|
|
328
|
+
this.PlayerState = AVPlayerState.released;
|
|
329
|
+
break;
|
|
330
|
+
}
|
|
331
|
+
default:
|
|
332
|
+
Logger.debug(TAG, 'case start is unknown');
|
|
333
|
+
break;
|
|
334
|
+
}
|
|
335
|
+
});
|
|
336
|
+
this.audioPlayer!.on(EventTarget.error, async (err) => {
|
|
337
|
+
this.audioPlayer.reset();
|
|
338
|
+
this.stopPlayeringProcess();
|
|
339
|
+
Logger.debug(
|
|
340
|
+
TAG,
|
|
341
|
+
'case avPlayer.on(error) called, errMessage is ' + err.message,
|
|
342
|
+
);
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
private async setAudioPlayerUrl(url?: string, httpHeaders?: Record<string, string>): Promise<void> {
|
|
347
|
+
Logger.debug(TAG, 'url is ', url);
|
|
348
|
+
if (pathStartWith.some((item) => url.startsWith(item))) {
|
|
349
|
+
if (!url?.startsWith(fdPath) && httpHeaders) {
|
|
350
|
+
this.playUrl = url;
|
|
351
|
+
let mediaSource: media.MediaSource = media.createMediaSourceWithUrl(this.playUrl, httpHeaders);
|
|
352
|
+
let playStrategy: media.PlaybackStrategy = {
|
|
353
|
+
preferredWidth: 1,
|
|
354
|
+
preferredHeight: 1,
|
|
355
|
+
preferredBufferDuration: 3,
|
|
356
|
+
preferredHdr: false
|
|
357
|
+
};
|
|
358
|
+
this.audioPlayer.setMediaSource(mediaSource, playStrategy);
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
this.audioPlayer.url = url;
|
|
362
|
+
this.playUrl = url;
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
const lastUrl = this.getFileList(url);
|
|
366
|
+
if (lastUrl) {
|
|
367
|
+
this.audioPlayer.url = fdPath + lastUrl;
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
Logger.error(TAG, 'this uri is not allowed');
|
|
371
|
+
return Promise.reject('this uri is not allowed');
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
private handelPlayerFunc(func: Promise<void>, msg: string): Promise<string> {
|
|
375
|
+
return new Promise((resolve, reject) => {
|
|
376
|
+
func
|
|
377
|
+
.then(() => {
|
|
378
|
+
resolve(msg);
|
|
379
|
+
}, reject)
|
|
380
|
+
.catch(reject);
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
private getFileList(url?: string): number {
|
|
385
|
+
const context = this.context as common.UIAbilityContext;
|
|
386
|
+
const path = context.filesDir;
|
|
387
|
+
let isExist = false;
|
|
388
|
+
if (url && url !== 'DEFAULT') {
|
|
389
|
+
isExist = fs.accessSync(`${path}/${url}`);
|
|
390
|
+
}
|
|
391
|
+
if (url && url === 'DEFAULT') {
|
|
392
|
+
isExist = fs.accessSync(`${path}/${'sound.m4a'}`);
|
|
393
|
+
}
|
|
394
|
+
if (isExist) {
|
|
395
|
+
this.playUrl = `${path}/${url}`;
|
|
396
|
+
const file = fs.openSync(this.playUrl);
|
|
397
|
+
return file.fd;
|
|
398
|
+
} else {
|
|
399
|
+
const fileList: string[] = fs.listFileSync(path) ?? [];
|
|
400
|
+
const audioList = fileList.filter((item: string) => item.endsWith(media.ContainerFormatType.CFT_MPEG_4A));
|
|
401
|
+
const last = audioList.pop();
|
|
402
|
+
Logger.debug(TAG, last);
|
|
403
|
+
if (last) {
|
|
404
|
+
this.playUrl = `${path}/${last}`;
|
|
405
|
+
const file = fs.openSync(this.playUrl);
|
|
406
|
+
Logger.debug(TAG, 'last file path ', last);
|
|
407
|
+
return file.fd;
|
|
408
|
+
}
|
|
409
|
+
return null;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
private async prepareAudioPlayer(): Promise<void> {
|
|
414
|
+
Logger.debug(TAG, 'case prepareAudioPlayer in');
|
|
415
|
+
await this.audioPlayer!.prepare()
|
|
416
|
+
.then(() => {
|
|
417
|
+
Logger.debug(TAG, 'case prepare AVPlayer called');
|
|
418
|
+
this.startAudioPlayering();
|
|
419
|
+
}, this.failureCallback)
|
|
420
|
+
.catch(this.catchCallback);
|
|
421
|
+
Logger.debug(TAG, 'case prepareAudioPlayer out');
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
private async startAudioPlayering(): Promise<void> {
|
|
425
|
+
Logger.debug(TAG, 'case startAudioPlayering called');
|
|
426
|
+
await this.audioPlayer!.play()
|
|
427
|
+
.then(() => {
|
|
428
|
+
Logger.debug(TAG, 'case start AudioPlayer called');
|
|
429
|
+
}, this.failureCallback)
|
|
430
|
+
.catch(this.catchCallback);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
private async stopAudioPlayering(): Promise<void> {
|
|
434
|
+
Logger.debug(TAG, 'case stopAudioPlayering called');
|
|
435
|
+
await this.audioPlayer!.stop()
|
|
436
|
+
.then(() => {
|
|
437
|
+
Logger.debug(TAG, 'case stop AudioPlayer called');
|
|
438
|
+
}, this.failureCallback)
|
|
439
|
+
.catch(this.catchCallback);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
private async resetAudioPlayering(): Promise<void> {
|
|
443
|
+
await this.audioPlayer!.reset()
|
|
444
|
+
.then(() => {
|
|
445
|
+
Logger.debug(TAG, 'case resetAudioPlayering called');
|
|
446
|
+
}, this.failureCallback)
|
|
447
|
+
.catch(this.catchCallback);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
private async releaseAudioPlayer(): Promise<void> {
|
|
451
|
+
if (this.audioPlayer) {
|
|
452
|
+
this.audioPlayer.off(EventTarget.stateChange);
|
|
453
|
+
this.audioPlayer.off(EventTarget.error);
|
|
454
|
+
this.audioPlayer.off(EventTarget.timeUpdate);
|
|
455
|
+
await this.audioPlayer
|
|
456
|
+
.release()
|
|
457
|
+
.then(() => {
|
|
458
|
+
Logger.debug(TAG, 'case releaseAudioPlayer called');
|
|
459
|
+
}, this.failureCallback)
|
|
460
|
+
.catch(this.catchCallback);
|
|
461
|
+
this.audioPlayer = undefined;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
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
|
+
export const TAG: string = 'Audio_Player';
|
|
8
|
+
|
|
9
|
+
export const playBackName: string = 'rn-playback';
|
|
10
|
+
|
|
11
|
+
export enum EventTarget {
|
|
12
|
+
durationUpdate = 'durationUpdate',
|
|
13
|
+
timeUpdate = 'timeUpdate',
|
|
14
|
+
stateChange = 'stateChange',
|
|
15
|
+
error = 'error',
|
|
16
|
+
seekDone = 'seekDone',
|
|
17
|
+
volumeChange = 'volumeChange',
|
|
18
|
+
seekToPlayer = 'seekToPlayer',
|
|
19
|
+
setVolume = 'setVolume',
|
|
20
|
+
setPlaybackSpeed = 'setPlaybackSpeed',
|
|
21
|
+
speedDone = 'speedDone',
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export enum AVPlayerState {
|
|
25
|
+
idle = 'idle',
|
|
26
|
+
initialized = 'initialized',
|
|
27
|
+
prepared = 'prepared',
|
|
28
|
+
playing = 'playing',
|
|
29
|
+
paused = 'paused',
|
|
30
|
+
completed = 'completed',
|
|
31
|
+
stopped = 'stopped',
|
|
32
|
+
released = 'released',
|
|
33
|
+
error = 'error',
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export enum processValue {
|
|
37
|
+
play = 'playing',
|
|
38
|
+
resume = 'Already playing',
|
|
39
|
+
stop = 'Already stopped playing',
|
|
40
|
+
pause = 'Already paused playing',
|
|
41
|
+
noPlaying = 'No audio playing',
|
|
42
|
+
stateError = 'current state is not allowed',
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const pathStartWith: Array<string> = ['fd://', 'http://', 'https://'];
|
|
@@ -0,0 +1,209 @@
|
|
|
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
|
+
export enum AudioSourceHarmonyType {
|
|
26
|
+
DEFAULT = 0,
|
|
27
|
+
MIC,
|
|
28
|
+
VOICE_RECOGNITION,
|
|
29
|
+
VOICE_COMMUNICATION = 7,
|
|
30
|
+
VOICE_MESSAGE = 10,
|
|
31
|
+
CAMCORDER = 12,
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export enum AudioFormatHarmonyType {
|
|
35
|
+
MPEG_4 = 'mp4',
|
|
36
|
+
MPEG_4A = 'm4a',
|
|
37
|
+
MP3 = 'mp3',
|
|
38
|
+
WAV = 'wav'
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export enum AudioMimeHarmonyType {
|
|
42
|
+
VIDEO_H263 = 'video/h263', // 表示视频/h263类型。
|
|
43
|
+
VIDEO_AVC = 'video/avc', // 表示视频/avc类型。
|
|
44
|
+
VIDEO_MPEG2 = 'video/mpeg2', // 表示视频/mpeg2类型。
|
|
45
|
+
VIDEO_MPEG4 = 'video/mp4v-es', // 表示视频/mpeg4类型。
|
|
46
|
+
VIDEO_VP8 = 'video/x-vnd.on2.vp8', // 表示视频/vp8类型。
|
|
47
|
+
VIDEO_HEVC = 'video/hevc', // 表示视频/H265类型。
|
|
48
|
+
AUDIO_AAC = 'audio/mp4a-latm', // 表示音频/mp4a-latm类型。
|
|
49
|
+
|
|
50
|
+
AUDIO_VORBIS = 'audio/vorbis', // 表示音频/vorbis类型。
|
|
51
|
+
AUDIO_FLAC = 'audio/flac', // 表示音频/flac类型。
|
|
52
|
+
AUDIO_MP3 = 'audio/mpeg', // 表示音频/mpeg类型。
|
|
53
|
+
AUDIO_G711MU = 'audio/g711mu', // 表示音频/G711-mulaw类型。
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export enum AudioSourceAndroidType {
|
|
57
|
+
DEFAULT = 0,
|
|
58
|
+
MIC,
|
|
59
|
+
VOICE_UPLINK,
|
|
60
|
+
VOICE_DOWNLINK,
|
|
61
|
+
VOICE_CALL,
|
|
62
|
+
CAMCORDER,
|
|
63
|
+
VOICE_RECOGNITION,
|
|
64
|
+
VOICE_COMMUNICATION,
|
|
65
|
+
REMOTE_SUBMIX,
|
|
66
|
+
UNPROCESSED,
|
|
67
|
+
RADIO_TUNER = 1998,
|
|
68
|
+
HOTWORD,
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export enum OutputFormatAndroidType {
|
|
72
|
+
DEFAULT = 0,
|
|
73
|
+
THREE_GPP,
|
|
74
|
+
MPEG_4,
|
|
75
|
+
AMR_NB,
|
|
76
|
+
AMR_WB,
|
|
77
|
+
AAC_ADIF,
|
|
78
|
+
AAC_ADTS,
|
|
79
|
+
OUTPUT_FORMAT_RTP_AVP,
|
|
80
|
+
MPEG_2_TS,
|
|
81
|
+
WEBM,
|
|
82
|
+
UNUSED,
|
|
83
|
+
OGG,
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export enum AudioEncoderAndroidType {
|
|
87
|
+
DEFAULT = 0,
|
|
88
|
+
AMR_NB,
|
|
89
|
+
AMR_WB,
|
|
90
|
+
AAC,
|
|
91
|
+
HE_AAC,
|
|
92
|
+
AAC_ELD,
|
|
93
|
+
VORBIS,
|
|
94
|
+
OPUS,
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export enum AVEncodingOption {
|
|
98
|
+
lpcm = 'lpcm',
|
|
99
|
+
ima4 = 'ima4',
|
|
100
|
+
aac = 'aac',
|
|
101
|
+
MAC3 = 'MAC3',
|
|
102
|
+
MAC6 = 'MAC6',
|
|
103
|
+
ulaw = 'ulaw',
|
|
104
|
+
alaw = 'alaw',
|
|
105
|
+
mp1 = 'mp1',
|
|
106
|
+
mp2 = 'mp2',
|
|
107
|
+
mp4 = 'mp4',
|
|
108
|
+
alac = 'alac',
|
|
109
|
+
amr = 'amr',
|
|
110
|
+
flac = 'flac',
|
|
111
|
+
opus = 'opus',
|
|
112
|
+
wav = 'wav',
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
type AVEncodingType =
|
|
116
|
+
| AVEncodingOption.lpcm
|
|
117
|
+
| AVEncodingOption.ima4
|
|
118
|
+
| AVEncodingOption.aac
|
|
119
|
+
| AVEncodingOption.MAC3
|
|
120
|
+
| AVEncodingOption.MAC6
|
|
121
|
+
| AVEncodingOption.ulaw
|
|
122
|
+
| AVEncodingOption.alaw
|
|
123
|
+
| AVEncodingOption.mp1
|
|
124
|
+
| AVEncodingOption.mp2
|
|
125
|
+
| AVEncodingOption.mp4
|
|
126
|
+
| AVEncodingOption.alac
|
|
127
|
+
| AVEncodingOption.amr
|
|
128
|
+
| AVEncodingOption.flac
|
|
129
|
+
| AVEncodingOption.opus
|
|
130
|
+
| AVEncodingOption.wav;
|
|
131
|
+
|
|
132
|
+
export enum AVModeIOSOption {
|
|
133
|
+
gamechat = 'gamechat',
|
|
134
|
+
measurement = 'measurement',
|
|
135
|
+
movieplayback = 'movieplayback',
|
|
136
|
+
spokenaudio = 'spokenaudio',
|
|
137
|
+
videochat = 'videochat',
|
|
138
|
+
videorecording = 'videorecording',
|
|
139
|
+
voicechat = 'voicechat',
|
|
140
|
+
voiceprompt = 'voiceprompt',
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export type AVModeIOSType =
|
|
144
|
+
| AVModeIOSOption.gamechat
|
|
145
|
+
| AVModeIOSOption.measurement
|
|
146
|
+
| AVModeIOSOption.movieplayback
|
|
147
|
+
| AVModeIOSOption.spokenaudio
|
|
148
|
+
| AVModeIOSOption.videochat
|
|
149
|
+
| AVModeIOSOption.videorecording
|
|
150
|
+
| AVModeIOSOption.voicechat
|
|
151
|
+
| AVModeIOSOption.voiceprompt;
|
|
152
|
+
|
|
153
|
+
export enum AVEncoderAudioQualityIOSType {
|
|
154
|
+
min = 0,
|
|
155
|
+
low = 32,
|
|
156
|
+
medium = 64,
|
|
157
|
+
high = 96,
|
|
158
|
+
max = 127,
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export enum AVLinearPCMBitDepthKeyIOSType {
|
|
162
|
+
'bit8' = 8,
|
|
163
|
+
'bit16' = 16,
|
|
164
|
+
'bit24' = 24,
|
|
165
|
+
'bit32' = 32,
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export interface AudioSet {
|
|
169
|
+
AVSampleRateKeyIOS?: number;
|
|
170
|
+
AVFormatIDKeyIOS?: AVEncodingType;
|
|
171
|
+
AVModeIOS?: AVModeIOSType;
|
|
172
|
+
AVNumberOfChannelsKeyIOS?: number;
|
|
173
|
+
AVEncoderAudioQualityKeyIOS?: AVEncoderAudioQualityIOSType;
|
|
174
|
+
AudioSourceAndroid?: AudioSourceAndroidType;
|
|
175
|
+
AVLinearPCMBitDepthKeyIOS?: AVLinearPCMBitDepthKeyIOSType;
|
|
176
|
+
AVLinearPCMIsBigEndianKeyIOS?: boolean;
|
|
177
|
+
AVLinearPCMIsFloatKeyIOS?: boolean;
|
|
178
|
+
AVLinearPCMIsNonInterleavedIOS?: boolean;
|
|
179
|
+
AVEncoderBitRateKeyIOS?: number;
|
|
180
|
+
OutputFormatAndroid?: OutputFormatAndroidType;
|
|
181
|
+
AudioEncoderAndroid?: AudioEncoderAndroidType;
|
|
182
|
+
AudioEncodingBitRateAndroid?: number;
|
|
183
|
+
AudioSamplingRateAndroid?: number;
|
|
184
|
+
AudioChannelsAndroid?: number;
|
|
185
|
+
AudioSourceHarmony?: AudioSourceHarmonyType;
|
|
186
|
+
AudioMimeHarmony?: AudioMimeHarmonyType;
|
|
187
|
+
AudioFileFormatHarmony?: AudioFormatHarmonyType;
|
|
188
|
+
AudioEncodingBitRateHarmony?: number;
|
|
189
|
+
AudioSamplingRateHarmony?: number;
|
|
190
|
+
AudioChannelsHarmony?: number;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export type RecordBackType = {
|
|
194
|
+
isRecording?: boolean;
|
|
195
|
+
currentPosition: number;
|
|
196
|
+
currentMetering?: number;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
export type PlayBackType = {
|
|
200
|
+
isMuted?: boolean;
|
|
201
|
+
currentPosition: number;
|
|
202
|
+
duration: number;
|
|
203
|
+
isFinished: boolean;
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
export enum harmonyEventName {
|
|
207
|
+
recordBack = 'rn-recordback',
|
|
208
|
+
playBack = 'rn-playback'
|
|
209
|
+
}
|