@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
package/README.md CHANGED
@@ -1,14 +1,899 @@
1
- # @react-native-ohos/react-native-audio-recorder-player
2
-
3
- This project is based on [react-native-audio-recorder-player](https://github.com/hyochan/react-native-audio-recorder-player)
4
-
5
- ## Documentation
6
-
7
- - [中文](https://gitee.com/react-native-oh-library/usage-docs/blob/master/zh-cn/react-native-audio-recorder-player.md)
8
-
9
- - [English](https://gitee.com/react-native-oh-library/usage-docs/blob/master/en/react-native-audio-recorder-player.md)
10
-
11
- ## License
12
-
13
- This library is licensed under [The MIT License (MIT)](https://github.com/hyochan/react-native-audio-recorder-player/blob/main/LICENSE)
14
-
1
+ > 文档模板:v0.4.2
2
+
3
+ <p align="center">
4
+ <h1 align="center"> <code>react-native-audio-recorder-player</code> </h1>
5
+ </p>
6
+
7
+ 本项目基于 [react-native-audio-recorder-player](https://github.com/hyochan/react-native-audio-recorder-player) 开发。
8
+
9
+ 该第三方库的仓库已迁移至 Gitcode,且支持直接从 npm 下载,新的包名为:`@react-native-ohos/react-native-audio-recorder-player` 版本所属关系如下:
10
+ | 三方库名称 | 三方库版本(npm地址) | 发布信息 | 支持RN版本 | Autolink | 编译API版本 | 社区基线版本 | 源码地址 |
11
+ | ------------ | ------------ | ------------------------------ | ------------- | ------------- |------------------------ | ------------- | ------------- |
12
+ | @react-native-ohos/react-native-audio-recorder-player | [~ 3.6.11](https://www.npmjs.com/package/@react-native-ohos/react-native-audio-recorder-player) | [Gitcode Releases](https://gitcode.com/CPF-RN/rntpc_react-native-audio-recorder-player/releases) | 0.72.* | 是 | API12+ | 3.6.10 | [br_rnoh0.72](https://gitcode.com/CPF-RN/rntpc_react-native-audio-recorder-player/tree/br_rnoh0.72) |
13
+ | @react-native-oh-tpl/react-native-audio-recorder-player | [<= 3.6.10-0.0.2@deprecated](https://www.npmjs.com/package/@react-native-oh-tpl/react-native-audio-recorder-player) | [Github Releases(deprecated)](https://github.com/react-native-oh-library/react-native-audio-recorder-player/releases) | 0.72.* | 否 | API12+ |3.6.10 | [sig](https://github.com/react-native-oh-library/react-native-audio-recorder-player/tree/sig) |
14
+
15
+
16
+ ## 简介
17
+
18
+ react-native-audio-recorder-player组件用于 React Native。是一个录音与播放组件,支持音频录制、音频播放等功能。
19
+
20
+ ## 下载安装
21
+
22
+ 进入到工程目录并输入以下命令:
23
+
24
+ **npm**
25
+
26
+ ```bash
27
+ npm install @react-native-ohos/react-native-audio-recorder-player
28
+ ```
29
+
30
+ **yarn**
31
+
32
+ ```bash
33
+ yarn add @react-native-ohos/react-native-audio-recorder-player
34
+ ```
35
+
36
+ ## Link
37
+
38
+ | | 是否支持autolink | RN框架版本 |
39
+ |--------------------------------------|-----------------|------------|
40
+ | ~3.6.11 | Yes | 0.72 |
41
+ | <= 3.6.10-0.0.2@deprecated | No | 0.72 |
42
+
43
+ 使用AutoLink的工程需要根据该文档配置,Autolink框架指导文档:https://gitcode.com/openharmony-sig/ohos_react_native/blob/master/docs/zh-cn/Autolinking.md
44
+
45
+ 如您使用的版本支持 Autolink,并且工程已接入 Autolink,可跳过ManualLink配置。
46
+ <details>
47
+ <summary>ManualLink: 此步骤为手动配置原生依赖项的指导</summary>
48
+
49
+ 首先需要使用 DevEco Studio 打开项目里的 HarmonyOS 工程 `harmony`。
50
+
51
+ ### 1. Overrides RN SDK
52
+
53
+ 为了让工程依赖同一个版本的 RN SDK,需要在工程根目录的 `oh-package.json5` 添加 overrides 字段,指向工程需要使用的 RN SDK 版本。替换的版本既可以是一个具体的版本号,也可以是一个模糊版本,还可以是本地存在的 HAR 包或源码目录。
54
+
55
+ 关于该字段的作用请阅读[官方说明](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/ide-oh-package-json5-V5#zh-cn_topic_0000001792256137_overrides)
56
+
57
+ ```json
58
+ {
59
+ "overrides": {
60
+ "@rnoh/react-native-openharmony": "^0.72.38" // ohpm 在线版本
61
+ // "@rnoh/react-native-openharmony" : "./react_native_openharmony.har" // 指向本地 har 包的路径
62
+ // "@rnoh/react-native-openharmony" : "./react_native_openharmony" // 指向源码路径
63
+ }
64
+ }
65
+ ```
66
+
67
+ ### 2. 引入原生端代码
68
+
69
+ 目前有两种方法:
70
+
71
+ 1. 通过 har 包引入;
72
+ 2. 直接链接源码。
73
+
74
+ 方法一:通过 har 包引入(推荐)
75
+
76
+ > [!TIP] har 包位于三方库安装路径的 `harmony` 文件夹下。
77
+
78
+ 打开 `entry/oh-package.json5`,添加以下依赖
79
+
80
+ ```json
81
+ "dependencies": {
82
+ "@react-native-ohos/react-native-audio-recorder-player": "file:../../node_modules/@react-native-ohos/react-native-audio-recorder-player/harmony/audio_recorder_player.har"
83
+ }
84
+ ```
85
+ 点击右上角的 `sync` 按钮
86
+
87
+ 或者在命令行终端执行:
88
+
89
+ ```bash
90
+ cd entry
91
+ ohpm install
92
+ ```
93
+
94
+ 方法二:直接链接源码
95
+
96
+ > [!TIP] 如需使用直接链接源码,请参考[直接链接源码说明](https://gitcode.com/CPF-RN/usage-docs/blob/master/zh-cn/link-source-code.md)
97
+
98
+ ### 3. 配置 CMakeLists 和引入 RNAudioRecorderPlayerPackage
99
+
100
+ > 若使用的是 <= 3.6.10-0.0.2 版本,请跳过本章。
101
+
102
+ 打开 `entry/src/main/cpp/CMakeLists.txt`,添加:
103
+
104
+ ```diff
105
+ project(rnapp)
106
+ cmake_minimum_required(VERSION 3.4.1)
107
+ set(CMAKE_SKIP_BUILD_RPATH TRUE)
108
+ set(RNOH_APP_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
109
+ set(NODE_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../node_modules")
110
+ + set(OH_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules")
111
+ set(RNOH_CPP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../react-native-harmony/harmony/cpp")
112
+ set(LOG_VERBOSITY_LEVEL 1)
113
+ set(CMAKE_ASM_FLAGS "-Wno-error=unused-command-line-argument -Qunused-arguments")
114
+ set(CMAKE_CXX_FLAGS "-fstack-protector-strong -Wl,-z,relro,-z,now,-z,noexecstack -s -fPIE -pie")
115
+ set(WITH_HITRACE_SYSTRACE 1) # for other CMakeLists.txt files to use
116
+ add_compile_definitions(WITH_HITRACE_SYSTRACE)
117
+
118
+ add_subdirectory("${RNOH_CPP_DIR}" ./rn)
119
+
120
+ # RNOH_BEGIN: manual_package_linking_1
121
+ + add_subdirectory("${OH_MODULES}/@react-native-ohos/react-native-audio-recorder-player/src/main/cpp" ./audio_recorder_player)
122
+ # RNOH_END: manual_package_linking_1
123
+
124
+ file(GLOB GENERATED_CPP_FILES "./generated/*.cpp")
125
+
126
+ add_library(rnoh_app SHARED
127
+ ${GENERATED_CPP_FILES}
128
+ "./PackageProvider.cpp"
129
+ "${RNOH_CPP_DIR}/RNOHAppNapiBridge.cpp"
130
+ )
131
+ target_link_libraries(rnoh_app PUBLIC rnoh)
132
+
133
+ # RNOH_BEGIN: manual_package_linking_2
134
+ + target_link_libraries(rnoh_app PUBLIC rnoh_audio_recorder_player)
135
+ # RNOH_END: manual_package_linking_2
136
+ ```
137
+
138
+ 打开 `entry/src/main/cpp/PackageProvider.cpp`,添加:
139
+
140
+ ```diff
141
+ #include "RNOH/PackageProvider.h"
142
+ #include "SamplePackage.h"
143
+ + #include "RNAudioRecorderPlayerPackage.h"
144
+
145
+ using namespace rnoh;
146
+
147
+ std::vector<std::shared_ptr<Package>> PackageProvider::getPackages(Package::Context ctx) {
148
+ return {
149
+ std::make_shared<SamplePackage>(ctx),
150
+ + std::make_shared<RNAudioRecorderPlayerPackage>(ctx)
151
+ };
152
+ }
153
+ ```
154
+
155
+ ### 4. 在 ArkTs 侧引入 RNAudioRecorderPlayerPackage
156
+
157
+ 打开 `entry/src/main/ets/RNPackagesFactory.ts`,添加:
158
+
159
+ ```diff
160
+ + import {RNAudioRecorderPlayerPackage} from '@react-native-ohos/react-native-audio-recorder-player/ts';
161
+
162
+ export function createRNPackages(ctx: RNPackageContext): RNPackage[] {
163
+ return [
164
+ + new RNAudioRecorderPlayerPackage(ctx)
165
+ ];
166
+ }
167
+ ```
168
+
169
+ </details>
170
+
171
+ ### 运行
172
+
173
+ 点击右上角的 `sync` 按钮
174
+
175
+ 或者在命令行终端执行:
176
+
177
+ ```bash
178
+ cd entry
179
+ ohpm install
180
+ ```
181
+ 然后编译、运行即可。
182
+
183
+ ## 约束与限制
184
+
185
+ ### 兼容性
186
+
187
+ 本文档内容基于以下版本验证通过:
188
+
189
+ 1. RNOH: 0.72.96; SDK: HarmonyOS 6.0.0 Release SDK; IDE: DevEco Studio 6.0.0.858; ROM: 6.0.0.112;
190
+ 2. RNOH: 0.72.33; SDK: HarmonyOS NEXT B1; IDE: DevEco Studio: 5.0.3.900; ROM: Next.0.0.71;
191
+
192
+ ### 权限要求
193
+
194
+ #### 在 entry 目录下的module.json5中添加权限
195
+
196
+ 在`entry/src/main/module.json5`,添加:
197
+
198
+ ```json
199
+ "requestPermissions": [
200
+ {"name": "ohos.permission.INTERNET"},//网络播放音频需添加
201
+ {
202
+ "name": "ohos.permission.MICROPHONE",
203
+ "reason": "$string:Access_Create_Audio",
204
+ "usedScene": {
205
+ "abilities": [
206
+ "EntryAbility"
207
+ ],
208
+ "when": "always"
209
+ }
210
+ },
211
+ ]
212
+ ```
213
+ #### 在 entry 目录下添加申请权限的原因
214
+
215
+ 打开 `entry/src/main/resources/base/element/string.json`,添加:
216
+
217
+ ```json
218
+ {
219
+ "string": [
220
+ {
221
+ "name": "Access_Create_Audio",
222
+ "value": "access create Audio"
223
+ }
224
+ ]
225
+ }
226
+ ```
227
+
228
+ ## 使用示例
229
+
230
+ 下面的代码展示了这个库的基本使用场景:
231
+
232
+ > [!WARNING] 使用时 import 的库名不变。
233
+
234
+ ```javascript
235
+ import AudioRecorderPlayer, {
236
+ AVEncoderAudioQualityIOSType,
237
+ AVEncodingOption,
238
+ AudioEncoderAndroidType,
239
+ AudioSourceAndroidType,
240
+ OutputFormatAndroidType,
241
+ AudioMimeHarmonyType,
242
+ AudioFormatHarmonyType,
243
+ AudioSourceHarmonyType,
244
+ AudioSet
245
+ } from 'react-native-audio-recorder-player'; import type {
246
+ PlayBackType,
247
+ RecordBackType,
248
+ } from 'react-native-audio-recorder-player';
249
+
250
+ import {
251
+ Dimensions,
252
+ Platform,
253
+ ActivityIndicator,
254
+ Image,
255
+ SafeAreaView,
256
+ StyleSheet,
257
+ Text,
258
+ TouchableOpacity,
259
+ View,
260
+ } from 'react-native';
261
+ import React, { Component } from 'react';
262
+
263
+ import type { ReactElement, ReactNode } from 'react';
264
+
265
+ const stylesButton: any = StyleSheet.create({
266
+ btn: {
267
+ backgroundColor: 'transparent',
268
+ alignSelf: 'center',
269
+ borderRadius: 4,
270
+ borderWidth: 2,
271
+ width: 320,
272
+ height: 52,
273
+ borderColor: 'white',
274
+
275
+ alignItems: 'center',
276
+ justifyContent: 'center',
277
+ },
278
+ btnDisabled: {
279
+ backgroundColor: 'rgb(243,243,243)',
280
+ alignSelf: 'center',
281
+ borderRadius: 4,
282
+ borderWidth: 2,
283
+ width: 320,
284
+ height: 52,
285
+ borderColor: '#333',
286
+
287
+ alignItems: 'center',
288
+ justifyContent: 'center',
289
+ },
290
+ txt: {
291
+ fontSize: 14,
292
+ color: 'white',
293
+ },
294
+ imgLeft: {
295
+ width: 24,
296
+ height: 24,
297
+ position: 'absolute',
298
+ left: 16,
299
+ },
300
+ });
301
+
302
+ interface ItemProps {
303
+ children?: ReactNode;
304
+ isLoading?: boolean;
305
+ isDisabled?: boolean;
306
+ onPress?: () => void;
307
+ style?: any;
308
+ disabledStyle?: any;
309
+ textStyle?: any;
310
+ imgLeftSrc?: any;
311
+ imgLeftStyle?: any;
312
+ indicatorColor?: string;
313
+ activeOpacity?: number;
314
+ }
315
+
316
+ class Button extends Component<ItemProps, any> {
317
+ private static defaultProps: Partial<ItemProps> = {
318
+ isLoading: false,
319
+ isDisabled: false,
320
+ style: stylesButton.btn,
321
+ textStyle: stylesButton.txt,
322
+ imgLeftStyle: stylesButton.imgLeft,
323
+ indicatorColor: 'white',
324
+ activeOpacity: 0.5,
325
+ };
326
+
327
+ constructor(props: ItemProps) {
328
+ super(props);
329
+ this.state = {};
330
+ }
331
+
332
+ public render(): ReactElement {
333
+ if (this.props.isDisabled) {
334
+ return (
335
+ <View style={this.props.disabledStyle}>
336
+ <Text style={this.props.textStyle}>{this.props.children}</Text>
337
+ </View>
338
+ );
339
+ }
340
+
341
+ if (this.props.isLoading) {
342
+ return (
343
+ <View style={this.props.style}>
344
+ <ActivityIndicator size="small" color={this.props.indicatorColor} />
345
+ </View>
346
+ );
347
+ }
348
+
349
+ return (
350
+ <TouchableOpacity
351
+ activeOpacity={this.props.activeOpacity}
352
+ onPress={this.props.onPress}>
353
+ <View style={this.props.style}>
354
+ {this.props.imgLeftSrc ? (
355
+ <Image
356
+ style={this.props.imgLeftStyle}
357
+ source={this.props.imgLeftSrc}
358
+ />
359
+ ) : null}
360
+ <Text style={this.props.textStyle}>{this.props.children}</Text>
361
+ </View>
362
+ </TouchableOpacity>
363
+ );
364
+ }
365
+ }
366
+
367
+ const styles: any = StyleSheet.create({
368
+ container: {
369
+ flex: 1,
370
+ backgroundColor: '#455A64',
371
+ flexDirection: 'column',
372
+ alignItems: 'center',
373
+ height: Dimensions.get('screen').height
374
+ },
375
+ titleTxt: {
376
+ marginTop: 100,
377
+ color: 'white',
378
+ fontSize: 28,
379
+ },
380
+ viewRecorder: {
381
+ marginTop: 40,
382
+ width: '100%',
383
+ alignItems: 'center',
384
+ },
385
+ recordBtnWrapper: {
386
+ flexDirection: 'row',
387
+ },
388
+ viewPlayer: {
389
+ marginTop: 60,
390
+ alignSelf: 'stretch',
391
+ alignItems: 'center',
392
+ },
393
+ viewBarWrapper: {
394
+ marginTop: 28,
395
+ marginHorizontal: 28,
396
+ alignSelf: 'stretch',
397
+ },
398
+ viewBar: {
399
+ backgroundColor: '#ccc',
400
+ height: 4,
401
+ alignSelf: 'stretch',
402
+ },
403
+ viewBarPlay: {
404
+ backgroundColor: 'white',
405
+ height: 4,
406
+ width: 0,
407
+ },
408
+ playStatusTxt: {
409
+ marginTop: 8,
410
+ color: '#ccc',
411
+ },
412
+ playBtnWrapper: {
413
+ flexDirection: 'row',
414
+ marginTop: 40,
415
+ },
416
+ btn: {
417
+ borderColor: 'white',
418
+ borderWidth: 1,
419
+ },
420
+ txt: {
421
+ color: 'white',
422
+ fontSize: 14,
423
+ marginHorizontal: 8,
424
+ marginVertical: 4,
425
+ },
426
+ txtRecordCounter: {
427
+ marginTop: 32,
428
+ color: 'white',
429
+ fontSize: 20,
430
+ textAlignVertical: 'center',
431
+ fontWeight: '200',
432
+ fontFamily: 'Helvetica Neue',
433
+ letterSpacing: 3,
434
+ width: 300
435
+ },
436
+ txtCounter: {
437
+ marginTop: 12,
438
+ color: 'white',
439
+ fontSize: 20,
440
+ textAlignVertical: 'center',
441
+ fontWeight: '200',
442
+ fontFamily: 'Helvetica Neue',
443
+ letterSpacing: 3,
444
+ width: 300
445
+ },
446
+ });
447
+
448
+ interface State {
449
+ isLoggingIn: boolean;
450
+ recordSecs: number;
451
+ recordTime: string;
452
+ currentPositionSec: number;
453
+ currentDurationSec: number;
454
+ playTime: string;
455
+ duration: string;
456
+ }
457
+
458
+ const screenWidth = Dimensions.get('screen').width;
459
+
460
+ class Page extends Component<any, State> {
461
+ private audioRecorderPlayer
462
+
463
+ constructor(props: any) {
464
+ super(props);
465
+ this.state = {
466
+ isLoggingIn: false,
467
+ recordSecs: 0,
468
+ recordTime: '00:00:00',
469
+ currentPositionSec: 0,
470
+ currentDurationSec: 0,
471
+ playTime: '00:00:00',
472
+ duration: '00:00:00',
473
+ };
474
+
475
+ this.audioRecorderPlayer = new AudioRecorderPlayer()
476
+ this.audioRecorderPlayer.setSubscriptionDuration(0.5); // optional. Default is 0.5
477
+ }
478
+
479
+ public render(): ReactElement {
480
+ let playWidth =
481
+ (this.state.currentPositionSec / this.state.currentDurationSec) *
482
+ (screenWidth - 56);
483
+
484
+ if (!playWidth) {
485
+ playWidth = 0;
486
+ }
487
+
488
+ return (
489
+ <SafeAreaView style={styles.container}>
490
+ <Text style={styles.titleTxt}>Audio Recorder Player</Text>
491
+ <Text style={styles.txtRecordCounter}>{this.state.recordTime}</Text>
492
+ <View style={styles.viewRecorder}>
493
+ <View style={styles.recordBtnWrapper}>
494
+ <Button
495
+ style={styles.btn}
496
+ onPress={this.onStartRecord}
497
+ textStyle={styles.txt}>
498
+ startRecord
499
+ </Button>
500
+ <Button
501
+ style={[
502
+ styles.btn,
503
+ {
504
+ marginLeft: 12,
505
+ },
506
+ ]}
507
+ onPress={this.onPauseRecord}
508
+ textStyle={styles.txt}>
509
+ Pause
510
+ </Button>
511
+ <Button
512
+ style={[
513
+ styles.btn,
514
+ {
515
+ marginLeft: 12,
516
+ },
517
+ ]}
518
+ onPress={this.onResumeRecord}
519
+ textStyle={styles.txt}>
520
+ Resume
521
+ </Button>
522
+ <Button
523
+ style={[styles.btn, { marginLeft: 12 }]}
524
+ onPress={this.onStopRecord}
525
+ textStyle={styles.txt}>
526
+ Stop
527
+ </Button>
528
+ </View>
529
+ </View>
530
+ <View style={styles.viewPlayer}>
531
+ <TouchableOpacity
532
+ style={styles.viewBarWrapper}
533
+ onPress={this.onStatusPress}>
534
+ <View style={styles.viewBar}>
535
+ <View style={[styles.viewBarPlay, { width: playWidth }]} />
536
+ </View>
537
+ </TouchableOpacity>
538
+ <Text style={styles.txtCounter}>
539
+ {this.state.playTime} / {this.state.duration}
540
+ </Text>
541
+ <View style={styles.playBtnWrapper}>
542
+ <Button
543
+ style={styles.btn}
544
+ onPress={this.onStartPlay}
545
+ textStyle={styles.txt}>
546
+ Play
547
+ </Button>
548
+ <Button
549
+ style={[
550
+ styles.btn,
551
+ {
552
+ marginLeft: 12,
553
+ },
554
+ ]}
555
+ onPress={this.onPausePlay}
556
+ textStyle={styles.txt}>
557
+ Pause
558
+ </Button>
559
+ <Button
560
+ style={[
561
+ styles.btn,
562
+ {
563
+ marginLeft: 12,
564
+ },
565
+ ]}
566
+ onPress={this.onResumePlay}
567
+ textStyle={styles.txt}>
568
+ Resume
569
+ </Button>
570
+ <Button
571
+ style={[
572
+ styles.btn,
573
+ {
574
+ marginLeft: 12,
575
+ },
576
+ ]}
577
+ onPress={this.onStopPlay}
578
+ textStyle={styles.txt}>
579
+ Stop
580
+ </Button>
581
+ </View>
582
+ </View>
583
+ </SafeAreaView>
584
+ );
585
+ }
586
+
587
+ private onStatusPress = (e: any): void => {
588
+ const touchX = e.nativeEvent.locationX;
589
+ console.log(`touchX: ${touchX}`);
590
+
591
+ const playWidth =
592
+ (this.state.currentPositionSec / this.state.currentDurationSec) *
593
+ (screenWidth - 56);
594
+ console.log(`currentPlayWidth: ${playWidth}`);
595
+
596
+ const currentPosition = Math.round(this.state.currentPositionSec);
597
+
598
+ if (playWidth && playWidth < touchX) {
599
+ const addSecs = Math.round(currentPosition + 1000);
600
+ this.audioRecorderPlayer.seekToPlayer(addSecs);
601
+ console.log(`addSecs: ${addSecs}`);
602
+ } else {
603
+ const subSecs = Math.round(currentPosition - 1000);
604
+ this.audioRecorderPlayer.seekToPlayer(subSecs);
605
+ console.log(`subSecs: ${subSecs}`);
606
+ }
607
+ };
608
+
609
+ private onStartRecord = async (): Promise<void> => {
610
+
611
+ const audioSet: AudioSet = {
612
+ AudioEncoderAndroid: AudioEncoderAndroidType.AAC,
613
+ AudioSourceAndroid: AudioSourceAndroidType.MIC,
614
+ AVEncoderAudioQualityKeyIOS: AVEncoderAudioQualityIOSType.high,
615
+ AVNumberOfChannelsKeyIOS: 2,
616
+ AVFormatIDKeyIOS: AVEncodingOption.aac,
617
+ OutputFormatAndroid: OutputFormatAndroidType.AAC_ADTS,
618
+ AudioSourceHarmony: AudioSourceHarmonyType.MIC,
619
+ AudioMimeHarmony: AudioMimeHarmonyType.AUDIO_AAC,
620
+ AudioFileFormatHarmony: AudioFormatHarmonyType.MPEG_4A,
621
+ AudioEncodingBitRateHarmony: 128000,
622
+ AudioSamplingRateHarmony: 44100,
623
+ AudioChannelsHarmony: 2,
624
+ };
625
+
626
+ console.log('audioSet', audioSet);
627
+
628
+ const uri = await this.audioRecorderPlayer.startRecorder(
629
+ 'audio.m4a',
630
+ audioSet,
631
+ );
632
+
633
+ this.audioRecorderPlayer.addRecordBackListener((e: RecordBackType) => {
634
+ console.log('record-back', e);
635
+ this.setState({
636
+ recordSecs: e.currentPosition,
637
+ recordTime: this.audioRecorderPlayer.mmssss(
638
+ Math.floor(e.currentPosition),
639
+ ),
640
+ });
641
+ });
642
+
643
+ console.log(`uri: ${uri}`);
644
+ };
645
+
646
+ private onPauseRecord = async (): Promise<void> => {
647
+ try {
648
+ const r = await this.audioRecorderPlayer.pauseRecorder();
649
+ console.log(r);
650
+ } catch (err) {
651
+ console.log('pauseRecord', err);
652
+ }
653
+ };
654
+
655
+ private onResumeRecord = async (): Promise<void> => {
656
+ await this.audioRecorderPlayer.resumeRecorder();
657
+ };
658
+
659
+ private onStopRecord = async (): Promise<void> => {
660
+ const result = await this.audioRecorderPlayer.stopRecorder();
661
+ this.audioRecorderPlayer.removeRecordBackListener();
662
+ this.setState({
663
+ recordSecs: 0,
664
+ });
665
+ console.log(result, '>>>>>>>stopRecorder');
666
+ };
667
+
668
+ private onStartPlay = async (): Promise<void> => {
669
+ console.log('onStartPlay');
670
+
671
+ try {
672
+ const msg = await this.audioRecorderPlayer.startPlayer('https://sis-sample-audio.obs.cn-north-1.myhuaweicloud.com/16k16bit.mp3', { 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/21.0' });
673
+ const volume = await this.audioRecorderPlayer.setVolume(1);
674
+ this.audioRecorderPlayer.addPlayBackListener((e: PlayBackType) => {
675
+ console.log('playBackListener', e);
676
+ this.setState({
677
+ currentPositionSec: e.currentPosition,
678
+ currentDurationSec: e.duration,
679
+ playTime: this.audioRecorderPlayer.mmssss(e.currentPosition),
680
+ duration: this.audioRecorderPlayer.mmssss(Math.floor(e.duration)),
681
+ });
682
+ });
683
+
684
+ } catch (err) {
685
+ console.log('startPlayer error', err);
686
+ }
687
+ };
688
+
689
+ private onPausePlay = async (): Promise<void> => {
690
+ await this.audioRecorderPlayer.pausePlayer();
691
+ };
692
+
693
+ private onResumePlay = async (): Promise<void> => {
694
+ await this.audioRecorderPlayer.resumePlayer();
695
+ };
696
+
697
+ private onStopPlay = async (): Promise<void> => {
698
+ console.log('onStopPlay');
699
+ this.audioRecorderPlayer.stopPlayer();
700
+ this.audioRecorderPlayer.removePlayBackListener();
701
+ };
702
+ }
703
+
704
+ export default Page;
705
+ ```
706
+
707
+ ## 使用说明
708
+
709
+ **事件监听**
710
+ ```javascript
711
+ this.audioRecorderPlayer = new AudioRecorderPlayer()
712
+ // 添加播放回调
713
+ this.audioRecorderPlayer.addPlayBackListener((e: PlayBackType) => {
714
+ // 播放中触发
715
+ });
716
+ // 移除播放回调
717
+ this.audioRecorderPlayer.removePlayBackListener();
718
+ // 添加录制回调
719
+ this.audioRecorderPlayer.addRecordBackListener((e: RecordBackType) => {
720
+ // 录制中触发
721
+ });
722
+ // 移除录制回调
723
+ this.audioRecorderPlayer.removeRecordBackListener();
724
+ ```
725
+
726
+ **录制控制**
727
+ ```javascript
728
+ this.audioRecorderPlayer = new AudioRecorderPlayer()
729
+ // 开始录制
730
+ this.audioRecorderPlayer.startRecorder(
731
+ 'audio.m4a',
732
+ audioSet,
733
+ );
734
+ // 暂停录制
735
+ this.audioRecorderPlayer.pauseRecorder();
736
+ // 恢复录制
737
+ this.audioRecorderPlayer.resumeRecorder();
738
+ // 停止录制
739
+ this.audioRecorderPlayer.stopRecorder();
740
+ ```
741
+
742
+ **播放控制**
743
+ ```javascript
744
+ this.audioRecorderPlayer = new AudioRecorderPlayer()
745
+ // 设置订阅间隔时长
746
+ this.audioRecorderPlayer.setSubscriptionDuration(0.5);
747
+ // 定位到XX播放位置
748
+ this.audioRecorderPlayer.seekToPlayer(addSecs);
749
+ // 暂停播放
750
+ this.audioRecorderPlayer.pausePlayer();
751
+ // 恢复播放
752
+ this.audioRecorderPlayer.resumePlayer();
753
+ // 停止播放
754
+ this.audioRecorderPlayer.stopPlayer();
755
+ // 设置音量
756
+ this.audioRecorderPlayer.setVolume(1);
757
+ // 将毫秒转换为“分钟:秒:毫秒”格式的字符串
758
+ this.audioRecorderPlayer.mmssss(e.currentPosition),
759
+ // 将毫秒转换为“分:秒”格式的字符串
760
+ this.audioRecorderPlayer.mmss(
761
+ Math.floor(e.currentPosition),
762
+ ),
763
+ ```
764
+
765
+ ## 接口说明
766
+
767
+ > [!TIP] "Platform"列表示该属性在原三方库上支持的平台。
768
+
769
+ > [!TIP] "HarmonyOS Support"列为 yes 表示 OpenHarmony 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。
770
+
771
+ | Name | Description | Type | Required | Platform | HarmonyOS Support |
772
+ | ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- | -------- | -------- | ----------------- |
773
+ | `mmss` | 将秒转换为“分:秒”格式的字符串 | function(seconds:number): string | No | Android,iOS | yes |
774
+ | `mmssss` | 将毫秒转换为“分钟:秒:毫秒”格式的字符串 | function(milisecs:number): string | No | Android,iOS | yes |
775
+ | `setSubscriptionDuration` | 设置录音器或播放器启动时的默认回调时间间隔。默认为 0.5,即 500 毫秒。| function(sec:number):Promise<string> | No | Android,iOS | yes |
776
+ | `addRecordBackListener` |从原生模块获取回调。将接收 currentPosition、currentMetering(如果在 startRecorder 中进行了配置)。 | function(callBack:(value:RecordBackType)=>void): void | No | Android,iOS | yes |
777
+ | `removeRecordBackListener` |移除 recordBack 监听器。 | function(): void | No | Android,iOS | yes |
778
+ | `addPlayBackListener` | 从原生模块获取回调,将收到 duration 和 currentPosition。 | function(callBack:(value: PlayBackType)=>void): void | No | Android,iOS | yes |
779
+ | `removePlayBackListener` | 移除 playback 监听器。 | function(): void | No | Android,iOS | yes |
780
+ | `startRecorder` |开始录音。不传递 uri 参数时,音频将保存在默认位置。 | function(uri?:string,audioSets?: AudioSet,meteringEnabled?: boolean): Promise< string > | No | Android,iOS | yes |
781
+ | `pauseRecorder` | 暂停录音。 | function() : Promise< string > | No | Android,iOS | yes |
782
+ | `resumeRecorder` | 恢复录音。 | function() : Promise< string > | No | Android,iOS | yes |
783
+ | `stopRecorder` | 停止录音。 | function() : Promise< string > | No | Android,iOS | yes |
784
+ | `startPlayer` | 开始播放。不传递参数时,将播放默认位置的音频。 | function(uri?:string,httpHeaders?:Record<string,string>) : Promise< string > | No | Android,iOS | yes |
785
+ | `stopPlayer` | 停止播放。 | function() : Promise< string > | No | Android,iOS | yes |
786
+ | `pausePlayer` | 暂停播放。 | function() : Promise< string > | No | Android,iOS | yes |
787
+ | `resumePlayer` | 恢复播放。 | function() : Promise< string > | No | Android,iOS | yes |
788
+ | `seekToPlayer` | 音频跳转。 | function(time:number) : Promise< string > | No | Android,iOS | yes |
789
+ | `setVolume` | 设置音频播放器的音量(默认值为 1.0,取值范围:0.0 ~ 1.0)。 | function(volume:float) : Promise< string > | No | Android,iOS | yes |
790
+
791
+ ## 鸿蒙平台 AudioSet 属性说明
792
+
793
+ 在 `startRecorder` 方法中使用的 `AudioSet` 对象包含多个平台的配置属性,其中以 `Harmony` 后缀结尾的属性为鸿蒙平台专属配置。以下对鸿蒙平台相关的枚举类型和属性进行详细说明。
794
+
795
+ ### AudioSourceHarmonyType(音频输入源类型)
796
+
797
+ 用于设置录音时的音频输入源,通过 `AudioSet.AudioSourceHarmony` 属性配置。
798
+
799
+ | 枚举值 | 对应数值 | 说明 |
800
+ | --------------------- | -------- | ----------------------------------- |
801
+ | `DEFAULT` | 0 | 默认音频源 |
802
+ | `MIC` | 1 | 麦克风输入 |
803
+ | `VOICE_RECOGNITION` | 2 | 语音识别,适用于语音转文字等场景 |
804
+ | `VOICE_COMMUNICATION` | 7 | 语音通信,适用于 VoIP 等实时通话场景 |
805
+ | `VOICE_MESSAGE` | 10 | 语音消息,适用于录制语音短消息 |
806
+ | `CAMCORDER` | 12 | 摄像头录音,与视频同步录制音频 |
807
+
808
+ ### AudioMimeHarmonyType(音频 MIME 类型)
809
+
810
+ 用于设置录音的音频编码格式 MIME 类型,通过 `AudioSet.AudioMimeHarmony` 属性配置。
811
+
812
+ | 枚举值 | 对应值 | 说明 |
813
+ | --------------- | ----------------- | ----------------------------- |
814
+ | `AUDIO_AAC` | `audio/mp4a-latm` | AAC 编码格式(音频/mp4a-latm) |
815
+ | `AUDIO_VORBIS` | `audio/vorbis` | Vorbis 编码格式 |
816
+ | `AUDIO_FLAC` | `audio/flac` | FLAC 无损编码格式 |
817
+ | `AUDIO_MP3` | `audio/mpeg` | MP3 编码格式 |
818
+ | `AUDIO_G711MU` | `audio/g711mu` | G711-mulaw 编码格式 |
819
+
820
+ ### AudioFormatHarmonyType(音频文件格式类型)
821
+
822
+ 用于设置录音输出的音频文件格式(后缀名),通过 `AudioSet.AudioFileFormatHarmony` 属性配置。
823
+
824
+ | 枚举值 | 对应值 | 说明 |
825
+ | --------- | ------ | ----------------------------- |
826
+ | `MPEG_4` | `mp4` | MPEG-4 容器格式(.mp4 文件) |
827
+ | `MPEG_4A` | `m4a` | MPEG-4 音频格式(.m4a 文件) |
828
+ | `MP3` | `mp3` | MP3 格式(.mp3 文件) |
829
+ | `WAV` | `wav` | WAV 格式(.wav 文件) |
830
+ | `AAC` | `aac` | AAC 格式(.aac 文件) |
831
+
832
+ ### AudioSet 鸿蒙专属属性说明
833
+
834
+ | 属性名 | 类型 | 说明 |
835
+ | ----------------------------- | ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
836
+ | `AudioSourceHarmony` | `AudioSourceHarmonyType` | 音频输入源类型。用于指定录音时的音频来源,默认为 `MIC`(麦克风)。 |
837
+ | `AudioMimeHarmony` | `AudioMimeHarmonyType` | 音频编码格式(MIME 类型)。用于指定录音的音频编码方式,如 AAC、MP3、FLAC 等。需与对应的采样率、比特率、声道数范围匹配。 |
838
+ | `AudioFileFormatHarmony` | `AudioFormatHarmonyType` | 音频文件输出格式。用于指定录音文件的容器格式及后缀名,如 `m4a`、`mp3`、`wav` 等。 |
839
+ | `AudioEncodingBitRateHarmony` | `number` | 音频编码比特率(单位:bps)。选择音频录制时必填。支持范围:<br>- AAC 编码格式支持比特率范围 `[32000 - 500000]`<br>- G711-mulaw 编码格式支持比特率范围 `[64000 - 64000]`<br>- MP3 编码格式支持 `[8000, 16000, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000]` |
840
+ | `AudioSamplingRateHarmony` | `number` | 音频采样率(单位:Hz)。支持范围:<br>- AAC 编码支持采样率范围 `[8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000]`<br>- G711-mulaw 编码支持采样率范围 `[8000 - 8000]`<br>- MP3 编码支持采样率范围 `[8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000]` |
841
+ | `AudioChannelsHarmony` | `number` | 音频采集声道数。支持范围:<br>- AAC 编码格式支持范围 `[1 - 8]`<br>- G711-mulaw 编码格式支持范围 `[1 - 1]`<br>- MP3 编码格式支持范围 `[1 - 2]` |
842
+
843
+ > [!TIP] 在使用 MP3 编码格式时,采样率和比特率存在映射关系:采样率 16K 以下时对应比特率范围为 `[8kbps - 64kbps]`;采样率 16K~32K 时对应比特率范围为 `[8kbps - 160kbps]`;采样率 32K 以上时对应比特率范围为 `[32kbps - 320kbps]`。
844
+
845
+ ### 使用示例
846
+
847
+ ```javascript
848
+ import {
849
+ AudioMimeHarmonyType,
850
+ AudioFormatHarmonyType,
851
+ AudioSourceHarmonyType,
852
+ AudioSet
853
+ } from 'react-native-audio-recorder-player';
854
+
855
+ const audioSet: AudioSet = {
856
+ // 鸿蒙平台录音配置
857
+ AudioSourceHarmony: AudioSourceHarmonyType.MIC, // 使用麦克风作为音频输入源
858
+ AudioMimeHarmony: AudioMimeHarmonyType.AUDIO_AAC, // 使用 AAC 编码格式
859
+ AudioFileFormatHarmony: AudioFormatHarmonyType.MPEG_4A, // 输出为 m4a 格式文件
860
+ AudioEncodingBitRateHarmony: 128000, // 设置编码比特率为 128000 bps
861
+ AudioSamplingRateHarmony: 44100, // 设置采样率为 44100 Hz
862
+ AudioChannelsHarmony: 2, // 设置双声道录制
863
+ };
864
+
865
+ const uri = await this.audioRecorderPlayer.startRecorder('audio.m4a', audioSet);
866
+ ```
867
+
868
+ ## 遗留问题
869
+ 无
870
+
871
+ ## 其他
872
+ 无
873
+
874
+ ## 目录结构
875
+ ````
876
+ /rntpc_react-native-audio-recorder-player # 项目根目录
877
+ ├── harmony # 鸿蒙适配代码
878
+ │ ├─ audio_recorder_player.har # har包
879
+ │ └─ audio_recorder_player # 鸿蒙适配核心代码
880
+ │ └─ index.ets # 鸿蒙适配代码入口
881
+ │ └─ src/main/ets
882
+ │ ├─ audioPlayer
883
+ │ └─ AudioPlayer.ts # 音频播放文件
884
+ │ └─ recorder
885
+ │ └─ Recorder.ts # 音频录制文件
886
+ ├── src # RN代码
887
+ │ └─ NativeAudioRecorderPlayerModule.ts # TurboModule文件
888
+ ├── index.ts # 入口文件
889
+ ├── README_en.md # 英文安装使用方法
890
+ ├── README.md # 中文安装使用方法
891
+ ````
892
+
893
+ ## 贡献代码
894
+
895
+ 使用过程中发现任何问题都可以提交 [Issue](https://gitcode.com/CPF-RN/rntpc_react-native-audio-recorder-player/issues),当然,也非常欢迎提交 [PR](https://gitcode.com/CPF-RN/rntpc_react-native-audio-recorder-player/pulls) 。
896
+
897
+ ## 开源协议
898
+
899
+ 本项目基于 [The MIT License (MIT)](https://github.com/hyochan/react-native-audio-recorder-player/blob/main/LICENSE) ,请自由地享受和参与开源。