@react-native-ohos/audio-toolkit 2.0.4-rc.2 → 2.0.4-rc.3
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/CHANGELOG.md +2 -136
- package/LICENSE +22 -22
- package/{harmony/audio_toolkit/OAT.xml → OAT.xml} +47 -37
- package/README.md +11 -17
- package/ReactNativeAudioToolkit.podspec +18 -18
- package/harmony/audio_toolkit/build-profile.json5 +7 -7
- package/harmony/audio_toolkit/hvigorfile.ts +1 -1
- package/harmony/audio_toolkit/index.ets +26 -25
- package/harmony/audio_toolkit/oh-package.json5 +12 -12
- package/harmony/audio_toolkit/src/main/cpp/AudioToolkitPackage.h +9 -13
- package/harmony/audio_toolkit/src/main/cpp/CMakeLists.txt +3 -2
- package/harmony/audio_toolkit/src/main/cpp/generated/RNOH/generated/{BaseAudioToolkitPackage.h → BaseReactNativeAudioToolkitPackage.h} +69 -76
- package/harmony/audio_toolkit/src/main/cpp/generated/RNOH/generated/turbo_modules/RCTAudioPlayer.cpp +23 -23
- package/harmony/audio_toolkit/src/main/cpp/generated/RNOH/generated/turbo_modules/RCTAudioPlayer.h +16 -16
- package/harmony/audio_toolkit/src/main/cpp/generated/RNOH/generated/turbo_modules/RCTAudioRecorder.cpp +20 -20
- package/harmony/audio_toolkit/src/main/cpp/generated/RNOH/generated/turbo_modules/RCTAudioRecorder.h +16 -16
- package/harmony/audio_toolkit/src/main/cpp/generated/react/renderer/components/{audio_toolkit → react_native_audio_toolkit}/ComponentDescriptors.h +22 -24
- package/harmony/audio_toolkit/src/main/cpp/generated/react/renderer/components/{audio_toolkit → react_native_audio_toolkit}/EventEmitters.cpp +18 -16
- package/harmony/audio_toolkit/src/main/cpp/generated/react/renderer/components/{audio_toolkit → react_native_audio_toolkit}/EventEmitters.h +19 -17
- package/harmony/audio_toolkit/src/main/cpp/generated/react/renderer/components/{audio_toolkit → react_native_audio_toolkit}/Props.cpp +21 -19
- package/harmony/audio_toolkit/src/main/cpp/generated/react/renderer/components/{audio_toolkit → react_native_audio_toolkit}/Props.h +20 -18
- package/harmony/audio_toolkit/src/main/cpp/generated/react/renderer/components/{audio_toolkit → react_native_audio_toolkit}/ShadowNodes.cpp +19 -17
- package/harmony/audio_toolkit/src/main/cpp/generated/react/renderer/components/{audio_toolkit → react_native_audio_toolkit}/ShadowNodes.h +25 -23
- package/harmony/audio_toolkit/src/main/cpp/generated/react/renderer/components/{audio_toolkit → react_native_audio_toolkit}/States.cpp +18 -16
- package/harmony/audio_toolkit/src/main/cpp/generated/react/renderer/components/{audio_toolkit → react_native_audio_toolkit}/States.h +23 -19
- package/harmony/audio_toolkit/src/main/ets/{AudioToolkitPackage.ts → AudioToolkitPackage.ets} +51 -50
- package/harmony/audio_toolkit/src/main/ets/Logger.ts +63 -63
- package/harmony/audio_toolkit/src/main/ets/RNCAudioPlayerTurboModule.ts +432 -432
- package/harmony/audio_toolkit/src/main/ets/RNCAudioRecorderTurboModule.ts +260 -260
- package/harmony/audio_toolkit/src/main/ets/generated/components/ts.ts +5 -5
- package/harmony/audio_toolkit/src/main/ets/generated/index.ets +5 -5
- package/harmony/audio_toolkit/src/main/ets/generated/ts.ts +6 -6
- package/harmony/audio_toolkit/src/main/ets/generated/turboModules/RCTAudioPlayer.ts +40 -40
- package/harmony/audio_toolkit/src/main/ets/generated/turboModules/RCTAudioRecorder.ts +24 -24
- package/harmony/audio_toolkit/src/main/ets/generated/turboModules/ts.ts +6 -6
- package/harmony/audio_toolkit/src/main/module.json5 +6 -6
- package/harmony/audio_toolkit/src/main/resources/base/element/string.json +7 -7
- package/harmony/audio_toolkit/src/main/resources/en_US/element/string.json +7 -7
- package/harmony/audio_toolkit/src/main/resources/zh_CN/element/string.json +7 -7
- package/harmony/audio_toolkit/{ts.ts → ts.ets} +25 -25
- package/harmony/audio_toolkit.har +0 -0
- package/package.json +48 -47
- package/src/Player.js +329 -329
- package/src/PlayerModule.ts +51 -51
- package/src/Recorder.js +183 -183
- package/src/RecorderModule.ts +83 -83
- package/src/index.ts +5 -5
- package/COMMITTERS.md +0 -7
- package/harmony/audio_toolkit/BuildProfile.ets +0 -17
- package/harmony/audio_toolkit/LICENSE +0 -21
- package/harmony/audio_toolkit/NOTICE +0 -33
- package/harmony/audio_toolkit/README.OpenSource +0 -11
- package/harmony/audio_toolkit/README.md +0 -17
|
@@ -1,261 +1,261 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* MIT License
|
|
3
|
-
*
|
|
4
|
-
* Copyright (C) 2024 Huawei Device Co., Ltd.
|
|
5
|
-
*
|
|
6
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
-
* in the Software without restriction, including without limitation the rights
|
|
9
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
-
* furnished to do so, subject to the following conditions:
|
|
12
|
-
*
|
|
13
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
-
* copies or substantial portions of the Software.
|
|
15
|
-
*
|
|
16
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
-
* SOFTWARE.
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
import { TurboModule, type TurboModuleContext } from '@rnoh/react-native-openharmony/ts';
|
|
26
|
-
import { type TM } from './generated/ts';
|
|
27
|
-
import { type BusinessError } from '@ohos.base';
|
|
28
|
-
import media from '@ohos.multimedia.media';
|
|
29
|
-
import fs from '@ohos.file.fs';
|
|
30
|
-
import abilityAccessCtrl, { type Permissions } from '@ohos.abilityAccessCtrl';
|
|
31
|
-
import picker from '@ohos.file.picker';
|
|
32
|
-
import logger from './Logger';
|
|
33
|
-
|
|
34
|
-
const TAG = 'RCTAudioRecorderTurboModule';
|
|
35
|
-
|
|
36
|
-
const PERMISSIONS: Array<Permissions> = [
|
|
37
|
-
'ohos.permission.MICROPHONE',
|
|
38
|
-
];
|
|
39
|
-
|
|
40
|
-
enum FormatType {
|
|
41
|
-
CFT_MPEG_4 = 'mp4',
|
|
42
|
-
CFT_MPEG_4A = 'm4a'
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export class RCTAudioRecorderTurboModule extends TurboModule {
|
|
46
|
-
private _file: fs.File;
|
|
47
|
-
// 音频参数
|
|
48
|
-
private avRecorder: media.AVRecorder | undefined = undefined;
|
|
49
|
-
private avProfile: media.AVRecorderProfile;
|
|
50
|
-
private avConfig: media.AVRecorderConfig;
|
|
51
|
-
private readonly AUDIOBITRATE_DEFAULT = 48000;
|
|
52
|
-
private readonly AUDIOCHANNELS_DEFAULT = 2;
|
|
53
|
-
private readonly AUDIOSAMPLERATE = 48000;
|
|
54
|
-
private readonly FD_PATH = 'fd://';
|
|
55
|
-
|
|
56
|
-
constructor(protected ctx: TurboModuleContext) {
|
|
57
|
-
super(ctx);
|
|
58
|
-
this.ctx = ctx;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// 注册audioRecorder回调函数
|
|
62
|
-
setAudioRecorderCallback(recorderId: number): void {
|
|
63
|
-
if (this.avRecorder !== undefined) {
|
|
64
|
-
// 状态机变化回调函数
|
|
65
|
-
this.avRecorder.on('stateChange', (state: media.AVRecorderState, reason: media.StateChangeReason) => {
|
|
66
|
-
this.toEmit(recorderId, 'info', {
|
|
67
|
-
event: 'info',
|
|
68
|
-
data: {
|
|
69
|
-
info: {
|
|
70
|
-
what: state,
|
|
71
|
-
extra: reason,
|
|
72
|
-
},
|
|
73
|
-
},
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
// 错误上报回调函数
|
|
77
|
-
this.avRecorder.on('error', (err: BusinessError) => {
|
|
78
|
-
this.toEmit(recorderId, 'error', {
|
|
79
|
-
event: 'err',
|
|
80
|
-
message: 'harmony MediaRecorder error',
|
|
81
|
-
});
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
emit(name: string, data: object): void {
|
|
87
|
-
this.ctx.rnInstance.emitDeviceEvent(name, data);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
toEmit(recorderId: number, name: string, data: object): void {
|
|
91
|
-
this.emit(`RCTAudioRecorderEvent:${recorderId}`, { event: name, data });
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// 开始录制对应的流程
|
|
95
|
-
async startRecordingProcess(path: string, next: (object?, fsPath?: string) => void,
|
|
96
|
-
preparingCall?: (object?) => void): Promise<void> {
|
|
97
|
-
try {
|
|
98
|
-
let audioSaveOptions = new picker.AudioSaveOptions();
|
|
99
|
-
audioSaveOptions.newFileNames = [path];
|
|
100
|
-
let audioPicker = new picker.AudioViewPicker();
|
|
101
|
-
|
|
102
|
-
audioPicker.save(audioSaveOptions, async (err: BusinessError, audioSaveResult: Array<string>) => {
|
|
103
|
-
if (err || !audioSaveResult || audioSaveResult.length === 0) {
|
|
104
|
-
next(null);
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
preparingCall(null);
|
|
108
|
-
this._file = fs.openSync(audioSaveResult[0], fs.OpenMode.READ_WRITE);
|
|
109
|
-
this.avConfig.url = this.FD_PATH + this._file.fd;
|
|
110
|
-
this.requestPermission().then(async res => {
|
|
111
|
-
if (res) {
|
|
112
|
-
await this.avRecorder.prepare(this.avConfig);
|
|
113
|
-
await this.avRecorder.start();
|
|
114
|
-
next(null, audioSaveResult[0]);
|
|
115
|
-
} else {
|
|
116
|
-
next(null);
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
});
|
|
120
|
-
} catch (error) {
|
|
121
|
-
next(null);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// 以下demo为使用fs文件系统打开沙箱地址获取媒体文件地址并通过url属性进行播放示例
|
|
126
|
-
async prepare(recorderId: number, path: string, option: TM.RCTAudioRecorder.RecorderOptions
|
|
127
|
-
, next: (object?, fsPath?: string) => void, preparingCall?: (object?) => void): Promise<void> {
|
|
128
|
-
if (path === null || path === undefined) {
|
|
129
|
-
next({
|
|
130
|
-
err: 'invalidpath',
|
|
131
|
-
stackTrace: 'Exception occurred while parsing stack trace',
|
|
132
|
-
message: 'Provided path was empty',
|
|
133
|
-
});
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
if (this.avRecorder !== undefined) {
|
|
137
|
-
await this.avRecorder.release();
|
|
138
|
-
this.avRecorder = undefined;
|
|
139
|
-
}
|
|
140
|
-
// 1.创建录制实例
|
|
141
|
-
this.avRecorder = await media.createAVRecorder();
|
|
142
|
-
this.setAudioRecorderCallback(recorderId);
|
|
143
|
-
this.avProfile = {
|
|
144
|
-
audioBitrate: option.bitrate ?? this.AUDIOBITRATE_DEFAULT, // 音频比特率
|
|
145
|
-
audioChannels: option.channels ?? this.AUDIOCHANNELS_DEFAULT, // 音频声道数
|
|
146
|
-
audioCodec: media.CodecMimeType.AUDIO_AAC, // 音频编码格式,当前只支持aac
|
|
147
|
-
audioSampleRate: option.sampleRate ?? this.AUDIOSAMPLERATE, // 音频采样率
|
|
148
|
-
fileFormat: this.getFileFormat(option.format, path), // 封装格式,当前只支持m4a、mp4
|
|
149
|
-
};
|
|
150
|
-
logger.debug(`avProfile = ${JSON.stringify(this.avProfile)}}`);
|
|
151
|
-
this.avConfig = {
|
|
152
|
-
audioSourceType: media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC, // 音频输入源,这里设置为麦克风
|
|
153
|
-
profile: this.avProfile,
|
|
154
|
-
url: '', // 参考应用文件访问与管理开发示例新建并读写一个文件
|
|
155
|
-
};
|
|
156
|
-
await this.startRecordingProcess(path, next, preparingCall);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
private formatFromPath(path: String): media.ContainerFormatType {
|
|
160
|
-
if (!path) {
|
|
161
|
-
return media.ContainerFormatType.CFT_MPEG_4A;
|
|
162
|
-
}
|
|
163
|
-
let ext = path.substring(path.lastIndexOf('.') + 1);
|
|
164
|
-
if (!ext) {
|
|
165
|
-
return media.ContainerFormatType.CFT_MPEG_4A;
|
|
166
|
-
}
|
|
167
|
-
return this.getFileFormat(ext);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
getFileFormat(format: string, path?: string): media.ContainerFormatType {
|
|
171
|
-
if (!format) {
|
|
172
|
-
return this.formatFromPath(path);
|
|
173
|
-
}
|
|
174
|
-
switch (format) {
|
|
175
|
-
case FormatType.CFT_MPEG_4:
|
|
176
|
-
return media.ContainerFormatType.CFT_MPEG_4;
|
|
177
|
-
default:
|
|
178
|
-
return media.ContainerFormatType.CFT_MPEG_4A
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
async record(recorderId: number, next: (object?) => void): Promise<void> {
|
|
183
|
-
if (this.avRecorder !== undefined) {
|
|
184
|
-
this.avRecorder.resume();
|
|
185
|
-
next();
|
|
186
|
-
} else {
|
|
187
|
-
next({ code: 'notfound', recorderId: `${recorderId}not found.` });
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
async pause(recorderId: number, next: () => void): Promise<void> {
|
|
192
|
-
if (this.avRecorder !== undefined) {
|
|
193
|
-
// 1. 停止录制
|
|
194
|
-
if (this.avRecorder.state === 'started') { // 仅在started状态下调用started为合理状态切换
|
|
195
|
-
await this.avRecorder.pause();
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
next();
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Get clipboard image as JPG in base64, this method returns a `Promise`, so you can use following code to get clipboard content
|
|
203
|
-
* ```javascript
|
|
204
|
-
* async _getContent() {
|
|
205
|
-
* var content = await Clipboard.getImageJPG();
|
|
206
|
-
* }
|
|
207
|
-
* ```
|
|
208
|
-
*/
|
|
209
|
-
|
|
210
|
-
// 停止录制对应的流程
|
|
211
|
-
async stop(recorderId: number, next: (object?) => void): Promise<void> {
|
|
212
|
-
if (this.avRecorder !== undefined) {
|
|
213
|
-
// 1. 停止录制
|
|
214
|
-
if (this.avRecorder.state === 'started' ||
|
|
215
|
-
this.avRecorder.state === 'paused') { // 仅在started或者paused状态下调用stop为合理状态切换
|
|
216
|
-
await this.avRecorder.stop();
|
|
217
|
-
}
|
|
218
|
-
next();
|
|
219
|
-
} else {
|
|
220
|
-
next({ code: 'notfound', recorderId: `${recorderId}not found.` });
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* recorder destroy
|
|
226
|
-
* @param recorderId
|
|
227
|
-
* @param next
|
|
228
|
-
* @returns void
|
|
229
|
-
*/
|
|
230
|
-
async destroy(recorderId: number, next: (object?) => void): Promise<void> {
|
|
231
|
-
if (this.avRecorder) {
|
|
232
|
-
//重置
|
|
233
|
-
await this.avRecorder.reset();
|
|
234
|
-
//释放录制实例
|
|
235
|
-
await this.avRecorder.release();
|
|
236
|
-
//关闭录制文件fd
|
|
237
|
-
fs.close(this._file);
|
|
238
|
-
this.toEmit(recorderId, 'info', {
|
|
239
|
-
message: 'Destroyed recorder',
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
logger.debug(TAG, 'RCTAudioRecorderTurboModule destroy');
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
requestPermission(): Promise<boolean> {
|
|
246
|
-
return new Promise<boolean>((resolve) => {
|
|
247
|
-
abilityAccessCtrl.createAtManager()
|
|
248
|
-
.requestPermissionsFromUser(this.ctx.uiAbilityContext, PERMISSIONS).then(result => {
|
|
249
|
-
if (result.authResults[0] === 0) {
|
|
250
|
-
resolve(true);
|
|
251
|
-
} else {
|
|
252
|
-
logger.debug(`getString,text out:用户拒绝授权`);
|
|
253
|
-
resolve(false);
|
|
254
|
-
}
|
|
255
|
-
}).catch(() => {
|
|
256
|
-
logger.debug(`getString,text out:用户拒绝授权`);
|
|
257
|
-
resolve(false);
|
|
258
|
-
});
|
|
259
|
-
});
|
|
260
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (C) 2024 Huawei Device Co., Ltd.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { TurboModule, type TurboModuleContext } from '@rnoh/react-native-openharmony/ts';
|
|
26
|
+
import { type TM } from './generated/ts';
|
|
27
|
+
import { type BusinessError } from '@ohos.base';
|
|
28
|
+
import media from '@ohos.multimedia.media';
|
|
29
|
+
import fs from '@ohos.file.fs';
|
|
30
|
+
import abilityAccessCtrl, { type Permissions } from '@ohos.abilityAccessCtrl';
|
|
31
|
+
import picker from '@ohos.file.picker';
|
|
32
|
+
import logger from './Logger';
|
|
33
|
+
|
|
34
|
+
const TAG = 'RCTAudioRecorderTurboModule';
|
|
35
|
+
|
|
36
|
+
const PERMISSIONS: Array<Permissions> = [
|
|
37
|
+
'ohos.permission.MICROPHONE',
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
enum FormatType {
|
|
41
|
+
CFT_MPEG_4 = 'mp4',
|
|
42
|
+
CFT_MPEG_4A = 'm4a'
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export class RCTAudioRecorderTurboModule extends TurboModule {
|
|
46
|
+
private _file: fs.File;
|
|
47
|
+
// 音频参数
|
|
48
|
+
private avRecorder: media.AVRecorder | undefined = undefined;
|
|
49
|
+
private avProfile: media.AVRecorderProfile;
|
|
50
|
+
private avConfig: media.AVRecorderConfig;
|
|
51
|
+
private readonly AUDIOBITRATE_DEFAULT = 48000;
|
|
52
|
+
private readonly AUDIOCHANNELS_DEFAULT = 2;
|
|
53
|
+
private readonly AUDIOSAMPLERATE = 48000;
|
|
54
|
+
private readonly FD_PATH = 'fd://';
|
|
55
|
+
|
|
56
|
+
constructor(protected ctx: TurboModuleContext) {
|
|
57
|
+
super(ctx);
|
|
58
|
+
this.ctx = ctx;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// 注册audioRecorder回调函数
|
|
62
|
+
setAudioRecorderCallback(recorderId: number): void {
|
|
63
|
+
if (this.avRecorder !== undefined) {
|
|
64
|
+
// 状态机变化回调函数
|
|
65
|
+
this.avRecorder.on('stateChange', (state: media.AVRecorderState, reason: media.StateChangeReason) => {
|
|
66
|
+
this.toEmit(recorderId, 'info', {
|
|
67
|
+
event: 'info',
|
|
68
|
+
data: {
|
|
69
|
+
info: {
|
|
70
|
+
what: state,
|
|
71
|
+
extra: reason,
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
// 错误上报回调函数
|
|
77
|
+
this.avRecorder.on('error', (err: BusinessError) => {
|
|
78
|
+
this.toEmit(recorderId, 'error', {
|
|
79
|
+
event: 'err',
|
|
80
|
+
message: 'harmony MediaRecorder error',
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
emit(name: string, data: object): void {
|
|
87
|
+
this.ctx.rnInstance.emitDeviceEvent(name, data);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
toEmit(recorderId: number, name: string, data: object): void {
|
|
91
|
+
this.emit(`RCTAudioRecorderEvent:${recorderId}`, { event: name, data });
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// 开始录制对应的流程
|
|
95
|
+
async startRecordingProcess(path: string, next: (object?, fsPath?: string) => void,
|
|
96
|
+
preparingCall?: (object?) => void): Promise<void> {
|
|
97
|
+
try {
|
|
98
|
+
let audioSaveOptions = new picker.AudioSaveOptions();
|
|
99
|
+
audioSaveOptions.newFileNames = [path];
|
|
100
|
+
let audioPicker = new picker.AudioViewPicker();
|
|
101
|
+
|
|
102
|
+
audioPicker.save(audioSaveOptions, async (err: BusinessError, audioSaveResult: Array<string>) => {
|
|
103
|
+
if (err || !audioSaveResult || audioSaveResult.length === 0) {
|
|
104
|
+
next(null);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
preparingCall(null);
|
|
108
|
+
this._file = fs.openSync(audioSaveResult[0], fs.OpenMode.READ_WRITE);
|
|
109
|
+
this.avConfig.url = this.FD_PATH + this._file.fd;
|
|
110
|
+
this.requestPermission().then(async res => {
|
|
111
|
+
if (res) {
|
|
112
|
+
await this.avRecorder.prepare(this.avConfig);
|
|
113
|
+
await this.avRecorder.start();
|
|
114
|
+
next(null, audioSaveResult[0]);
|
|
115
|
+
} else {
|
|
116
|
+
next(null);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
} catch (error) {
|
|
121
|
+
next(null);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// 以下demo为使用fs文件系统打开沙箱地址获取媒体文件地址并通过url属性进行播放示例
|
|
126
|
+
async prepare(recorderId: number, path: string, option: TM.RCTAudioRecorder.RecorderOptions
|
|
127
|
+
, next: (object?, fsPath?: string) => void, preparingCall?: (object?) => void): Promise<void> {
|
|
128
|
+
if (path === null || path === undefined) {
|
|
129
|
+
next({
|
|
130
|
+
err: 'invalidpath',
|
|
131
|
+
stackTrace: 'Exception occurred while parsing stack trace',
|
|
132
|
+
message: 'Provided path was empty',
|
|
133
|
+
});
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (this.avRecorder !== undefined) {
|
|
137
|
+
await this.avRecorder.release();
|
|
138
|
+
this.avRecorder = undefined;
|
|
139
|
+
}
|
|
140
|
+
// 1.创建录制实例
|
|
141
|
+
this.avRecorder = await media.createAVRecorder();
|
|
142
|
+
this.setAudioRecorderCallback(recorderId);
|
|
143
|
+
this.avProfile = {
|
|
144
|
+
audioBitrate: option.bitrate ?? this.AUDIOBITRATE_DEFAULT, // 音频比特率
|
|
145
|
+
audioChannels: option.channels ?? this.AUDIOCHANNELS_DEFAULT, // 音频声道数
|
|
146
|
+
audioCodec: media.CodecMimeType.AUDIO_AAC, // 音频编码格式,当前只支持aac
|
|
147
|
+
audioSampleRate: option.sampleRate ?? this.AUDIOSAMPLERATE, // 音频采样率
|
|
148
|
+
fileFormat: this.getFileFormat(option.format, path), // 封装格式,当前只支持m4a、mp4
|
|
149
|
+
};
|
|
150
|
+
logger.debug(`avProfile = ${JSON.stringify(this.avProfile)}}`);
|
|
151
|
+
this.avConfig = {
|
|
152
|
+
audioSourceType: media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC, // 音频输入源,这里设置为麦克风
|
|
153
|
+
profile: this.avProfile,
|
|
154
|
+
url: '', // 参考应用文件访问与管理开发示例新建并读写一个文件
|
|
155
|
+
};
|
|
156
|
+
await this.startRecordingProcess(path, next, preparingCall);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
private formatFromPath(path: String): media.ContainerFormatType {
|
|
160
|
+
if (!path) {
|
|
161
|
+
return media.ContainerFormatType.CFT_MPEG_4A;
|
|
162
|
+
}
|
|
163
|
+
let ext = path.substring(path.lastIndexOf('.') + 1);
|
|
164
|
+
if (!ext) {
|
|
165
|
+
return media.ContainerFormatType.CFT_MPEG_4A;
|
|
166
|
+
}
|
|
167
|
+
return this.getFileFormat(ext);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
getFileFormat(format: string, path?: string): media.ContainerFormatType {
|
|
171
|
+
if (!format) {
|
|
172
|
+
return this.formatFromPath(path);
|
|
173
|
+
}
|
|
174
|
+
switch (format) {
|
|
175
|
+
case FormatType.CFT_MPEG_4:
|
|
176
|
+
return media.ContainerFormatType.CFT_MPEG_4;
|
|
177
|
+
default:
|
|
178
|
+
return media.ContainerFormatType.CFT_MPEG_4A
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async record(recorderId: number, next: (object?) => void): Promise<void> {
|
|
183
|
+
if (this.avRecorder !== undefined) {
|
|
184
|
+
this.avRecorder.resume();
|
|
185
|
+
next();
|
|
186
|
+
} else {
|
|
187
|
+
next({ code: 'notfound', recorderId: `${recorderId}not found.` });
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
async pause(recorderId: number, next: () => void): Promise<void> {
|
|
192
|
+
if (this.avRecorder !== undefined) {
|
|
193
|
+
// 1. 停止录制
|
|
194
|
+
if (this.avRecorder.state === 'started') { // 仅在started状态下调用started为合理状态切换
|
|
195
|
+
await this.avRecorder.pause();
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
next();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Get clipboard image as JPG in base64, this method returns a `Promise`, so you can use following code to get clipboard content
|
|
203
|
+
* ```javascript
|
|
204
|
+
* async _getContent() {
|
|
205
|
+
* var content = await Clipboard.getImageJPG();
|
|
206
|
+
* }
|
|
207
|
+
* ```
|
|
208
|
+
*/
|
|
209
|
+
|
|
210
|
+
// 停止录制对应的流程
|
|
211
|
+
async stop(recorderId: number, next: (object?) => void): Promise<void> {
|
|
212
|
+
if (this.avRecorder !== undefined) {
|
|
213
|
+
// 1. 停止录制
|
|
214
|
+
if (this.avRecorder.state === 'started' ||
|
|
215
|
+
this.avRecorder.state === 'paused') { // 仅在started或者paused状态下调用stop为合理状态切换
|
|
216
|
+
await this.avRecorder.stop();
|
|
217
|
+
}
|
|
218
|
+
next();
|
|
219
|
+
} else {
|
|
220
|
+
next({ code: 'notfound', recorderId: `${recorderId}not found.` });
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* recorder destroy
|
|
226
|
+
* @param recorderId
|
|
227
|
+
* @param next
|
|
228
|
+
* @returns void
|
|
229
|
+
*/
|
|
230
|
+
async destroy(recorderId: number, next: (object?) => void): Promise<void> {
|
|
231
|
+
if (this.avRecorder) {
|
|
232
|
+
//重置
|
|
233
|
+
await this.avRecorder.reset();
|
|
234
|
+
//释放录制实例
|
|
235
|
+
await this.avRecorder.release();
|
|
236
|
+
//关闭录制文件fd
|
|
237
|
+
fs.close(this._file);
|
|
238
|
+
this.toEmit(recorderId, 'info', {
|
|
239
|
+
message: 'Destroyed recorder',
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
logger.debug(TAG, 'RCTAudioRecorderTurboModule destroy');
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
requestPermission(): Promise<boolean> {
|
|
246
|
+
return new Promise<boolean>((resolve) => {
|
|
247
|
+
abilityAccessCtrl.createAtManager()
|
|
248
|
+
.requestPermissionsFromUser(this.ctx.uiAbilityContext, PERMISSIONS).then(result => {
|
|
249
|
+
if (result.authResults[0] === 0) {
|
|
250
|
+
resolve(true);
|
|
251
|
+
} else {
|
|
252
|
+
logger.debug(`getString,text out:用户拒绝授权`);
|
|
253
|
+
resolve(false);
|
|
254
|
+
}
|
|
255
|
+
}).catch(() => {
|
|
256
|
+
logger.debug(`getString,text out:用户拒绝授权`);
|
|
257
|
+
resolve(false);
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
261
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
export {}
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export {}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This code was generated by "react-native codegen-lib-harmony"
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
export * from "./ts"
|
|
1
|
+
/**
|
|
2
|
+
* This code was generated by "react-native codegen-lib-harmony"
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export * from "./ts"
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This code was generated by "react-native codegen-lib-harmony"
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
export * as RNC from "./components/ts"
|
|
6
|
-
export * as TM from "./turboModules/ts"
|
|
1
|
+
/**
|
|
2
|
+
* This code was generated by "react-native codegen-lib-harmony"
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export * as RNC from "./components/ts"
|
|
6
|
+
export * as TM from "./turboModules/ts"
|
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This code was generated by "react-native codegen-lib-harmony"
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { Tag } from "@rnoh/react-native-openharmony/ts"
|
|
6
|
-
|
|
7
|
-
export namespace RCTAudioPlayer {
|
|
8
|
-
export const NAME = 'RCTAudioPlayer' as const
|
|
9
|
-
|
|
10
|
-
export enum PlaybackCategories {
|
|
11
|
-
Playback = 1,
|
|
12
|
-
Ambient = 2,
|
|
13
|
-
SoloAmbient = 3,
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export type
|
|
17
|
-
|
|
18
|
-
export type
|
|
19
|
-
|
|
20
|
-
export type
|
|
21
|
-
|
|
22
|
-
export interface Spec {
|
|
23
|
-
set(playerId: number, option: setOptions, next: () => void): void;
|
|
24
|
-
|
|
25
|
-
prepare(playerId: number, path: string, option: Option, next: () => void): void;
|
|
26
|
-
|
|
27
|
-
play(playerId: number, next: () => void): void;
|
|
28
|
-
|
|
29
|
-
pause(): void;
|
|
30
|
-
|
|
31
|
-
stop(): void;
|
|
32
|
-
|
|
33
|
-
destroy(content: string): void;
|
|
34
|
-
|
|
35
|
-
seek(): void;
|
|
36
|
-
|
|
37
|
-
getCurrentTime(playerId: number, callback: (err: string, result: PlayInfo) => void): void;
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* This code was generated by "react-native codegen-lib-harmony"
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { Tag } from "@rnoh/react-native-openharmony/ts"
|
|
6
|
+
|
|
7
|
+
export namespace RCTAudioPlayer {
|
|
8
|
+
export const NAME = 'RCTAudioPlayer' as const
|
|
9
|
+
|
|
10
|
+
export enum PlaybackCategories {
|
|
11
|
+
Playback = 1,
|
|
12
|
+
Ambient = 2,
|
|
13
|
+
SoloAmbient = 3,
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type setOptions = {volume: string, pan: string, wakeLock: string, looping: boolean, speed: boolean}
|
|
17
|
+
|
|
18
|
+
export type Option = {autoDestroy: boolean, continuesToPlayInBackground: boolean, category: PlaybackCategories, mixWithOthers: boolean}
|
|
19
|
+
|
|
20
|
+
export type PlayInfo = {duration: number, position: number}
|
|
21
|
+
|
|
22
|
+
export interface Spec {
|
|
23
|
+
set(playerId: number, option: setOptions, next: () => void): void;
|
|
24
|
+
|
|
25
|
+
prepare(playerId: number, path: string, option: Option, next: () => void): void;
|
|
26
|
+
|
|
27
|
+
play(playerId: number, next: () => void): void;
|
|
28
|
+
|
|
29
|
+
pause(): void;
|
|
30
|
+
|
|
31
|
+
stop(): void;
|
|
32
|
+
|
|
33
|
+
destroy(content: string): void;
|
|
34
|
+
|
|
35
|
+
seek(): void;
|
|
36
|
+
|
|
37
|
+
getCurrentTime(playerId: number, callback: (err: string, result: PlayInfo) => void): void;
|
|
38
|
+
|
|
39
|
+
}
|
|
40
|
+
}
|