Skip to content

Commit e169598

Browse files
zhangmengjiaZhangMeng
andauthored
release:@react-native-oh-library/react-native-audio-toolkit@2.0.3-0.0.4 (#14)
Co-authored-by: ZhangMeng <n030236@archermind.com>
1 parent 796987d commit e169598

File tree

7 files changed

+51
-35
lines changed

7 files changed

+51
-35
lines changed

harmony/audio_toolkit.har

130 Bytes
Binary file not shown.

harmony/audio_toolkit/oh-package.json5

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"types": "",
44
"devDependencies": {},
55
"name": "@react-native-community/audio-toolkit",
6-
"version": "2.0.3-0.0.3",
6+
"version": "2.0.3-0.0.4",
77
"description": "",
88
"main": "index.ets",
99
"dependencies": {

harmony/audio_toolkit/src/main/ets/AudioToolkitPackage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { RNPackage, TurboModulesFactory } from '@rnoh/react-native-openharmony/t
2626
import type { TurboModule, TurboModuleContext } from '@rnoh/react-native-openharmony/ts';
2727
import { RCTAudioPlayerTurboModule } from './RNCAudioPlayerTurboModule';
2828
import { RCTAudioRecorderTurboModule } from './RNCAudioRecorderTurboModule';
29-
export const AUDIO_TYPE: string = "RCTAudioPlayer1"
29+
export const AUDIO_TYPE: string = 'RCTAudioPlayer1';
3030

3131
class AudioPlayer1TurboModulesFactory extends TurboModulesFactory {
3232
createTurboModule(name: string): TurboModule | null {

harmony/audio_toolkit/src/main/ets/RNCAudioRecorderTurboModule.ts

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ const PERMISSIONS: Array<Permissions> = [
3737
'ohos.permission.MICROPHONE',
3838
];
3939

40-
interface Error {
41-
err?: string;
42-
message?: string;
40+
enum FormatType {
41+
CFT_MPEG_4 = 'mp4',
42+
CFT_MPEG_4A = 'm4a'
4343
}
4444

4545
export class RCTAudioRecorderTurboModule extends TurboModule {
@@ -141,12 +141,13 @@ export class RCTAudioRecorderTurboModule extends TurboModule {
141141
this.avRecorder = await media.createAVRecorder();
142142
this.setAudioRecorderCallback(recorderId);
143143
this.avProfile = {
144-
audioBitrate: option.bitRate ?? this.AUDIOBITRATE_DEFAULT, // 音频比特率
144+
audioBitrate: option.bitrate ?? this.AUDIOBITRATE_DEFAULT, // 音频比特率
145145
audioChannels: option.channels ?? this.AUDIOCHANNELS_DEFAULT, // 音频声道数
146146
audioCodec: media.CodecMimeType.AUDIO_AAC, // 音频编码格式,当前只支持aac
147147
audioSampleRate: option.sampleRate ?? this.AUDIOSAMPLERATE, // 音频采样率
148-
fileFormat: media.ContainerFormatType.CFT_MPEG_4A, // 封装格式,当前只支持m4a
148+
fileFormat: this.getFileFormat(option.format, path), // 封装格式,当前只支持m4a、mp4
149149
};
150+
logger.debug(`avProfile = ${JSON.stringify(this.avProfile)}}`);
150151
this.avConfig = {
151152
audioSourceType: media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC, // 音频输入源,这里设置为麦克风
152153
profile: this.avProfile,
@@ -155,6 +156,29 @@ export class RCTAudioRecorderTurboModule extends TurboModule {
155156
await this.startRecordingProcess(path, next, preparingCall);
156157
}
157158

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+
158182
async record(recorderId: number, next: (object?) => void): Promise<void> {
159183
if (this.avRecorder !== undefined) {
160184
this.avRecorder.resume();
@@ -198,46 +222,38 @@ export class RCTAudioRecorderTurboModule extends TurboModule {
198222
}
199223

200224
/**
201-
* (iOS Only)
202-
* Set content of base64 image type. You can use following code to set clipboard content
203-
* ```javascript
204-
* _setContent() {
205-
* Clipboard.setImage(...);
206-
* }
207-
* ```
208-
* @param the content to be stored in the clipboard.
225+
* recorder destroy
226+
* @param recorderId
227+
* @param next
228+
* @returns void
209229
*/
210230
async destroy(recorderId: number, next: (object?) => void): Promise<void> {
211-
if (this.avRecorder !== undefined) {
212-
if (this.avRecorder.state === 'stopped') {
213-
// 2.重置
214-
await this.avRecorder.reset();
215-
// 3.释放录制实例
216-
await this.avRecorder.release();
217-
218-
// 4.关闭录制文件fd
219-
fs.close(this._file);
220-
this.toEmit(recorderId, 'info', {
221-
message: 'Destroyed recorder',
222-
});
223-
}
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+
});
224241
}
225242
logger.debug(TAG, 'RCTAudioRecorderTurboModule destroy');
226243
}
227244

228-
229245
requestPermission(): Promise<boolean> {
230246
return new Promise<boolean>((resolve) => {
231247
abilityAccessCtrl.createAtManager()
232248
.requestPermissionsFromUser(this.ctx.uiAbilityContext, PERMISSIONS).then(result => {
233249
if (result.authResults[0] === 0) {
234250
resolve(true);
235251
} else {
236-
logger.debug(TAG, `getString,text out:用户拒绝授权`);
252+
logger.debug(`getString,text out:用户拒绝授权`);
237253
resolve(false);
238254
}
239255
}).catch(() => {
240-
logger.debug(TAG, `getString,text out:用户拒绝授权`);
256+
logger.debug(`getString,text out:用户拒绝授权`);
241257
resolve(false);
242258
});
243259
});

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@react-native-oh-tpl/audio-toolkit",
3-
"version": "2.0.3-0.0.3",
3+
"version": "2.0.3-0.0.4",
44
"description": "Cross-platform audio library for React Native",
55
"main": "./src/index.ts",
66
"types": "typings/index.d.ts",

src/RecorderModule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface RecorderOptions {
1111
/**
1212
* Set bitrate for the recorder, in bits per second (Default: 128000)
1313
*/
14-
bitRate: number;
14+
bitrate: number;
1515

1616
/**
1717
* Set number of channels (Default: 2)

0 commit comments

Comments
 (0)