audio-capturer.md 5.4 KB
Newer Older
W
wusongqing 已提交
1
# Audio Capture Development
G
Geevarghese V K 已提交
2

W
wusongqing 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
## When to Use

You can use the APIs provided by **AudioCapturer** to record raw audio files.  

### State Check

During 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.

For details about the APIs, see [AudioCapturer in Audio Management](../reference/apis/js-apis-audio.md).

**Figure 1** Audio capturer state

![](figures/audio-capturer-state.png)

## How to Develop

1. Use **createAudioCapturer()** to create an **AudioCapturer** instance.

   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.
G
Geevarghese V K 已提交
22

W
wusongqing 已提交
23 24
   ```js
      var audioStreamInfo = {
W
wusongqing 已提交
25 26 27 28 29
        samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100,
        channels: audio.AudioChannel.CHANNEL_1,
        sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE,
        encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW
      }
W
wusongqing 已提交
30
      
W
wusongqing 已提交
31 32 33 34
      var audioCapturerInfo = {
        source: audio.SourceType.SOURCE_TYPE_MIC,
        capturerFlags: 1
      }
W
wusongqing 已提交
35
      
W
wusongqing 已提交
36 37 38 39
      var audioCapturerOptions = {
        streamInfo: audioStreamInfo,
        capturerInfo: audioCapturerInfo
      }
W
wusongqing 已提交
40
      
W
wusongqing 已提交
41 42
      let audioCapturer = await audio.createAudioCapturer(audioCapturerOptions);
      var state = audioRenderer.state;
G
Geevarghese V K 已提交
43
   ```
W
wusongqing 已提交
44 45 46 47 48

2. (Optional) Use **on('stateChange')** to subscribe to audio renderer state changes.
If 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).

   ```js
G
Geevarghese V K 已提交
49 50 51
    audioCapturer.on('stateChange',(state) => {
    console.info('AudioCapturerLog: Changed State to : ' + state)
    switch (state) {
W
wusongqing 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
      case audio.AudioState.STATE_PREPARED:
        console.info('--------CHANGE IN AUDIO STATE----------PREPARED--------------');
        console.info('Audio State is : Prepared');
        break;
      case audio.AudioState.STATE_RUNNING:
        console.info('--------CHANGE IN AUDIO STATE----------RUNNING--------------');
        console.info('Audio State is : Running');
        break;
      case audio.AudioState.STATE_STOPPED:
        console.info('--------CHANGE IN AUDIO STATE----------STOPPED--------------');
        console.info('Audio State is : stopped');
        break;
      case audio.AudioState.STATE_RELEASED:
        console.info('--------CHANGE IN AUDIO STATE----------RELEASED--------------');
        console.info('Audio State is : released');
        break;
      default:
        console.info('--------CHANGE IN AUDIO STATE----------INVALID--------------');
        console.info('Audio State is : invalid');
        break;
W
wusongqing 已提交
72
     }
G
Geevarghese V K 已提交
73 74 75
    });
   ```

W
wusongqing 已提交
76
3. Use **start()** to start audio recording.
G
Geevarghese V K 已提交
77

W
wusongqing 已提交
78
   The capturer state will be **STATE_RUNNING** once the audio capturer is started. The application can then begin reading buffers.
G
Geevarghese V K 已提交
79

W
wusongqing 已提交
80 81 82
   ```js
   await audioCapturer.start();
   if (audioCapturer.state == audio.AudioState.STATE_RUNNING) {
W
wusongqing 已提交
83
     console.info('AudioRecLog: Capturer started');
W
wusongqing 已提交
84
   } else {
W
wusongqing 已提交
85
     console.info('AudioRecLog: Capturer start failed');
W
wusongqing 已提交
86
   }
G
Geevarghese V K 已提交
87 88
   ```

W
wusongqing 已提交
89
4. Use **getBufferSize()** to obtain the minimum buffer size to read.
G
Geevarghese V K 已提交
90

W
wusongqing 已提交
91 92 93
   ```js
   var bufferSize = await audioCapturer.getBufferSize();
   console.info('AudioRecLog: buffer size: ' + bufferSize);
G
Geevarghese V K 已提交
94
   ```
W
wusongqing 已提交
95 96 97 98 99 100 101 102 103 104 105

5. 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.  

   The following example shows how to write recorded data into a file.

   ```js
   import fileio from '@ohos.fileio';
      
   const path = '/data/data/.pulse_dir/capture_js.wav';
   let fd = fileio.openSync(path, 0o102, 0o777);
   if (fd !== null) {
W
wusongqing 已提交
106
     console.info('AudioRecLog: file fd created');
W
wusongqing 已提交
107 108
   }
   else{
W
wusongqing 已提交
109 110
     console.info('AudioRecLog: file fd create : FAILED');
     return;
W
wusongqing 已提交
111 112 113 114
   }
      
   fd = fileio.openSync(path, 0o2002, 0o666);
   if (fd !== null) {
W
wusongqing 已提交
115
     console.info('AudioRecLog: file fd opened in append mode');
W
wusongqing 已提交
116 117 118 119
   }
      
   var numBuffersToCapture = 150;
   while (numBuffersToCapture) {
W
wusongqing 已提交
120 121 122 123 124 125 126
     var buffer = await audioCapturer.read(bufferSize, true);
     if (typeof(buffer) == undefined) {
       console.info('read buffer failed');
     } else {
       var number = fileio.writeSync(fd, buffer);
       console.info('AudioRecLog: data written: ' + number);
     }
W
wusongqing 已提交
127
      
W
wusongqing 已提交
128
     numBuffersToCapture--;
W
wusongqing 已提交
129
   }
G
Geevarghese V K 已提交
130 131
   ```

W
wusongqing 已提交
132 133
6. Once the recording is complete, call **stop()** to stop the recording.

G
Geevarghese V K 已提交
134
   ```
W
wusongqing 已提交
135 136
   await audioCapturer.stop();
   if (audioCapturer.state == audio.AudioState.STATE_STOPPED) {
W
wusongqing 已提交
137
     console.info('AudioRecLog: Capturer stopped');
W
wusongqing 已提交
138
   } else {
W
wusongqing 已提交
139
     console.info('AudioRecLog: Capturer stop failed');
W
wusongqing 已提交
140
   }
G
Geevarghese V K 已提交
141 142
   ```

W
wusongqing 已提交
143
7. After the task is complete, call **release()** to release related resources.
G
Geevarghese V K 已提交
144

W
wusongqing 已提交
145 146 147
   ```js
   await audioCapturer.release();
   if (audioCapturer.state == audio.AudioState.STATE_RELEASED) {
W
wusongqing 已提交
148
     console.info('AudioRecLog: Capturer released');
W
wusongqing 已提交
149
   } else {
W
wusongqing 已提交
150
     console.info('AudioRecLog: Capturer release failed');
W
wusongqing 已提交
151 152
   }
   ```