未验证 提交 6bada599 编写于 作者: O openharmony_ci 提交者: Gitee

!5372 翻译完成:4024 audio文档新增状态机

Merge pull request !5372 from wusongqing/TR4024
# Audio Capture Development # Audio Capture Development
--- ## When to Use
## ***Note***:
1. This document applies to JavaScript. You can use the APIs provided by **AudioCapturer** to record raw audio files.
---
## **Summary** ### State Check
This guide will show how a JS application can use AudioCapturer to record the audio.
Applications can use the APIs provided in this document to record raw audio files. 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.
## **AudioCapturer Framework** For details about the APIs, see [AudioCapturer in Audio Management](../reference/apis/js-apis-audio.md).
The AudioCapturer interface is one of the most important components of the audio framework.
### **Audio Capturing:** **Figure 1** Audio capturer state
The AudioCapturer framework provides APIs for capturing raw audio files.
![](figures/audio-capturer-state.png)
## **Usage**
Here's an example of how to use AudioCapturer to capture a raw audio file. ## How to Develop
1. Use **createAudioCapturer()** to create an AudioCapturer instance. Capturer parameters can be set in **audioCapturerOptions**.\
This instance can be used to record, control, and obtain the recording status, as well as to register a callback for notifications. 1. Use **createAudioCapturer()** to create an **AudioCapturer** instance.
```
var audioStreamInfo = { 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.
samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100,
channels: audio.AudioChannel.CHANNEL_1,
sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE,
encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW
}
var audioCapturerInfo = {
source: audio.SourceType.SOURCE_TYPE_MIC,
capturerFlags: 1
}
var audioCapturerOptions = {
streamInfo: audioStreamInfo,
capturerInfo: audioCapturerInfo
}
let audioCapturer = await audio.createAudioCapturer(audioCapturerOptions);
var state = audioRenderer.state;
```
2. (Optional) Subscribe to audio capturer state change events using the **on('stateChange')** API. ```js
If an application wants to take some action based on the state updates in capturer, the application can subscribe to the state change event. var audioStreamInfo = {
There are more events that applications can subscribe to, such as 'markReach' and 'periodReach'. Refer to [Audio](../reference/apis/js-apis-audio.md) for more details. samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100,
channels: audio.AudioChannel.CHANNEL_1,
sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE,
encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW
}
var audioCapturerInfo = {
source: audio.SourceType.SOURCE_TYPE_MIC,
capturerFlags: 1
}
var audioCapturerOptions = {
streamInfo: audioStreamInfo,
capturerInfo: audioCapturerInfo
}
let audioCapturer = await audio.createAudioCapturer(audioCapturerOptions);
var state = audioRenderer.state;
``` ```
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
audioCapturer.on('stateChange',(state) => { audioCapturer.on('stateChange',(state) => {
console.info('AudioCapturerLog: Changed State to : ' + state) console.info('AudioCapturerLog: Changed State to : ' + state)
switch (state) { switch (state) {
case audio.AudioState.STATE_PREPARED: case audio.AudioState.STATE_PREPARED:
console.info('--------CHANGE IN AUDIO STATE----------PREPARED--------------'); console.info('--------CHANGE IN AUDIO STATE----------PREPARED--------------');
console.info('Audio State is : Prepared'); console.info('Audio State is : Prepared');
break; break;
case audio.AudioState.STATE_RUNNING: case audio.AudioState.STATE_RUNNING:
console.info('--------CHANGE IN AUDIO STATE----------RUNNING--------------'); console.info('--------CHANGE IN AUDIO STATE----------RUNNING--------------');
console.info('Audio State is : Running'); console.info('Audio State is : Running');
break; break;
case audio.AudioState.STATE_STOPPED: case audio.AudioState.STATE_STOPPED:
console.info('--------CHANGE IN AUDIO STATE----------STOPPED--------------'); console.info('--------CHANGE IN AUDIO STATE----------STOPPED--------------');
console.info('Audio State is : stopped'); console.info('Audio State is : stopped');
break; break;
case audio.AudioState.STATE_RELEASED: case audio.AudioState.STATE_RELEASED:
console.info('--------CHANGE IN AUDIO STATE----------RELEASED--------------'); console.info('--------CHANGE IN AUDIO STATE----------RELEASED--------------');
console.info('Audio State is : released'); console.info('Audio State is : released');
break; break;
default: default:
console.info('--------CHANGE IN AUDIO STATE----------INVALID--------------'); console.info('--------CHANGE IN AUDIO STATE----------INVALID--------------');
console.info('Audio State is : invalid'); console.info('Audio State is : invalid');
break; break;
} }
}); });
``` ```
3. Call the **start()** function on the AudioCapturer instance to start/resume the recording task.\ 3. Use **start()** to start audio recording.
The capturer state will be STATE_RUNNING once the start is complete. The application can then begin reading buffers.
```
await audioCapturer.start();
if (audioCapturer.state == audio.AudioState.STATE_RUNNING) {
console.info('AudioRecLog: Capturer started');
} else {
console.info('AudioRecLog: Capturer start failed');
}
``` The capturer state will be **STATE_RUNNING** once the audio capturer is started. The application can then begin reading buffers.
4. Obtain the minimum buffer size to read using the **getBufferSize()** API. ```js
await audioCapturer.start();
if (audioCapturer.state == audio.AudioState.STATE_RUNNING) {
console.info('AudioRecLog: Capturer started');
} else {
console.info('AudioRecLog: Capturer start failed');
}
``` ```
var bufferSize = await audioCapturer.getBufferSize();
console.info('AudioRecLog: buffer size: ' + bufferSize);
``` 4. Use **getBufferSize()** to obtain the minimum buffer size to read.
5. Read the captured audio data and convert it to a byte stream. Call the **read()** API repeatedly to read the data ```js
until the application wants to stop the recording. The following example shows how to write recorded data into a file. var bufferSize = await audioCapturer.getBufferSize();
console.info('AudioRecLog: buffer size: ' + bufferSize);
``` ```
import fileio from '@ohos.fileio';
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.
const path = '/data/data/.pulse_dir/capture_js.wav';
let fd = fileio.openSync(path, 0o102, 0o777); The following example shows how to write recorded data into a file.
if (fd !== null) {
console.info('AudioRecLog: file fd created'); ```js
} import fileio from '@ohos.fileio';
else{
console.info('AudioRecLog: file fd create : FAILED'); const path = '/data/data/.pulse_dir/capture_js.wav';
return; let fd = fileio.openSync(path, 0o102, 0o777);
} if (fd !== null) {
console.info('AudioRecLog: file fd created');
fd = fileio.openSync(path, 0o2002, 0o666); }
if (fd !== null) { else{
console.info('AudioRecLog: file fd opened in append mode'); console.info('AudioRecLog: file fd create : FAILED');
} return;
}
var numBuffersToCapture = 150;
while (numBuffersToCapture) { fd = fileio.openSync(path, 0o2002, 0o666);
var buffer = await audioCapturer.read(bufferSize, true); if (fd !== null) {
if (typeof(buffer) == undefined) { console.info('AudioRecLog: file fd opened in append mode');
console.info('read buffer failed'); }
} else {
var number = fileio.writeSync(fd, buffer); var numBuffersToCapture = 150;
console.info('AudioRecLog: data written: ' + number); while (numBuffersToCapture) {
} var buffer = await audioCapturer.read(bufferSize, true);
if (typeof(buffer) == undefined) {
numBuffersToCapture--; console.info('read buffer failed');
} } else {
``` var number = fileio.writeSync(fd, buffer);
6. Once the recording is complete, call the **stop()** API on the AudioCapturer instance to stop the recording. console.info('AudioRecLog: data written: ' + number);
``` }
await audioCapturer.stop();
if (audioCapturer.state == audio.AudioState.STATE_STOPPED) { numBuffersToCapture--;
console.info('AudioRecLog: Capturer stopped'); }
} else {
console.info('AudioRecLog: Capturer stop failed');
}
``` ```
7. After the recording task is complete, call the **release()** API on the AudioCapturer instance to release the stream resources. 6. Once the recording is complete, call **stop()** to stop the recording.
``` ```
await audioCapturer.release(); await audioCapturer.stop();
if (audioCapturer.state == audio.AudioState.STATE_RELEASED) { if (audioCapturer.state == audio.AudioState.STATE_STOPPED) {
console.info('AudioRecLog: Capturer released'); console.info('AudioRecLog: Capturer stopped');
} else { } else {
console.info('AudioRecLog: Capturer release failed'); console.info('AudioRecLog: Capturer stop failed');
} }
``` ```
## **Importance of State Check** 7. After the task is complete, call **release()** to release related resources.
Application developers should keep in mind that an AudioCapturer is state-based.
That is, the AudioCapturer has an internal state that the application must always check when calling recorder control APIs, because some operations are only acceptable while the capturer is in a given state.\
The system may throw an error/exception or generate other undefined behaviour if the application performs an operation while capturer is in an improper state.
## **Other APIs** ```js
See [AudioCapturer in the Audio API](../reference/apis/js-apis-audio.md) for more useful APIs like **getAudioTime**, **getCapturerInfo** and **getStreamInfo**. await audioCapturer.release();
if (audioCapturer.state == audio.AudioState.STATE_RELEASED) {
console.info('AudioRecLog: Capturer released');
} else {
console.info('AudioRecLog: Capturer release failed');
}
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册