• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Deprecated Interface (AudioRecorder, deprecated)
2<!--Kit: Media Kit-->
3<!--Subsystem: Multimedia-->
4<!--Owner: @wang-haizhou6-->
5<!--Designer: @HmQQQ-->
6<!--Tester: @xchaosioda-->
7<!--Adviser: @zengyawen-->
8
9> **NOTE**
10>
11> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVRecorder](arkts-apis-media-AVRecorder.md) instead.
12
13AudioRecorder is a class for audio recording management. It provides APIs to record audio. Before calling any API in AudioRecorder, you must use [createAudioRecorder()](arkts-apis-media-f.md#mediacreateaudiorecorderdeprecated) to create an AudioRecorder instance.
14
15## Modules to Import
16
17```ts
18import { media } from '@kit.MediaKit';
19```
20
21## prepare<sup>(deprecated)</sup>
22
23prepare(config: AudioRecorderConfig): void
24
25Prepares for recording.
26
27> **NOTE**
28>
29> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVRecorder.prepare](arkts-apis-media-AVRecorder.md#prepare9) instead.
30
31**Required permissions:** ohos.permission.MICROPHONE
32
33**System capability**: SystemCapability.Multimedia.Media.AudioRecorder
34
35**Parameters**
36
37| Name| Type                                       | Mandatory| Description                                                        |
38| ------ | ------------------------------------------- | ---- | ------------------------------------------------------------ |
39| config | [AudioRecorderConfig](arkts-apis-media-i.md#audiorecorderconfigdeprecated) | Yes  | Audio recording parameters, including the audio output URI, encoding format, sample rate, audio channel count, and output format.|
40
41**Error codes**
42
43For details about the error codes, see [Media Error Codes](errorcode-media.md).
44
45| ID| Error Message             |
46| -------- | --------------------- |
47| 201      | permission denied     |
48
49**Example**
50
51```ts
52let audioRecorderConfig: media.AudioRecorderConfig = {
53  audioEncoder : media.AudioEncoder.AAC_LC,
54  audioEncodeBitRate : 64000,
55  audioSampleRate : 44100,
56  numberOfChannels : 2,
57  format : media.AudioOutputFormat.AAC_ADTS,
58  uri : 'fd://1',       // The file must be created by the caller and granted with proper permissions.
59  location : { latitude : 30, longitude : 130},
60};
61audioRecorder.on('prepare', () => {    // Set the 'prepare' event callback.
62  console.info('prepare called');
63});
64audioRecorder.prepare(audioRecorderConfig);
65```
66
67## start<sup>(deprecated)</sup>
68
69start(): void
70
71Starts audio recording. This API can be called only after the **'prepare'** event is triggered.
72
73> **NOTE**
74>
75> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVRecorder.start](arkts-apis-media-AVRecorder.md#start9) instead.
76
77**System capability**: SystemCapability.Multimedia.Media.AudioRecorder
78
79**Example**
80
81```ts
82audioRecorder.on('start', () => {    // Set the 'start' event callback.
83  console.info('audio recorder start called');
84});
85audioRecorder.start();
86```
87
88## pause<sup>(deprecated)</sup>
89
90pause():void
91
92Pauses audio recording. This API can be called only after the **'start'** event is triggered.
93
94> **NOTE**
95>
96> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVRecorder.pause](arkts-apis-media-AVRecorder.md#pause9) instead.
97
98**System capability**: SystemCapability.Multimedia.Media.AudioRecorder
99
100**Example**
101
102```ts
103audioRecorder.on('pause', () => {    // Set the 'pause' event callback.
104  console.info('audio recorder pause called');
105});
106audioRecorder.pause();
107```
108
109## resume<sup>(deprecated)</sup>
110
111resume():void
112
113Resumes audio recording. This API can be called only after the **'pause'** event is triggered.
114
115> **NOTE**
116>
117> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVRecorder.resume](arkts-apis-media-AVRecorder.md#resume9) instead.
118
119**System capability**: SystemCapability.Multimedia.Media.AudioRecorder
120
121**Example**
122
123```ts
124audioRecorder.on('resume', () => { // Set the 'resume' event callback.
125  console.info('audio recorder resume called');
126});
127audioRecorder.resume();
128```
129
130## stop<sup>(deprecated)</sup>
131
132stop(): void
133
134Stops audio recording.
135
136> **NOTE**
137>
138> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVRecorder.stop](arkts-apis-media-AVRecorder.md#stop9) instead.
139
140**System capability**: SystemCapability.Multimedia.Media.AudioRecorder
141
142**Example**
143
144```ts
145audioRecorder.on('stop', () => {    // Set the 'stop' event callback.
146  console.info('audio recorder stop called');
147});
148audioRecorder.stop();
149```
150
151## release<sup>(deprecated)</sup>
152
153release(): void
154
155Releases the audio recording resources.
156
157> **NOTE**
158>
159> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVRecorder.release](arkts-apis-media-AVRecorder.md#release9) instead.
160
161**System capability**: SystemCapability.Multimedia.Media.AudioRecorder
162
163**Example**
164
165```ts
166audioRecorder.on('release', () => {    // Set the 'release' event callback.
167  console.info('audio recorder release called');
168});
169audioRecorder.release();
170audioRecorder = undefined;
171```
172
173## reset<sup>(deprecated)</sup>
174
175reset(): void
176
177Resets audio recording.
178
179Before resetting audio recording, you must call **stop()** to stop recording. After audio recording is reset, you must call **prepare()** to set the recording configurations for another recording.
180
181> **NOTE**
182>
183> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVRecorder.reset](arkts-apis-media-AVRecorder.md#reset9) instead.
184
185**System capability**: SystemCapability.Multimedia.Media.AudioRecorder
186
187**Example**
188
189```ts
190audioRecorder.on('reset', () => {    // Set the 'reset' event callback.
191  console.info('audio recorder reset called');
192});
193audioRecorder.reset();
194```
195
196## on('prepare' | 'start' | 'pause' | 'resume' | 'stop' | 'release' | 'reset')<sup>(deprecated)</sup>
197
198on(type: 'prepare' | 'start' | 'pause' | 'resume' | 'stop' | 'release' | 'reset', callback: () => void): void
199
200Subscribes to the audio recording events.
201
202> **NOTE**
203>
204> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVRecorder.on('stateChange')](arkts-apis-media-AVRecorder.md#onstatechange9) instead.
205
206**System capability**: SystemCapability.Multimedia.Media.AudioRecorder
207
208**Parameters**
209
210| Name  | Type    | Mandatory| Description                                                        |
211| -------- | -------- | ---- | ------------------------------------------------------------ |
212| type     | string   | Yes  | Event type. The following events are supported: 'prepare'\|'start'\|  'pause' \| 'resume' \|'stop'\|'release'\|'reset'<br>- 'prepare': triggered when the **prepare()** API is called and the audio recording parameters are set.<br>- 'start': triggered when the **start()** API is called and audio recording starts.<br>- 'pause': triggered when the **pause()** API is called and audio recording is paused.<br>- 'resume': triggered when the **resume()** API is called and audio recording is resumed.<br>- 'stop': triggered when the **stop()** API is called and audio recording stops.<br>- 'release': triggered when the **release()** API is called and the recording resources are released.<br>- 'reset': triggered when the **reset()** API is called and audio recording is reset.|
213| callback | ()=>void | Yes  | Callback invoked when the event is triggered.                                          |
214
215**Example**
216
217```ts
218import { BusinessError } from '@kit.BasicServicesKit';
219
220let audioRecorder: media.AudioRecorder = media.createAudioRecorder(); // Create an AudioRecorder instance.
221let audioRecorderConfig: media.AudioRecorderConfig = {
222  audioEncoder : media.AudioEncoder.AAC_LC,
223  audioEncodeBitRate : 64000,
224  audioSampleRate : 44100,
225  numberOfChannels : 2,
226  format : media.AudioOutputFormat.AAC_ADTS,
227  uri : 'fd://xx',  // The file must be created by the caller and granted with proper permissions.
228  location : { latitude : 30, longitude : 130}
229};
230audioRecorder.on('error', (error: BusinessError) => {  // Set the 'error' event callback.
231  console.error(`audio error called, error: ${error}`);
232});
233audioRecorder.on('prepare', () => {  // Set the 'prepare' event callback.
234  console.info('prepare called');
235  audioRecorder.start();  // // Start recording and trigger the 'start' event callback.
236});
237audioRecorder.on('start', () => {  // Set the 'start' event callback.
238  console.info('audio recorder start called');
239});
240audioRecorder.on('pause', () => {  // Set the 'pause' event callback.
241  console.info('audio recorder pause called');
242});
243audioRecorder.on('resume', () => {  // Set the 'resume' event callback.
244  console.info('audio recorder resume called');
245});
246audioRecorder.on('stop', () => {  // Set the 'stop' event callback.
247  console.info('audio recorder stop called');
248});
249audioRecorder.on('release', () => {  // Set the 'release' event callback.
250  console.info('audio recorder release called');
251});
252audioRecorder.on('reset', () => {  // Set the 'reset' event callback.
253  console.info('audio recorder reset called');
254});
255audioRecorder.prepare(audioRecorderConfig)  // // Set recording parameters and trigger the 'prepare' event callback.
256```
257
258## on('error')<sup>(deprecated)</sup>
259
260on(type: 'error', callback: ErrorCallback): void
261
262Subscribes to audio recording error events. After an error event is reported, you must handle the event and exit the recording.
263
264> **NOTE**
265>
266> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [AVRecorder.on('error')](arkts-apis-media-AVRecorder.md#onerror9) instead.
267
268**System capability**: SystemCapability.Multimedia.Media.AudioRecorder
269
270**Parameters**
271
272| Name  | Type         | Mandatory| Description                                                        |
273| -------- | ------------- | ---- | ------------------------------------------------------------ |
274| type     | string        | Yes  | Event type, which is **'error'** in this case.<br>This event is triggered when an error occurs during audio recording.|
275| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback) | Yes  | Callback invoked when the event is triggered.                                      |
276
277**Example**
278
279```ts
280import { BusinessError } from '@kit.BasicServicesKit';
281
282let audioRecorderConfig: media.AudioRecorderConfig = {
283  audioEncoder : media.AudioEncoder.AAC_LC,
284  audioEncodeBitRate : 22050,
285  audioSampleRate : 22050,
286  numberOfChannels : 2,
287  format : media.AudioOutputFormat.AAC_ADTS,
288  uri : 'fd://xx',   // The file must be created by the caller and granted with proper permissions.
289  location : { latitude : 30, longitude : 130}
290};
291audioRecorder.on('error', (error: BusinessError) => {  // Set the 'error' event callback.
292  console.error(`audio error called, error: ${error}`);
293});
294audioRecorder.prepare(audioRecorderConfig);  // // Do not set any parameter in prepare and trigger the 'error' event callback.
295```
296