@react-native-ohos/react-native-audio-recorder-player 3.6.11-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.OpenSource +11 -0
- package/README.md +14 -0
- package/RNAudioRecorderPlayer.podspec +21 -0
- package/harmony/audio_recorder_player/LICENSE +21 -0
- package/harmony/audio_recorder_player/ResourceTable.txt +2 -0
- package/harmony/audio_recorder_player/build-profile.json5 +28 -0
- package/harmony/audio_recorder_player/hvigorfile.ts +1 -0
- package/harmony/audio_recorder_player/index.ets +26 -0
- package/harmony/audio_recorder_player/oh-package.json5 +20 -0
- package/harmony/audio_recorder_player/src/main/cpp/CMakeLists.txt +7 -0
- package/harmony/audio_recorder_player/src/main/cpp/RNAudioRecorderPlayerPackage.h +53 -0
- package/harmony/audio_recorder_player/src/main/cpp/RNAudioRecorderPlayerTurboModule.cpp +93 -0
- package/harmony/audio_recorder_player/src/main/cpp/RNAudioRecorderPlayerTurboModule.h +38 -0
- package/harmony/audio_recorder_player/src/main/ets/RNAudioRecorderPlayerPackage.ets +48 -0
- package/harmony/audio_recorder_player/src/main/ets/RNAudioRecorderPlayerTurboModule.ts +303 -0
- package/harmony/audio_recorder_player/src/main/ets/audioPlayer/AudioPlayer.ts +414 -0
- package/harmony/audio_recorder_player/src/main/ets/audioPlayer/types.ts +43 -0
- package/harmony/audio_recorder_player/src/main/ets/index.d.ts +209 -0
- package/harmony/audio_recorder_player/src/main/ets/recorder/Recorder.ts +299 -0
- package/harmony/audio_recorder_player/src/main/ets/recorder/types.ts +61 -0
- package/harmony/audio_recorder_player/src/main/ets/utils/DateTimeUtils.ts +88 -0
- package/harmony/audio_recorder_player/src/main/ets/utils/Logger.ts +45 -0
- package/harmony/audio_recorder_player/src/main/ets/utils/SaveAudioAsset.ts +105 -0
- package/harmony/audio_recorder_player/src/main/module.json5 +9 -0
- package/harmony/audio_recorder_player/src/main/resources/base/element/string.json +8 -0
- package/harmony/audio_recorder_player/src/main/resources/base/media/ic_public_error.svg +10 -0
- package/harmony/audio_recorder_player/src/main/resources/en_US/element/string.json +8 -0
- package/harmony/audio_recorder_player/src/main/resources/zh_CN/element/string.json +8 -0
- package/harmony/audio_recorder_player/ts.ets +27 -0
- package/harmony/audio_recorder_player.har +0 -0
- package/index.d.ts +226 -0
- package/index.ts +524 -0
- package/package.json +69 -0
- package/src/NativeAudioRecorderPlayerModule.ts +42 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
|
|
3
|
+
* Use of this source code is governed by a MIT license that can be
|
|
4
|
+
* found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export const TAG: string = 'Audio_Player';
|
|
8
|
+
|
|
9
|
+
export const playBackName: string = 'rn-playback';
|
|
10
|
+
|
|
11
|
+
export enum EventTarget {
|
|
12
|
+
durationUpdate = 'durationUpdate',
|
|
13
|
+
timeUpdate = 'timeUpdate',
|
|
14
|
+
stateChange = 'stateChange',
|
|
15
|
+
error = 'error',
|
|
16
|
+
seekDone = 'seekDone',
|
|
17
|
+
volumeChange = 'volumeChange',
|
|
18
|
+
seekToPlayer = 'seekToPlayer',
|
|
19
|
+
setVolume = 'setVolume',
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export enum AVPlayerState {
|
|
23
|
+
idle = 'idle',
|
|
24
|
+
initialized = 'initialized',
|
|
25
|
+
prepared = 'prepared',
|
|
26
|
+
playing = 'playing',
|
|
27
|
+
paused = 'paused',
|
|
28
|
+
completed = 'completed',
|
|
29
|
+
stopped = 'stopped',
|
|
30
|
+
released = 'released',
|
|
31
|
+
error = 'error',
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export enum processValue {
|
|
35
|
+
play = 'playing',
|
|
36
|
+
resume = 'Already playing',
|
|
37
|
+
stop = 'Already stopped playing',
|
|
38
|
+
pause = 'Already paused playing',
|
|
39
|
+
noPlaying = 'No audio playing',
|
|
40
|
+
stateError = 'current state is not allowed',
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const pathStartWith: Array<string> = ['fd://', 'http://', 'https://'];
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (C) 2023 Huawei Device Co., Ltd.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export enum AudioSourceHarmonyType {
|
|
26
|
+
DEFAULT = 0,
|
|
27
|
+
MIC,
|
|
28
|
+
VOICE_RECOGNITION,
|
|
29
|
+
VOICE_COMMUNICATION = 7,
|
|
30
|
+
VOICE_MESSAGE = 10,
|
|
31
|
+
CAMCORDER = 12,
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export enum AudioFormatHarmonyType {
|
|
35
|
+
MPEG_4 = 'mp4',
|
|
36
|
+
MPEG_4A = 'm4a',
|
|
37
|
+
MP3 = 'mp3',
|
|
38
|
+
WAV = 'wav'
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export enum AudioMimeHarmonyType {
|
|
42
|
+
VIDEO_H263 = 'video/h263', // 表示视频/h263类型。
|
|
43
|
+
VIDEO_AVC = 'video/avc', // 表示视频/avc类型。
|
|
44
|
+
VIDEO_MPEG2 = 'video/mpeg2', // 表示视频/mpeg2类型。
|
|
45
|
+
VIDEO_MPEG4 = 'video/mp4v-es', // 表示视频/mpeg4类型。
|
|
46
|
+
VIDEO_VP8 = 'video/x-vnd.on2.vp8', // 表示视频/vp8类型。
|
|
47
|
+
VIDEO_HEVC = 'video/hevc', // 表示视频/H265类型。
|
|
48
|
+
AUDIO_AAC = 'audio/mp4a-latm', // 表示音频/mp4a-latm类型。
|
|
49
|
+
|
|
50
|
+
AUDIO_VORBIS = 'audio/vorbis', // 表示音频/vorbis类型。
|
|
51
|
+
AUDIO_FLAC = 'audio/flac', // 表示音频/flac类型。
|
|
52
|
+
AUDIO_MP3 = 'audio/mpeg', // 表示音频/mpeg类型。
|
|
53
|
+
AUDIO_G711MU = 'audio/g711mu', // 表示音频/G711-mulaw类型。
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export enum AudioSourceAndroidType {
|
|
57
|
+
DEFAULT = 0,
|
|
58
|
+
MIC,
|
|
59
|
+
VOICE_UPLINK,
|
|
60
|
+
VOICE_DOWNLINK,
|
|
61
|
+
VOICE_CALL,
|
|
62
|
+
CAMCORDER,
|
|
63
|
+
VOICE_RECOGNITION,
|
|
64
|
+
VOICE_COMMUNICATION,
|
|
65
|
+
REMOTE_SUBMIX,
|
|
66
|
+
UNPROCESSED,
|
|
67
|
+
RADIO_TUNER = 1998,
|
|
68
|
+
HOTWORD,
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export enum OutputFormatAndroidType {
|
|
72
|
+
DEFAULT = 0,
|
|
73
|
+
THREE_GPP,
|
|
74
|
+
MPEG_4,
|
|
75
|
+
AMR_NB,
|
|
76
|
+
AMR_WB,
|
|
77
|
+
AAC_ADIF,
|
|
78
|
+
AAC_ADTS,
|
|
79
|
+
OUTPUT_FORMAT_RTP_AVP,
|
|
80
|
+
MPEG_2_TS,
|
|
81
|
+
WEBM,
|
|
82
|
+
UNUSED,
|
|
83
|
+
OGG,
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export enum AudioEncoderAndroidType {
|
|
87
|
+
DEFAULT = 0,
|
|
88
|
+
AMR_NB,
|
|
89
|
+
AMR_WB,
|
|
90
|
+
AAC,
|
|
91
|
+
HE_AAC,
|
|
92
|
+
AAC_ELD,
|
|
93
|
+
VORBIS,
|
|
94
|
+
OPUS,
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export enum AVEncodingOption {
|
|
98
|
+
lpcm = 'lpcm',
|
|
99
|
+
ima4 = 'ima4',
|
|
100
|
+
aac = 'aac',
|
|
101
|
+
MAC3 = 'MAC3',
|
|
102
|
+
MAC6 = 'MAC6',
|
|
103
|
+
ulaw = 'ulaw',
|
|
104
|
+
alaw = 'alaw',
|
|
105
|
+
mp1 = 'mp1',
|
|
106
|
+
mp2 = 'mp2',
|
|
107
|
+
mp4 = 'mp4',
|
|
108
|
+
alac = 'alac',
|
|
109
|
+
amr = 'amr',
|
|
110
|
+
flac = 'flac',
|
|
111
|
+
opus = 'opus',
|
|
112
|
+
wav = 'wav',
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
type AVEncodingType =
|
|
116
|
+
| AVEncodingOption.lpcm
|
|
117
|
+
| AVEncodingOption.ima4
|
|
118
|
+
| AVEncodingOption.aac
|
|
119
|
+
| AVEncodingOption.MAC3
|
|
120
|
+
| AVEncodingOption.MAC6
|
|
121
|
+
| AVEncodingOption.ulaw
|
|
122
|
+
| AVEncodingOption.alaw
|
|
123
|
+
| AVEncodingOption.mp1
|
|
124
|
+
| AVEncodingOption.mp2
|
|
125
|
+
| AVEncodingOption.mp4
|
|
126
|
+
| AVEncodingOption.alac
|
|
127
|
+
| AVEncodingOption.amr
|
|
128
|
+
| AVEncodingOption.flac
|
|
129
|
+
| AVEncodingOption.opus
|
|
130
|
+
| AVEncodingOption.wav;
|
|
131
|
+
|
|
132
|
+
export enum AVModeIOSOption {
|
|
133
|
+
gamechat = 'gamechat',
|
|
134
|
+
measurement = 'measurement',
|
|
135
|
+
movieplayback = 'movieplayback',
|
|
136
|
+
spokenaudio = 'spokenaudio',
|
|
137
|
+
videochat = 'videochat',
|
|
138
|
+
videorecording = 'videorecording',
|
|
139
|
+
voicechat = 'voicechat',
|
|
140
|
+
voiceprompt = 'voiceprompt',
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export type AVModeIOSType =
|
|
144
|
+
| AVModeIOSOption.gamechat
|
|
145
|
+
| AVModeIOSOption.measurement
|
|
146
|
+
| AVModeIOSOption.movieplayback
|
|
147
|
+
| AVModeIOSOption.spokenaudio
|
|
148
|
+
| AVModeIOSOption.videochat
|
|
149
|
+
| AVModeIOSOption.videorecording
|
|
150
|
+
| AVModeIOSOption.voicechat
|
|
151
|
+
| AVModeIOSOption.voiceprompt;
|
|
152
|
+
|
|
153
|
+
export enum AVEncoderAudioQualityIOSType {
|
|
154
|
+
min = 0,
|
|
155
|
+
low = 32,
|
|
156
|
+
medium = 64,
|
|
157
|
+
high = 96,
|
|
158
|
+
max = 127,
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export enum AVLinearPCMBitDepthKeyIOSType {
|
|
162
|
+
'bit8' = 8,
|
|
163
|
+
'bit16' = 16,
|
|
164
|
+
'bit24' = 24,
|
|
165
|
+
'bit32' = 32,
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export interface AudioSet {
|
|
169
|
+
AVSampleRateKeyIOS?: number;
|
|
170
|
+
AVFormatIDKeyIOS?: AVEncodingType;
|
|
171
|
+
AVModeIOS?: AVModeIOSType;
|
|
172
|
+
AVNumberOfChannelsKeyIOS?: number;
|
|
173
|
+
AVEncoderAudioQualityKeyIOS?: AVEncoderAudioQualityIOSType;
|
|
174
|
+
AudioSourceAndroid?: AudioSourceAndroidType;
|
|
175
|
+
AVLinearPCMBitDepthKeyIOS?: AVLinearPCMBitDepthKeyIOSType;
|
|
176
|
+
AVLinearPCMIsBigEndianKeyIOS?: boolean;
|
|
177
|
+
AVLinearPCMIsFloatKeyIOS?: boolean;
|
|
178
|
+
AVLinearPCMIsNonInterleavedIOS?: boolean;
|
|
179
|
+
AVEncoderBitRateKeyIOS?: number;
|
|
180
|
+
OutputFormatAndroid?: OutputFormatAndroidType;
|
|
181
|
+
AudioEncoderAndroid?: AudioEncoderAndroidType;
|
|
182
|
+
AudioEncodingBitRateAndroid?: number;
|
|
183
|
+
AudioSamplingRateAndroid?: number;
|
|
184
|
+
AudioChannelsAndroid?: number;
|
|
185
|
+
AudioSourceHarmony?: AudioSourceHarmonyType;
|
|
186
|
+
AudioMimeHarmony?: AudioMimeHarmonyType;
|
|
187
|
+
AudioFileFormatHarmony?: AudioFormatHarmonyType;
|
|
188
|
+
AudioEncodingBitRateHarmony?: number;
|
|
189
|
+
AudioSamplingRateHarmony?: number;
|
|
190
|
+
AudioChannelsHarmony?: number;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export type RecordBackType = {
|
|
194
|
+
isRecording?: boolean;
|
|
195
|
+
currentPosition: number;
|
|
196
|
+
currentMetering?: number;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
export type PlayBackType = {
|
|
200
|
+
isMuted?: boolean;
|
|
201
|
+
currentPosition: number;
|
|
202
|
+
duration: number;
|
|
203
|
+
isFinished: boolean;
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
export enum harmonyEventName {
|
|
207
|
+
recordBack = 'rn-recordback',
|
|
208
|
+
playBack = 'rn-playback'
|
|
209
|
+
}
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
|
|
3
|
+
* Use of this source code is governed by a MIT license that can be
|
|
4
|
+
* found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import media from '@ohos.multimedia.media';
|
|
8
|
+
import { BusinessError } from '@kit.BasicServicesKit';
|
|
9
|
+
import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl';
|
|
10
|
+
import { common } from '@kit.AbilityKit';
|
|
11
|
+
import { avConfig, avProfile, AVRecorderState, defaultTimeOut, fdPath, PERMISSIONS, processValue, TAG, } from './types';
|
|
12
|
+
import Logger from '../utils/Logger';
|
|
13
|
+
import SaveAudioAsset from '../utils/SaveAudioAsset';
|
|
14
|
+
|
|
15
|
+
export default class Recorder {
|
|
16
|
+
public isRecording: boolean = false;
|
|
17
|
+
public milliseconds: number = 0;
|
|
18
|
+
public recorderState: AVRecorderState;
|
|
19
|
+
private audioRecorder: media.AVRecorder;
|
|
20
|
+
private fdPath: string;
|
|
21
|
+
private timer;
|
|
22
|
+
private mSaveAudioAsset: SaveAudioAsset;
|
|
23
|
+
private permissions: Array<Permissions> = [
|
|
24
|
+
PERMISSIONS.MICROPHONE,
|
|
25
|
+
PERMISSIONS.READ_MEDIA,
|
|
26
|
+
PERMISSIONS.WRITE_MEDIA,
|
|
27
|
+
];
|
|
28
|
+
private atManager: abilityAccessCtrl.AtManager =
|
|
29
|
+
abilityAccessCtrl.createAtManager();
|
|
30
|
+
private mFileAssetId: number;
|
|
31
|
+
private avProfile: media.AVRecorderProfile;
|
|
32
|
+
private avConfig: media.AVRecorderConfig;
|
|
33
|
+
private startRecordCallback: () => void;
|
|
34
|
+
private context: common.UIAbilityContext;
|
|
35
|
+
|
|
36
|
+
constructor(context: common.UIAbilityContext) {
|
|
37
|
+
this.context = context;
|
|
38
|
+
this.mSaveAudioAsset = new SaveAudioAsset(TAG, context);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public setAVProfile(newValue: media.AVRecorderProfile, audioSource?: media.AudioSourceType) {
|
|
42
|
+
this.avProfile = {
|
|
43
|
+
...avProfile,
|
|
44
|
+
...newValue,
|
|
45
|
+
};
|
|
46
|
+
if (audioSource) {
|
|
47
|
+
avConfig.audioSourceType = audioSource;
|
|
48
|
+
}
|
|
49
|
+
this.avConfig = {
|
|
50
|
+
...avConfig,
|
|
51
|
+
profile: this.avProfile,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public onEvent(callback: () => void) {
|
|
56
|
+
this.startRecordCallback = callback;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// start button process
|
|
60
|
+
async startRecordingProcess(url?: string): Promise<string> {
|
|
61
|
+
Logger.debug(TAG, 'startRecording called', this.recorderState);
|
|
62
|
+
if (this.isRecording) {
|
|
63
|
+
Logger.debug(TAG, 'audioRecorder exist,release it');
|
|
64
|
+
return processValue.stateError;
|
|
65
|
+
}
|
|
66
|
+
const isPermissions: boolean = await this.requestPermissions();
|
|
67
|
+
if (isPermissions) {
|
|
68
|
+
await this.getFileFd(url);
|
|
69
|
+
await this.createAudioRecorder();
|
|
70
|
+
await this.prepareAudioRecorder();
|
|
71
|
+
Logger.debug(TAG, 'startRecording done');
|
|
72
|
+
return this.mSaveAudioAsset.getFilePath();
|
|
73
|
+
}
|
|
74
|
+
return processValue.NoPermissions;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// pause button process
|
|
78
|
+
async pauseRecordingProcess(): Promise<string | undefined> {
|
|
79
|
+
Logger.debug(TAG, 'pauseRecording called');
|
|
80
|
+
if (this.recorderState === AVRecorderState.started) {
|
|
81
|
+
Logger.debug(TAG, 'current state is started, to pause');
|
|
82
|
+
return this.audioRecorder.pause().then(() => processValue.pause);
|
|
83
|
+
}
|
|
84
|
+
Logger.debug(TAG, 'pauseRecording done');
|
|
85
|
+
return processValue.stateError;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// resume button process
|
|
89
|
+
async resumeRecordingProcess(): Promise<string | undefined> {
|
|
90
|
+
Logger.debug(TAG, 'resumeRecording called');
|
|
91
|
+
if (this.recorderState === AVRecorderState.paused) {
|
|
92
|
+
Logger.debug(TAG, 'current state is paused, to resume');
|
|
93
|
+
this.isRecording = true;
|
|
94
|
+
return this.audioRecorder.resume().then(() => processValue.resume);
|
|
95
|
+
}
|
|
96
|
+
Logger.debug(TAG, 'resumeRecording done');
|
|
97
|
+
return processValue.stateError;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// stop button process
|
|
101
|
+
async stopRecordingProcess(): Promise<string | undefined> {
|
|
102
|
+
Logger.debug(TAG, 'stopRecording called');
|
|
103
|
+
clearInterval(this.timer);
|
|
104
|
+
this.isRecording = false;
|
|
105
|
+
this.milliseconds = 0;
|
|
106
|
+
await this.audioRecorder.stop();
|
|
107
|
+
Logger.debug(TAG, 'stopRecording stop');
|
|
108
|
+
await this.closeFd();
|
|
109
|
+
Logger.debug(TAG, 'stopRecording closeFd');
|
|
110
|
+
await this.resetAudioRecording();
|
|
111
|
+
Logger.debug(TAG, 'stopRecording reset');
|
|
112
|
+
await this.releaseAudioRecorder();
|
|
113
|
+
Logger.debug(TAG, 'stopRecording release');
|
|
114
|
+
return this.getFilePath();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
private async catchCallback(error: BusinessError): Promise<void> {
|
|
118
|
+
Logger.debug(
|
|
119
|
+
TAG,
|
|
120
|
+
`catchCallback code:${error.code} message:${error.message}`,
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
private async requestPermissions(): Promise<boolean> {
|
|
125
|
+
return await new Promise((resolve: Function) => {
|
|
126
|
+
try {
|
|
127
|
+
const context = this.context as common.UIAbilityContext;
|
|
128
|
+
this.atManager
|
|
129
|
+
.requestPermissionsFromUser(context, this.permissions)
|
|
130
|
+
.then(async () => {
|
|
131
|
+
resolve(true);
|
|
132
|
+
})
|
|
133
|
+
.catch(() => {
|
|
134
|
+
resolve(false);
|
|
135
|
+
});
|
|
136
|
+
} catch (err) {
|
|
137
|
+
resolve(false);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// show recording time
|
|
143
|
+
private getRecordTime(): void {
|
|
144
|
+
this.timer = setInterval(() => {
|
|
145
|
+
this.milliseconds += defaultTimeOut;
|
|
146
|
+
}, defaultTimeOut);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// create file fd
|
|
150
|
+
private async getFileFd(url?: string): Promise<void> {
|
|
151
|
+
Logger.debug(TAG, 'getFileFd called');
|
|
152
|
+
return new Promise(async (resolve, reject) => {
|
|
153
|
+
if (url && url.startsWith(fdPath)) {
|
|
154
|
+
this.avConfig.url = url;
|
|
155
|
+
Logger.debug(TAG, 'url is :', url);
|
|
156
|
+
resolve();
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
this.mFileAssetId = await this.mSaveAudioAsset.createAudioFd(url,
|
|
160
|
+
this.avConfig?.profile?.fileFormat || media.ContainerFormatType.CFT_MPEG_4A);
|
|
161
|
+
if (!this.mFileAssetId) {
|
|
162
|
+
reject();
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
this.fdPath = fdPath + this.mFileAssetId.toString();
|
|
166
|
+
this.avConfig.url = this.fdPath;
|
|
167
|
+
Logger.debug(TAG, 'fdPath is: ' + this.fdPath);
|
|
168
|
+
Logger.debug(TAG, 'getFileFd done');
|
|
169
|
+
resolve();
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
private async createAudioRecorder(): Promise<void> {
|
|
174
|
+
await media
|
|
175
|
+
.createAVRecorder()
|
|
176
|
+
.then((recorder) => {
|
|
177
|
+
Logger.debug(TAG, 'case createAVRecorder called');
|
|
178
|
+
if (!!recorder) {
|
|
179
|
+
this.audioRecorder = recorder;
|
|
180
|
+
this.setCallback();
|
|
181
|
+
} else {
|
|
182
|
+
Logger.debug(TAG, 'case create avRecorder failed!!!');
|
|
183
|
+
}
|
|
184
|
+
})
|
|
185
|
+
.catch(this.catchCallback);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// set callback on
|
|
189
|
+
private setCallback(): void {
|
|
190
|
+
Logger.debug(TAG, 'case callback');
|
|
191
|
+
this.audioRecorder!.on(
|
|
192
|
+
AVRecorderState.onStateChange,
|
|
193
|
+
(state: media.AVRecorderState) => {
|
|
194
|
+
Logger.debug(TAG, 'case state has changed, new state is' + state);
|
|
195
|
+
switch (state) {
|
|
196
|
+
case AVRecorderState.idle: {
|
|
197
|
+
this.recorderState = AVRecorderState.idle;
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
case AVRecorderState.prepared: {
|
|
201
|
+
this.recorderState = AVRecorderState.prepared;
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
case AVRecorderState.started: {
|
|
205
|
+
this.recorderState = AVRecorderState.started;
|
|
206
|
+
if (this.startRecordCallback) {
|
|
207
|
+
this.startRecordCallback();
|
|
208
|
+
}
|
|
209
|
+
this.getRecordTime();
|
|
210
|
+
this.isRecording = true;
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
case AVRecorderState.paused: {
|
|
214
|
+
this.recorderState = AVRecorderState.paused;
|
|
215
|
+
this.isRecording = false;
|
|
216
|
+
clearInterval(this.timer);
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
case AVRecorderState.stopped: {
|
|
220
|
+
this.recorderState = AVRecorderState.stopped;
|
|
221
|
+
this.isRecording = false;
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
case AVRecorderState.released: {
|
|
225
|
+
this.recorderState = AVRecorderState.released;
|
|
226
|
+
this.isRecording = false;
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
default:
|
|
230
|
+
Logger.debug(TAG, 'case start is unknown');
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
},
|
|
234
|
+
);
|
|
235
|
+
this.audioRecorder!.on(AVRecorderState.error, (err) => {
|
|
236
|
+
this.stopRecordingProcess();
|
|
237
|
+
Logger.debug(
|
|
238
|
+
TAG,
|
|
239
|
+
'case avRecorder.on(error) called, errMessage is ' + err.code,
|
|
240
|
+
);
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
private async prepareAudioRecorder(): Promise<void> {
|
|
245
|
+
Logger.debug(TAG, 'case prepareAudioRecorder in');
|
|
246
|
+
await this.audioRecorder!.prepare(this.avConfig)
|
|
247
|
+
.then(() => {
|
|
248
|
+
this.audioRecorder!.start();
|
|
249
|
+
Logger.debug(TAG, 'case prepare AVRecorder called');
|
|
250
|
+
}, this.catchCallback)
|
|
251
|
+
.catch(this.catchCallback);
|
|
252
|
+
Logger.debug(TAG, 'case prepareAudioRecorder out');
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
private async resetAudioRecording(): Promise<void> {
|
|
256
|
+
await this.audioRecorder!.reset()
|
|
257
|
+
.then(() => {
|
|
258
|
+
Logger.debug(TAG, 'case resetAudioRecording called');
|
|
259
|
+
}, this.catchCallback)
|
|
260
|
+
.catch(this.catchCallback);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
private async releaseAudioRecorder(): Promise<void> {
|
|
264
|
+
if (this.audioRecorder) {
|
|
265
|
+
await this.audioRecorder.off(AVRecorderState.onStateChange);
|
|
266
|
+
await this.audioRecorder.off(AVRecorderState.error);
|
|
267
|
+
this.audioRecorder
|
|
268
|
+
.release()
|
|
269
|
+
.then(() => {
|
|
270
|
+
this.audioRecorder = undefined;
|
|
271
|
+
Logger.debug(TAG, 'case releaseAudioRecorder called');
|
|
272
|
+
}, this.catchCallback)
|
|
273
|
+
.catch(this.catchCallback);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// close file fd
|
|
278
|
+
private async closeFd(): Promise<void> {
|
|
279
|
+
Logger.debug(TAG, 'case closeFd called');
|
|
280
|
+
if (this.fdPath) {
|
|
281
|
+
this.mSaveAudioAsset.closeFile();
|
|
282
|
+
this.mFileAssetId = -1;
|
|
283
|
+
this.fdPath = '';
|
|
284
|
+
Logger.debug(TAG, 'case closeFd done');
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
private getFilePath(): string {
|
|
289
|
+
const context = this.context as common.UIAbilityContext;
|
|
290
|
+
return `${context.filesDir}/${this.mSaveAudioAsset.getFileName()}`;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
public getMaxAmplitude(): Promise<number> {
|
|
294
|
+
if (!this.audioRecorder) {
|
|
295
|
+
return new Promise((resolve) => resolve(0));
|
|
296
|
+
}
|
|
297
|
+
return this.audioRecorder.getAudioCapturerMaxAmplitude();
|
|
298
|
+
}
|
|
299
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
|
|
3
|
+
* Use of this source code is governed by a MIT license that can be
|
|
4
|
+
* found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import media from '@ohos.multimedia.media';
|
|
8
|
+
|
|
9
|
+
export const defaultBitrate: number = 48000;
|
|
10
|
+
|
|
11
|
+
export const defaultSampleRate: number = 48000;
|
|
12
|
+
|
|
13
|
+
export const defaultChannels: number = 2;
|
|
14
|
+
|
|
15
|
+
export const defaultTimeOut: number = 200;
|
|
16
|
+
|
|
17
|
+
export const TAG: string = 'Audio_Recorder';
|
|
18
|
+
|
|
19
|
+
export const RecordBackName: string = 'rn-recordback';
|
|
20
|
+
|
|
21
|
+
export const fdPath: string = 'fd://';
|
|
22
|
+
|
|
23
|
+
export enum PERMISSIONS {
|
|
24
|
+
MICROPHONE = 'ohos.permission.MICROPHONE',
|
|
25
|
+
WRITE_MEDIA = 'ohos.permission.WRITE_MEDIA',
|
|
26
|
+
READ_MEDIA = 'ohos.permission.READ_MEDIA',
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const avProfile: media.AVRecorderProfile = {
|
|
30
|
+
audioBitrate: defaultBitrate, // set audioBitrate according to device ability
|
|
31
|
+
audioChannels: defaultChannels, // set audioChannels, valid value 1-8
|
|
32
|
+
audioCodec: media.CodecMimeType.AUDIO_AAC, // set audioCodec, AUDIO_AAC is the only choice
|
|
33
|
+
audioSampleRate: defaultSampleRate, // set audioSampleRate according to device ability
|
|
34
|
+
fileFormat: media.ContainerFormatType.CFT_MPEG_4A, // set fileFormat, for video is m4a
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export const avConfig: media.AVRecorderConfig = {
|
|
38
|
+
audioSourceType: media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC,
|
|
39
|
+
profile: avProfile,
|
|
40
|
+
url: fdPath,
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export enum AVRecorderState {
|
|
44
|
+
idle = 'idle',
|
|
45
|
+
prepared = 'prepared',
|
|
46
|
+
started = 'started',
|
|
47
|
+
paused = 'paused',
|
|
48
|
+
stopped = 'stopped',
|
|
49
|
+
released = 'released',
|
|
50
|
+
error = 'error',
|
|
51
|
+
onStateChange = 'stateChange',
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export enum processValue {
|
|
55
|
+
start = 'Already recording.',
|
|
56
|
+
pause = 'Already paused recording.',
|
|
57
|
+
resume = 'Currently recording.',
|
|
58
|
+
stop = 'Already stopped',
|
|
59
|
+
NoPermissions = 'No permissions of recorder',
|
|
60
|
+
stateError = 'current state is not allowed',
|
|
61
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2023 Huawei Device Co., Ltd.
|
|
3
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License.
|
|
5
|
+
* You may obtain a copy of the License at
|
|
6
|
+
*
|
|
7
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
* See the License for the specific language governing permissions and
|
|
13
|
+
* limitations under the License.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export default class DateTimeUtil {
|
|
17
|
+
getTime(): string {
|
|
18
|
+
const DATETIME = new Date();
|
|
19
|
+
return this.concatTime(
|
|
20
|
+
DATETIME.getHours(),
|
|
21
|
+
DATETIME.getMinutes(),
|
|
22
|
+
DATETIME.getSeconds(),
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
getDate(): string {
|
|
27
|
+
const DATETIME = new Date();
|
|
28
|
+
return this.concatDate(
|
|
29
|
+
DATETIME.getFullYear(),
|
|
30
|
+
DATETIME.getMonth() + 1,
|
|
31
|
+
DATETIME.getDate(),
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
fill(value: number): string {
|
|
36
|
+
return (value > 9 ? '' : '0') + value;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
concatDate(year: number, month: number, date: number): string {
|
|
40
|
+
return `${year}${month}${date}`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
concatTime(hour: number, minute: number, second: number): string {
|
|
44
|
+
return `${this.fill(hour)}${this.fill(minute)}${this.fill(second)}`;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function getShownTimer(ms: number): string {
|
|
49
|
+
let seconds: number = Math.round(ms / 1000);
|
|
50
|
+
let sec: number | string = seconds % 60;
|
|
51
|
+
let min: number | string = (seconds - sec) / 60;
|
|
52
|
+
if (sec < 10) {
|
|
53
|
+
sec = '0' + sec;
|
|
54
|
+
}
|
|
55
|
+
if (min < 10) {
|
|
56
|
+
min = '0' + min;
|
|
57
|
+
}
|
|
58
|
+
return min + ':' + sec;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function dateTime(t: number): string {
|
|
62
|
+
let minute: number = Math.floor(t / 60) % 60;
|
|
63
|
+
let m = minute < 10 ? '0' + minute : minute;
|
|
64
|
+
let second: number = t % 60;
|
|
65
|
+
let s = second < 10 ? '0' + second : second;
|
|
66
|
+
return m + ':' + s;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function getMMSSSS(milisecs: number): string {
|
|
70
|
+
const secs = Math.floor(milisecs / 1000);
|
|
71
|
+
const minutes = Math.floor(secs / 60);
|
|
72
|
+
const seconds = secs % 60;
|
|
73
|
+
const miliseconds = Math.floor((milisecs % 1000) / 10);
|
|
74
|
+
return `${formatString(minutes)}:${formatString(
|
|
75
|
+
seconds,
|
|
76
|
+
)}:${formatString(miliseconds)}`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function getMMSS(milisecs: number): string {
|
|
80
|
+
const secs = Math.floor(milisecs / 1000);
|
|
81
|
+
const minutes = Math.floor(secs / 60);
|
|
82
|
+
const seconds = secs % 60;
|
|
83
|
+
return `${formatString(minutes)}:${formatString(seconds)}`;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function formatString(num: number): string {
|
|
87
|
+
return `0${num}`.slice(-2);
|
|
88
|
+
}
|