• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Audio Capture Development
2
3## When to Use
4
5You can use the APIs provided by **AudioCapturer** to record raw audio files.
6
7### State Check
8
9During application development, you are advised to use **on('stateChange')** to subscribe to state changes of the **AudioCapturer** instance. This is because some operations can be performed only when the audio capturer is in a given state. If the application performs an operation when the audio capturer is not in the given state, the system may throw an exception or generate other undefined behavior.
10
11For details about the APIs, see [AudioCapturer in Audio Management](../reference/apis/js-apis-audio.md).
12
13**Figure 1** Audio capturer state
14
15![](figures/audio-capturer-state.png)
16
17## How to Develop
18
191. Use **createAudioCapturer()** to create an **AudioCapturer** instance.
20
21   Set parameters of the **AudioCapturer** instance in **audioCapturerOptions**. This instance is used to capture audio, control and obtain the recording status, and register a callback for notification.
22
23   ```js
24      var audioStreamInfo = {
25           samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100,
26           channels: audio.AudioChannel.CHANNEL_1,
27           sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE,
28           encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW
29       }
30
31       var audioCapturerInfo = {
32           source: audio.SourceType.SOURCE_TYPE_MIC,
33           capturerFlags: 1
34       }
35
36       var audioCapturerOptions = {
37           streamInfo: audioStreamInfo,
38           capturerInfo: audioCapturerInfo
39       }
40
41       let audioCapturer = await audio.createAudioCapturer(audioCapturerOptions);
42       var state = audioRenderer.state;
43   ```
44
452. (Optional) Use **on('stateChange')** to subscribe to audio renderer state changes.
46If an application needs to perform some operations when the audio renderer state is updated, the application can subscribe to the state changes. For more events that can be subscribed to, see [Audio Management](../reference/apis/js-apis-audio.md).
47
48   ```js
49    audioCapturer.on('stateChange',(state) => {
50    console.info('AudioCapturerLog: Changed State to : ' + state)
51    switch (state) {
52     case audio.AudioState.STATE_PREPARED:
53         console.info('--------CHANGE IN AUDIO STATE----------PREPARED--------------');
54         console.info('Audio State is : Prepared');
55         break;
56     case audio.AudioState.STATE_RUNNING:
57         console.info('--------CHANGE IN AUDIO STATE----------RUNNING--------------');
58         console.info('Audio State is : Running');
59         break;
60     case audio.AudioState.STATE_STOPPED:
61         console.info('--------CHANGE IN AUDIO STATE----------STOPPED--------------');
62         console.info('Audio State is : stopped');
63         break;
64     case audio.AudioState.STATE_RELEASED:
65         console.info('--------CHANGE IN AUDIO STATE----------RELEASED--------------');
66         console.info('Audio State is : released');
67         break;
68     default:
69         console.info('--------CHANGE IN AUDIO STATE----------INVALID--------------');
70         console.info('Audio State is : invalid');
71         break;
72     }
73    });
74   ```
75
763. Use **start()** to start audio recording.
77
78   The capturer state will be **STATE_RUNNING** once the audio capturer is started. The application can then begin reading buffers.
79
80   ```js
81   await audioCapturer.start();
82   if (audioCapturer.state == audio.AudioState.STATE_RUNNING) {
83       console.info('AudioRecLog: Capturer started');
84   } else {
85       console.info('AudioRecLog: Capturer start failed');
86   }
87   ```
88
894. Use **getBufferSize()** to obtain the minimum buffer size to read.
90
91   ```js
92   var bufferSize = await audioCapturer.getBufferSize();
93   console.info('AudioRecLog: buffer size: ' + bufferSize);
94   ```
95
965. Read the captured audio data and convert it to a byte stream. Call **read()** repeatedly to read the data until the application wants to stop the recording.
97
98   The following example shows how to write recorded data into a file.
99
100   ```js
101   import fileio from '@ohos.fileio';
102
103   const path = '/data/data/.pulse_dir/capture_js.wav';
104   let fd = fileio.openSync(path, 0o102, 0o777);
105   if (fd !== null) {
106       console.info('AudioRecLog: file fd created');
107   }
108   else{
109       console.info('AudioRecLog: file fd create : FAILED');
110       return;
111   }
112
113   fd = fileio.openSync(path, 0o2002, 0o666);
114   if (fd !== null) {
115       console.info('AudioRecLog: file fd opened in append mode');
116   }
117
118   var numBuffersToCapture = 150;
119   while (numBuffersToCapture) {
120       var buffer = await audioCapturer.read(bufferSize, true);
121       if (typeof(buffer) == undefined) {
122           console.info('read buffer failed');
123       } else {
124           var number = fileio.writeSync(fd, buffer);
125           console.info('AudioRecLog: data written: ' + number);
126       }
127
128       numBuffersToCapture--;
129   }
130   ```
131
1326. Once the recording is complete, call **stop()** to stop the recording.
133
134   ```
135   await audioCapturer.stop();
136   if (audioCapturer.state == audio.AudioState.STATE_STOPPED) {
137       console.info('AudioRecLog: Capturer stopped');
138   } else {
139       console.info('AudioRecLog: Capturer stop failed');
140   }
141   ```
142
1437. After the task is complete, call **release()** to release related resources.
144
145   ```js
146   await audioCapturer.release();
147   if (audioCapturer.state == audio.AudioState.STATE_RELEASED) {
148       console.info('AudioRecLog: Capturer released');
149   } else {
150       console.info('AudioRecLog: Capturer release failed');
151   }
152   ```
153