@@ -21,7 +21,7 @@ This following figure shows the audio capturer state transitions.
## Constraints
Before developing the audio data collection feature, configure the **ohos.permission.MICROPHONE** permission for your application. For details about permission configuration, see [Permission Application Guide](../security/accesstoken-guidelines.md).
Before developing the audio data collection feature, configure the **ohos.permission.MICROPHONE** permission for your application. For details, see [Permission Application Guide](../security/accesstoken-guidelines.md).
## How to Develop
...
...
@@ -72,7 +72,7 @@ For details about the APIs, see [AudioCapturer in Audio Management](../reference
}
awaitaudioCapturer.start();
letstate=audioCapturer.state;
state=audioCapturer.state;
if(state==audio.AudioState.STATE_RUNNING){
console.info('AudioRecLog: Capturer started');
}else{
...
...
@@ -86,7 +86,7 @@ For details about the APIs, see [AudioCapturer in Audio Management](../reference
The following example shows how to write recorded data into a file.
```js
importfileiofrom'@ohos.fileio';
importfsfrom'@ohos.file.fs';
letstate=audioCapturer.state;
// The read operation can be performed only when the state is STATE_RUNNING.
...
...
@@ -96,31 +96,36 @@ For details about the APIs, see [AudioCapturer in Audio Management](../reference
}
constpath='/data/data/.pulse_dir/capture_js.wav';// Path for storing the collected audio file.
@@ -34,8 +34,7 @@ For details about the APIs, see [AudioRenderer in Audio Management](../reference
1. Use **createAudioRenderer()** to create an **AudioRenderer** instance.
Set parameters of the **AudioRenderer** instance in **audioRendererOptions**. This instance is used to render audio, control and obtain the rendering status, and register a callback for notification.
Set parameters of the **AudioRenderer** instance in **audioRendererOptions**. This instance is used to render audio, control and obtain the rendering status, and register a callback for notification.
```js
importaudiofrom'@ohos.multimedia.audio';
...
...
@@ -82,15 +81,15 @@ Set parameters of the **AudioRenderer** instance in **audioRendererOptions**. Th
}
}
```
The renderer state will be **STATE_RUNNING** once the audio renderer is started. The application can then begin reading buffers.
The renderer state will be **STATE_RUNNING** once the audio renderer is started. The application can then begin reading buffers.
3. Call **write()** to write data to the buffer.
Read the audio data to be played to the buffer. Call **write()** repeatedly to write the data to the buffer.
```js
importfileiofrom'@ohos.fileio';
importfsfrom'@ohos.file.fs';
importaudiofrom'@ohos.multimedia.audio';
asyncfunctionwriteBuffer(buf){
...
...
@@ -109,35 +108,33 @@ Set parameters of the **AudioRenderer** instance in **audioRendererOptions**. Th
// Set a proper buffer size for the audio renderer. You can also select a buffer of another size.
letdir=globalThis.fileDir;// You must use the sandbox path.
constpath=dir+'/file_example_WAV_2MG.wav';// The file to render is in the following path: /data/storage/el2/base/haps/entry/files/file_example_WAV_2MG.wav
console.info(`file path: ${path}`);
letss=fileio.createStreamSync(path,'r');
consttotalSize=fileio.statSync(path).size;// Size of the file to render.
letdiscardHeader=newArrayBuffer(bufferSize);
ss.readSync(discardHeader);
letrlen=0;
rlen+=bufferSize;
letid=setInterval(()=>{
if(audioRenderer.state==audio.AudioState.STATE_RELEASED){// The rendering stops if the audio renderer is in the STATE_RELEASED state.
if(rlen>=totalSize){// The rendering stops if the file finishes reading.
ss.closeSync();
awaitaudioRenderer.stop();
clearInterval(id);
}
constfilePath=dir+'/file_example_WAV_2MG.wav';// The file to render is in the following path: /data/storage/el2/base/haps/entry/files/file_example_WAV_2MG.wav
@@ -424,35 +421,31 @@ Set parameters of the **AudioRenderer** instance in **audioRendererOptions**. Th
letdir=globalThis.fileDir;// You must use the sandbox path.
constpath1=dir+'/music001_48000_32_1.wav';// The file to render is in the following path: /data/storage/el2/base/haps/entry/files/music001_48000_32_1.wav
console.info(`audioRender1 file path: ${path1}`);
letss1=awaitfileio.createStream(path1,'r');
consttotalSize1=fileio.statSync(path1).size;// Size of the file to render.
@@ -499,36 +492,32 @@ Set parameters of the **AudioRenderer** instance in **audioRendererOptions**. Th
// 2.6 Read the original audio data file.
letdir=globalThis.fileDir;// You must use the sandbox path.
constpath2=dir+'/music002_48000_32_1.wav';// The file to render is in the following path: /data/storage/el2/base/haps/entry/files/music002_48000_32_1.wav