@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
package/README.md CHANGED
@@ -1,14 +1,822 @@
1
- # @react-native-ohos/react-native-audio-recorder-player
2
-
3
- This project is based on [react-native-audio-recorder-player@3.6.13](https://github.com/hyochan/react-native-audio-recorder-player/tree/3.6.13)
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.7.0(开发中)](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.77.* /0.82.* /0.84.* | partially(0.82/0.84) | API12+ | 3.6.13 | [master](https://gitcode.com/CPF-RN/rntpc_react-native-audio-recorder-player/tree/master) |
13
+ | @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) |
14
+ | @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) |
15
+
16
+
17
+ ## 简介
18
+
19
+ react-native-audio-recorder-player组件用于 React Native。是一个录音与播放组件,支持音频录制、音频播放等功能。
20
+
21
+ ## 下载安装
22
+
23
+ 进入到工程目录并输入以下命令:
24
+
25
+ **npm**
26
+
27
+ ```bash
28
+ npm install @react-native-ohos/react-native-audio-recorder-player
29
+ ```
30
+
31
+ **yarn**
32
+
33
+ ```bash
34
+ yarn add @react-native-ohos/react-native-audio-recorder-player
35
+ ```
36
+
37
+ ## Link
38
+
39
+ | | 是否支持autolink | RN框架版本 |
40
+ |--------------------------------------|-----------------|------------|
41
+ | ~3.7.0 | partially(0.82/0.84) | 0.77/0.82/0.84 |
42
+
43
+ 使用AutoLink的工程需要根据该文档配置,Autolink框架指导文档:https://gitcode.com/CPF-RN/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
+ 打开 `entry/src/main/cpp/CMakeLists.txt`,添加:
101
+
102
+ ```diff
103
+ project(rnapp)
104
+ cmake_minimum_required(VERSION 3.4.1)
105
+ set(CMAKE_SKIP_BUILD_RPATH TRUE)
106
+ set(RNOH_APP_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
107
+ set(NODE_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../node_modules")
108
+ + set(OH_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules")
109
+ set(RNOH_CPP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../react-native-harmony/harmony/cpp")
110
+ set(LOG_VERBOSITY_LEVEL 1)
111
+ set(CMAKE_ASM_FLAGS "-Wno-error=unused-command-line-argument -Qunused-arguments")
112
+ set(CMAKE_CXX_FLAGS "-fstack-protector-strong -Wl,-z,relro,-z,now,-z,noexecstack -s -fPIE -pie")
113
+ set(WITH_HITRACE_SYSTRACE 1) # for other CMakeLists.txt files to use
114
+ add_compile_definitions(WITH_HITRACE_SYSTRACE)
115
+
116
+ add_subdirectory("${RNOH_CPP_DIR}" ./rn)
117
+
118
+ # RNOH_BEGIN: manual_package_linking_1
119
+ + add_subdirectory("${OH_MODULES}/@react-native-ohos/react-native-audio-recorder-player/src/main/cpp" ./audio_recorder_player)
120
+ # RNOH_END: manual_package_linking_1
121
+
122
+ file(GLOB GENERATED_CPP_FILES "./generated/*.cpp")
123
+
124
+ add_library(rnoh_app SHARED
125
+ ${GENERATED_CPP_FILES}
126
+ "./PackageProvider.cpp"
127
+ "${RNOH_CPP_DIR}/RNOHAppNapiBridge.cpp"
128
+ )
129
+ target_link_libraries(rnoh_app PUBLIC rnoh)
130
+
131
+ # RNOH_BEGIN: manual_package_linking_2
132
+ + target_link_libraries(rnoh_app PUBLIC rnoh_audio_recorder_player)
133
+ # RNOH_END: manual_package_linking_2
134
+ ```
135
+
136
+ 打开 `entry/src/main/cpp/PackageProvider.cpp`,添加:
137
+
138
+ ```diff
139
+ #include "RNOH/PackageProvider.h"
140
+ #include "SamplePackage.h"
141
+ + #include "RNAudioRecorderPlayerPackage.h"
142
+
143
+ using namespace rnoh;
144
+
145
+ std::vector<std::shared_ptr<Package>> PackageProvider::getPackages(Package::Context ctx) {
146
+ return {
147
+ std::make_shared<SamplePackage>(ctx),
148
+ + std::make_shared<RNAudioRecorderPlayerPackage>(ctx)
149
+ };
150
+ }
151
+ ```
152
+
153
+ ### 4. 在 ArkTs 侧引入 RNAudioRecorderPlayerPackage
154
+
155
+ 打开 `entry/src/main/ets/RNPackagesFactory.ts`,添加:
156
+
157
+ ```diff
158
+ + import {RNAudioRecorderPlayerPackage} from '@react-native-ohos/react-native-audio-recorder-player/ts';
159
+
160
+ export function createRNPackages(ctx: RNPackageContext): RNPackage[] {
161
+ return [
162
+ + new RNAudioRecorderPlayerPackage(ctx)
163
+ ];
164
+ }
165
+ ```
166
+
167
+ </details>
168
+
169
+ ### 运行
170
+
171
+ 点击右上角的 `sync` 按钮
172
+
173
+ 或者在命令行终端执行:
174
+
175
+ ```bash
176
+ cd entry
177
+ ohpm install
178
+ ```
179
+ 然后编译、运行即可。
180
+
181
+ ## 约束与限制
182
+
183
+ ### 兼容性
184
+
185
+ 本文档内容基于以下版本验证通过:
186
+
187
+ 1. RNOH: 0.77.18; SDK: HarmonyOS 6.0.0 Release SDK; IDE: DevEco Studio 6.0.0.858; ROM: 6.0.0.112;
188
+ 2. RNOH: 0.82.30; SDK: HarmonyOS 6.0.2 Release SDK; IDE: DevEco Studio 6.0.2.650; ROM: 7.0.0;
189
+ 3. RNOH: 0.84.1; SDK: HarmonyOS SDK 26.0.0 Beta2; IDE: DevEco Studio 26.0.0.621; ROM: 7.0.0;
190
+
191
+ ### 权限要求
192
+
193
+ #### 在 entry 目录下的module.json5中添加权限
194
+
195
+ 在`entry/src/main/module.json5`,添加:
196
+
197
+ ```json
198
+ "requestPermissions": [
199
+ {"name": "ohos.permission.INTERNET"},//网络播放音频需添加
200
+ {
201
+ "name": "ohos.permission.MICROPHONE",
202
+ "reason": "$string:Access_Create_Audio",
203
+ "usedScene": {
204
+ "abilities": [
205
+ "EntryAbility"
206
+ ],
207
+ "when": "always"
208
+ }
209
+ },
210
+ ]
211
+ ```
212
+ #### 在 entry 目录下添加申请权限的原因
213
+
214
+ 打开 `entry/src/main/resources/base/element/string.json`,添加:
215
+
216
+ ```json
217
+ {
218
+ "string": [
219
+ {
220
+ "name": "Access_Create_Audio",
221
+ "value": "access create Audio"
222
+ }
223
+ ]
224
+ }
225
+ ```
226
+
227
+ ## 使用示例
228
+
229
+ 下面的代码展示了这个库的基本使用场景:
230
+
231
+ > [!WARNING] 使用时 import 的库名不变。
232
+
233
+ ```javascript
234
+ import AudioRecorderPlayer, {
235
+ AVEncoderAudioQualityIOSType,
236
+ AVEncodingOption,
237
+ AudioEncoderAndroidType,
238
+ AudioSourceAndroidType,
239
+ OutputFormatAndroidType,
240
+ AudioMimeHarmonyType,
241
+ AudioFormatHarmonyType,
242
+ AudioSourceHarmonyType,
243
+ AudioSet
244
+ } from 'react-native-audio-recorder-player'; import type {
245
+ PlayBackType,
246
+ RecordBackType,
247
+ } from 'react-native-audio-recorder-player';
248
+
249
+ import {
250
+ Dimensions,
251
+ Platform,
252
+ ActivityIndicator,
253
+ Image,
254
+ SafeAreaView,
255
+ StyleSheet,
256
+ Text,
257
+ TouchableOpacity,
258
+ View,
259
+ } from 'react-native';
260
+ import React, { Component } from 'react';
261
+
262
+ import type { ReactElement, ReactNode } from 'react';
263
+
264
+ const stylesButton: any = StyleSheet.create({
265
+ btn: {
266
+ backgroundColor: 'transparent',
267
+ alignSelf: 'center',
268
+ borderRadius: 4,
269
+ borderWidth: 2,
270
+ width: 320,
271
+ height: 52,
272
+ borderColor: 'white',
273
+
274
+ alignItems: 'center',
275
+ justifyContent: 'center',
276
+ },
277
+ btnDisabled: {
278
+ backgroundColor: 'rgb(243,243,243)',
279
+ alignSelf: 'center',
280
+ borderRadius: 4,
281
+ borderWidth: 2,
282
+ width: 320,
283
+ height: 52,
284
+ borderColor: '#333',
285
+
286
+ alignItems: 'center',
287
+ justifyContent: 'center',
288
+ },
289
+ txt: {
290
+ fontSize: 14,
291
+ color: 'white',
292
+ },
293
+ imgLeft: {
294
+ width: 24,
295
+ height: 24,
296
+ position: 'absolute',
297
+ left: 16,
298
+ },
299
+ });
300
+
301
+ interface ItemProps {
302
+ children?: ReactNode;
303
+ isLoading?: boolean;
304
+ isDisabled?: boolean;
305
+ onPress?: () => void;
306
+ style?: any;
307
+ disabledStyle?: any;
308
+ textStyle?: any;
309
+ imgLeftSrc?: any;
310
+ imgLeftStyle?: any;
311
+ indicatorColor?: string;
312
+ activeOpacity?: number;
313
+ }
314
+
315
+ class Button extends Component<ItemProps, any> {
316
+ private static defaultProps: Partial<ItemProps> = {
317
+ isLoading: false,
318
+ isDisabled: false,
319
+ style: stylesButton.btn,
320
+ textStyle: stylesButton.txt,
321
+ imgLeftStyle: stylesButton.imgLeft,
322
+ indicatorColor: 'white',
323
+ activeOpacity: 0.5,
324
+ };
325
+
326
+ constructor(props: ItemProps) {
327
+ super(props);
328
+ this.state = {};
329
+ }
330
+
331
+ public render(): ReactElement {
332
+ if (this.props.isDisabled) {
333
+ return (
334
+ <View style={this.props.disabledStyle}>
335
+ <Text style={this.props.textStyle}>{this.props.children}</Text>
336
+ </View>
337
+ );
338
+ }
339
+
340
+ if (this.props.isLoading) {
341
+ return (
342
+ <View style={this.props.style}>
343
+ <ActivityIndicator size="small" color={this.props.indicatorColor} />
344
+ </View>
345
+ );
346
+ }
347
+
348
+ return (
349
+ <TouchableOpacity
350
+ activeOpacity={this.props.activeOpacity}
351
+ onPress={this.props.onPress}>
352
+ <View style={this.props.style}>
353
+ {this.props.imgLeftSrc ? (
354
+ <Image
355
+ style={this.props.imgLeftStyle}
356
+ source={this.props.imgLeftSrc}
357
+ />
358
+ ) : null}
359
+ <Text style={this.props.textStyle}>{this.props.children}</Text>
360
+ </View>
361
+ </TouchableOpacity>
362
+ );
363
+ }
364
+ }
365
+
366
+ const styles: any = StyleSheet.create({
367
+ container: {
368
+ flex: 1,
369
+ backgroundColor: '#455A64',
370
+ flexDirection: 'column',
371
+ alignItems: 'center',
372
+ height: Dimensions.get('screen').height
373
+ },
374
+ titleTxt: {
375
+ marginTop: 100,
376
+ color: 'white',
377
+ fontSize: 28,
378
+ },
379
+ viewRecorder: {
380
+ marginTop: 40,
381
+ width: '100%',
382
+ alignItems: 'center',
383
+ },
384
+ recordBtnWrapper: {
385
+ flexDirection: 'row',
386
+ },
387
+ viewPlayer: {
388
+ marginTop: 60,
389
+ alignSelf: 'stretch',
390
+ alignItems: 'center',
391
+ },
392
+ viewBarWrapper: {
393
+ marginTop: 28,
394
+ marginHorizontal: 28,
395
+ alignSelf: 'stretch',
396
+ },
397
+ viewBar: {
398
+ backgroundColor: '#ccc',
399
+ height: 4,
400
+ alignSelf: 'stretch',
401
+ },
402
+ viewBarPlay: {
403
+ backgroundColor: 'white',
404
+ height: 4,
405
+ width: 0,
406
+ },
407
+ playStatusTxt: {
408
+ marginTop: 8,
409
+ color: '#ccc',
410
+ },
411
+ playBtnWrapper: {
412
+ flexDirection: 'row',
413
+ marginTop: 40,
414
+ },
415
+ btn: {
416
+ borderColor: 'white',
417
+ borderWidth: 1,
418
+ },
419
+ txt: {
420
+ color: 'white',
421
+ fontSize: 14,
422
+ marginHorizontal: 8,
423
+ marginVertical: 4,
424
+ },
425
+ txtRecordCounter: {
426
+ marginTop: 32,
427
+ color: 'white',
428
+ fontSize: 20,
429
+ textAlignVertical: 'center',
430
+ fontWeight: '200',
431
+ fontFamily: 'Helvetica Neue',
432
+ letterSpacing: 3,
433
+ width: 300
434
+ },
435
+ txtCounter: {
436
+ marginTop: 12,
437
+ color: 'white',
438
+ fontSize: 20,
439
+ textAlignVertical: 'center',
440
+ fontWeight: '200',
441
+ fontFamily: 'Helvetica Neue',
442
+ letterSpacing: 3,
443
+ width: 300
444
+ },
445
+ });
446
+
447
+ interface State {
448
+ isLoggingIn: boolean;
449
+ recordSecs: number;
450
+ recordTime: string;
451
+ currentPositionSec: number;
452
+ currentDurationSec: number;
453
+ playTime: string;
454
+ duration: string;
455
+ }
456
+
457
+ const screenWidth = Dimensions.get('screen').width;
458
+
459
+ class Page extends Component<any, State> {
460
+ private audioRecorderPlayer
461
+
462
+ constructor(props: any) {
463
+ super(props);
464
+ this.state = {
465
+ isLoggingIn: false,
466
+ recordSecs: 0,
467
+ recordTime: '00:00:00',
468
+ currentPositionSec: 0,
469
+ currentDurationSec: 0,
470
+ playTime: '00:00:00',
471
+ duration: '00:00:00',
472
+ };
473
+
474
+ this.audioRecorderPlayer = new AudioRecorderPlayer()
475
+ this.audioRecorderPlayer.setSubscriptionDuration(0.5); // optional. Default is 0.5
476
+ }
477
+
478
+ public render(): ReactElement {
479
+ let playWidth =
480
+ (this.state.currentPositionSec / this.state.currentDurationSec) *
481
+ (screenWidth - 56);
482
+
483
+ if (!playWidth) {
484
+ playWidth = 0;
485
+ }
486
+
487
+ return (
488
+ <SafeAreaView style={styles.container}>
489
+ <Text style={styles.titleTxt}>Audio Recorder Player</Text>
490
+ <Text style={styles.txtRecordCounter}>{this.state.recordTime}</Text>
491
+ <View style={styles.viewRecorder}>
492
+ <View style={styles.recordBtnWrapper}>
493
+ <Button
494
+ style={styles.btn}
495
+ onPress={this.onStartRecord}
496
+ textStyle={styles.txt}>
497
+ startRecord
498
+ </Button>
499
+ <Button
500
+ style={[
501
+ styles.btn,
502
+ {
503
+ marginLeft: 12,
504
+ },
505
+ ]}
506
+ onPress={this.onPauseRecord}
507
+ textStyle={styles.txt}>
508
+ Pause
509
+ </Button>
510
+ <Button
511
+ style={[
512
+ styles.btn,
513
+ {
514
+ marginLeft: 12,
515
+ },
516
+ ]}
517
+ onPress={this.onResumeRecord}
518
+ textStyle={styles.txt}>
519
+ Resume
520
+ </Button>
521
+ <Button
522
+ style={[styles.btn, { marginLeft: 12 }]}
523
+ onPress={this.onStopRecord}
524
+ textStyle={styles.txt}>
525
+ Stop
526
+ </Button>
527
+ </View>
528
+ </View>
529
+ <View style={styles.viewPlayer}>
530
+ <TouchableOpacity
531
+ style={styles.viewBarWrapper}
532
+ onPress={this.onStatusPress}>
533
+ <View style={styles.viewBar}>
534
+ <View style={[styles.viewBarPlay, { width: playWidth }]} />
535
+ </View>
536
+ </TouchableOpacity>
537
+ <Text style={styles.txtCounter}>
538
+ {this.state.playTime} / {this.state.duration}
539
+ </Text>
540
+ <View style={styles.playBtnWrapper}>
541
+ <Button
542
+ style={styles.btn}
543
+ onPress={this.onStartPlay}
544
+ textStyle={styles.txt}>
545
+ Play
546
+ </Button>
547
+ <Button
548
+ style={[
549
+ styles.btn,
550
+ {
551
+ marginLeft: 12,
552
+ },
553
+ ]}
554
+ onPress={this.onPausePlay}
555
+ textStyle={styles.txt}>
556
+ Pause
557
+ </Button>
558
+ <Button
559
+ style={[
560
+ styles.btn,
561
+ {
562
+ marginLeft: 12,
563
+ },
564
+ ]}
565
+ onPress={this.onResumePlay}
566
+ textStyle={styles.txt}>
567
+ Resume
568
+ </Button>
569
+ <Button
570
+ style={[
571
+ styles.btn,
572
+ {
573
+ marginLeft: 12,
574
+ },
575
+ ]}
576
+ onPress={this.onStopPlay}
577
+ textStyle={styles.txt}>
578
+ Stop
579
+ </Button>
580
+ </View>
581
+ </View>
582
+ </SafeAreaView>
583
+ );
584
+ }
585
+
586
+ private onStatusPress = (e: any): void => {
587
+ const touchX = e.nativeEvent.locationX;
588
+ console.log(`touchX: ${touchX}`);
589
+
590
+ const playWidth =
591
+ (this.state.currentPositionSec / this.state.currentDurationSec) *
592
+ (screenWidth - 56);
593
+ console.log(`currentPlayWidth: ${playWidth}`);
594
+
595
+ const currentPosition = Math.round(this.state.currentPositionSec);
596
+
597
+ if (playWidth && playWidth < touchX) {
598
+ const addSecs = Math.round(currentPosition + 1000);
599
+ this.audioRecorderPlayer.seekToPlayer(addSecs);
600
+ console.log(`addSecs: ${addSecs}`);
601
+ } else {
602
+ const subSecs = Math.round(currentPosition - 1000);
603
+ this.audioRecorderPlayer.seekToPlayer(subSecs);
604
+ console.log(`subSecs: ${subSecs}`);
605
+ }
606
+ };
607
+
608
+ private onStartRecord = async (): Promise<void> => {
609
+
610
+ const audioSet: AudioSet = {
611
+ AudioEncoderAndroid: AudioEncoderAndroidType.AAC,
612
+ AudioSourceAndroid: AudioSourceAndroidType.MIC,
613
+ AVEncoderAudioQualityKeyIOS: AVEncoderAudioQualityIOSType.high,
614
+ AVNumberOfChannelsKeyIOS: 2,
615
+ AVFormatIDKeyIOS: AVEncodingOption.aac,
616
+ OutputFormatAndroid: OutputFormatAndroidType.AAC_ADTS,
617
+ AudioSourceHarmony: AudioSourceHarmonyType.MIC,
618
+ AudioMimeHarmony: AudioMimeHarmonyType.AUDIO_AAC,
619
+ AudioFileFormatHarmony: AudioFormatHarmonyType.MPEG_4A,
620
+ AudioEncodingBitRateHarmony: 3200,
621
+ AudioSamplingRateHarmony: 44100,
622
+ AudioChannelsHarmony: 2,
623
+ };
624
+
625
+ console.log('audioSet', audioSet);
626
+
627
+ const uri = await this.audioRecorderPlayer.startRecorder(
628
+ 'audio.m4a',
629
+ audioSet,
630
+ );
631
+
632
+ this.audioRecorderPlayer.addRecordBackListener((e: RecordBackType) => {
633
+ console.log('record-back', e);
634
+ this.setState({
635
+ recordSecs: e.currentPosition,
636
+ recordTime: this.audioRecorderPlayer.mmssss(
637
+ Math.floor(e.currentPosition),
638
+ ),
639
+ });
640
+ });
641
+
642
+ console.log(`uri: ${uri}`);
643
+ };
644
+
645
+ private onPauseRecord = async (): Promise<void> => {
646
+ try {
647
+ const r = await this.audioRecorderPlayer.pauseRecorder();
648
+ console.log(r);
649
+ } catch (err) {
650
+ console.log('pauseRecord', err);
651
+ }
652
+ };
653
+
654
+ private onResumeRecord = async (): Promise<void> => {
655
+ await this.audioRecorderPlayer.resumeRecorder();
656
+ };
657
+
658
+ private onStopRecord = async (): Promise<void> => {
659
+ const result = await this.audioRecorderPlayer.stopRecorder();
660
+ this.audioRecorderPlayer.removeRecordBackListener();
661
+ this.setState({
662
+ recordSecs: 0,
663
+ });
664
+ console.log(result, '>>>>>>>stopRecorder');
665
+ };
666
+
667
+ private onStartPlay = async (): Promise<void> => {
668
+ console.log('onStartPlay');
669
+
670
+ try {
671
+ 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' });
672
+ const volume = await this.audioRecorderPlayer.setVolume(1);
673
+ this.audioRecorderPlayer.addPlayBackListener((e: PlayBackType) => {
674
+ console.log('playBackListener', e);
675
+ this.setState({
676
+ currentPositionSec: e.currentPosition,
677
+ currentDurationSec: e.duration,
678
+ playTime: this.audioRecorderPlayer.mmssss(e.currentPosition),
679
+ duration: this.audioRecorderPlayer.mmssss(Math.floor(e.duration)),
680
+ });
681
+ });
682
+
683
+ } catch (err) {
684
+ console.log('startPlayer error', err);
685
+ }
686
+ };
687
+
688
+ private onPausePlay = async (): Promise<void> => {
689
+ await this.audioRecorderPlayer.pausePlayer();
690
+ };
691
+
692
+ private onResumePlay = async (): Promise<void> => {
693
+ await this.audioRecorderPlayer.resumePlayer();
694
+ };
695
+
696
+ private onStopPlay = async (): Promise<void> => {
697
+ console.log('onStopPlay');
698
+ this.audioRecorderPlayer.stopPlayer();
699
+ this.audioRecorderPlayer.removePlayBackListener();
700
+ };
701
+ }
702
+
703
+ export default Page;
704
+ ```
705
+
706
+ ## 使用说明
707
+
708
+ **事件监听**
709
+ ```javascript
710
+ this.audioRecorderPlayer = new AudioRecorderPlayer()
711
+ // 添加播放回调
712
+ this.audioRecorderPlayer.addPlayBackListener((e: PlayBackType) => {
713
+ // 播放中触发
714
+ });
715
+ // 移除播放回调
716
+ this.audioRecorderPlayer.removePlayBackListener();
717
+ // 添加录制回调
718
+ this.audioRecorderPlayer.addRecordBackListener((e: RecordBackType) => {
719
+ // 录制中触发
720
+ });
721
+ // 移除录制回调
722
+ this.audioRecorderPlayer.removeRecordBackListener();
723
+ ```
724
+
725
+ **录制控制**
726
+ ```javascript
727
+ this.audioRecorderPlayer = new AudioRecorderPlayer()
728
+ // 开始录制
729
+ this.audioRecorderPlayer.startRecorder(
730
+ 'audio.m4a',
731
+ audioSet,
732
+ );
733
+ // 暂停录制
734
+ this.audioRecorderPlayer.pauseRecorder();
735
+ // 恢复录制
736
+ this.audioRecorderPlayer.resumeRecorder();
737
+ // 停止录制
738
+ this.audioRecorderPlayer.stopRecorder();
739
+ ```
740
+
741
+ **播放控制**
742
+ ```javascript
743
+ this.audioRecorderPlayer = new AudioRecorderPlayer()
744
+ // 设置订阅间隔时长
745
+ this.audioRecorderPlayer.setSubscriptionDuration(0.5);
746
+ // 定位到XX播放位置
747
+ this.audioRecorderPlayer.seekToPlayer(addSecs);
748
+ // 暂停播放
749
+ this.audioRecorderPlayer.pausePlayer();
750
+ // 恢复播放
751
+ this.audioRecorderPlayer.resumePlayer();
752
+ // 停止播放
753
+ this.audioRecorderPlayer.stopPlayer();
754
+ // 设置音量
755
+ this.audioRecorderPlayer.setVolume(1);
756
+ // 将毫秒转换为“分钟:秒:毫秒”格式的字符串
757
+ this.audioRecorderPlayer.mmssss(e.currentPosition),
758
+ // 将毫秒转换为“分:秒”格式的字符串
759
+ this.audioRecorderPlayer.mmss(
760
+ Math.floor(e.currentPosition),
761
+ ),
762
+ ```
763
+
764
+ ## 接口说明
765
+
766
+ > [!TIP] "Platform"列表示该属性在原三方库上支持的平台。
767
+
768
+ > [!TIP] "HarmonyOS Support"列为 yes 表示 OpenHarmony 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。
769
+
770
+ | Name | Description | Type | Required | Platform | HarmonyOS Support |
771
+ | ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- | -------- | -------- | ----------------- |
772
+ | `mmss` | 将毫秒转换为“分:秒”格式的字符串 | function(milliseconds:number): string | No | Android,iOS | yes |
773
+ | `mmssss` | 将毫秒转换为“分钟:秒:毫秒”格式的字符串 | function(milliseconds:number): string | No | Android,iOS | yes |
774
+ | `setSubscriptionDuration` | 设置录音器或播放器启动时的默认回调时间间隔。默认为 0.5,即 500 毫秒。| function(seconds:float):Promise<string> | No | Android,iOS | yes |
775
+ | `addRecordBackListener` |从原生模块获取回调。将接收 currentPosition、currentMetering(如果在 startRecorder 中进行了配置)。 | function(callBack:(value:RecordBackType)=>void): void | No | Android,iOS | yes |
776
+ | `removeRecordBackListener` |移除 recordBack 监听器。 | function(): void | No | Android,iOS | yes |
777
+ | `addPlayBackListener` | 从原生模块获取回调,将收到 duration 和 currentPosition。 | function(callBack:(value: PlayBackType)=>void): void | No | Android,iOS | yes |
778
+ | `removePlayBackListener` | 移除 playback 监听器。 | function(): void | No | Android,iOS | yes |
779
+ | `startRecorder` |开始录音。不传递 uri 参数时,音频将保存在默认位置。 | function(uri?:string,audioSets?: AudioSet,meteringEnabled?: boolean): Promise< string > | No | Android,iOS | yes |
780
+ | `pauseRecorder` | 暂停录音。 | function() : Promise< string > | No | Android,iOS | yes |
781
+ | `resumeRecorder` | 恢复录音。 | function() : Promise< string > | No | Android,iOS | yes |
782
+ | `stopRecorder` | 停止录音。 | function() : Promise< string > | No | Android,iOS | yes |
783
+ | `startPlayer` | 开始播放。不传递参数时,将播放默认位置的音频。 | function(uri?:string,httpHeaders?:Record<string,string>) : Promise< string > | No | Android,iOS | yes |
784
+ | `stopPlayer` | 停止播放。 | function() : Promise< string > | No | Android,iOS | yes |
785
+ | `pausePlayer` | 暂停播放。 | function() : Promise< string > | No | Android,iOS | yes |
786
+ | `resumePlayer` | 恢复播放。 | function() : Promise< string > | No | Android,iOS | yes |
787
+ | `seekToPlayer` | 音频跳转。 | function(milliseconds:number) : Promise< string > | No | Android,iOS | yes |
788
+ | `setVolume` | 设置音频播放器的音量(默认值为 1.0,取值范围:0.0 ~ 1.0)。 | function(volume:float) : Promise< string > | No | Android,iOS | yes |
789
+ | `setPlaybackSpeed` | 设置播放速度。<br/>鸿蒙化后仅支持0.125、0.25、0.5、0.75、1、1.25、1.5、1.75、2和3倍速设置,其中3倍速仅在API version 13开始支持。 | function(playbackSpeed: number): Promise< string > | No | Android,iOS | partially (3倍速仅在API version 13开始支持) |
790
+
791
+ ## 遗留问题
792
+ 无
793
+
794
+ ## 其他
795
+ 无
796
+
797
+ ## 目录结构
798
+ ````
799
+ /rntpc_react-native-audio-recorder-player # 项目根目录
800
+ ├── harmony # 鸿蒙适配代码
801
+ │ ├─ audio_recorder_player.har # har包
802
+ │ └─ audio_recorder_player # 鸿蒙适配核心代码
803
+ │ └─ index.ets # 鸿蒙适配代码入口
804
+ │ └─ src/main/ets
805
+ │ ├─ audioPlayer
806
+ │ └─ AudioPlayer.ts # 音频播放文件
807
+ │ └─ recorder
808
+ │ └─ Recorder.ts # 音频录制文件
809
+ ├── src # RN代码
810
+ │ └─ NativeAudioRecorderPlayerModule.ts # TurboModule文件
811
+ ├── index.ts # 入口文件
812
+ ├── README_en.md # 英文安装使用方法
813
+ ├── README.md # 中文安装使用方法
814
+ ````
815
+
816
+ ## 贡献代码
817
+
818
+ 使用过程中发现任何问题都可以提交 [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) 。
819
+
820
+ ## 开源协议
821
+
822
+ 本项目基于 [The MIT License (MIT)](https://github.com/hyochan/react-native-audio-recorder-player/blob/main/LICENSE) ,请自由地享受和参与开源。