@@ -27,13 +27,22 @@ Before developing the audio data collection feature, configure the **ohos.permis
For details about the APIs, see [AudioCapturer in Audio Management](../reference/apis/js-apis-audio.md#audiocapturer8).
1. Use **createAudioCapturer()** to create an**AudioCapturer** instance.
1. Use **createAudioCapturer()** to create a global**AudioCapturer** instance.
Set parameters of the **AudioCapturer** instance in **audioCapturerOptions**. This instance is used to capture audio, control and obtain the recording state, and register a callback for notification.
```js
importaudiofrom'@ohos.multimedia.audio';
importfsfrom'@ohos.file.fs';// It will be used for the call of the read function in step 3.
// Perform a self-test on APIs related to audio rendering.
@Entry
@Component
structAudioRenderer{
@Statemessage:string='Hello World'
privateaudioCapturer:audio.AudioCapturer;// It will be called globally.
3. Read the captured audio data and convert it to a byte stream. Call **read()** repeatedly to read the data until the application stops the recording.
...
...
@@ -86,17 +92,15 @@ For details about the APIs, see [AudioCapturer in Audio Management](../reference
The following example shows how to write recorded data into a file.
```js
importfsfrom'@ohos.file.fs';
letstate=audioCapturer.state;
asyncreadData(){
letstate=this.audioCapturer.state;
// The read operation can be performed only when the state is STATE_RUNNING.
if(state!=audio.AudioState.STATE_RUNNING){
console.info('Capturer is not in a correct state to read');
return;
}
constpath='/data/data/.pulse_dir/capture_js.wav';// Path for storing the collected audio file.
letfile=fs.openSync(filePath,0o2);
letfile=fs.openSync(path,0o2);
letfd=file.fd;
if(file!==null){
console.info('AudioRecLog: file created');
...
...
@@ -104,16 +108,14 @@ For details about the APIs, see [AudioCapturer in Audio Management](../reference
7. (Optional) Use **on('markReach')** to subscribe to the mark reached event, and use **off('markReach')** to unsubscribe from the event.
...
...
@@ -202,12 +202,13 @@ For details about the APIs, see [AudioCapturer in Audio Management](../reference
After the mark reached event is subscribed to, when the number of frames collected by the audio capturer reaches the specified value, a callback is triggered and the specified value is returned.
audioCapturer.off('markReach');// Unsubscribe from the mark reached event. This event will no longer be listened for.
this.audioCapturer.off('markReach');// Unsubscribe from the mark reached event. This event will no longer be listened for.
}
```
8. (Optional) Use **on('periodReach')** to subscribe to the period reached event, and use **off('periodReach')** to unsubscribe from the event.
...
...
@@ -215,18 +216,20 @@ For details about the APIs, see [AudioCapturer in Audio Management](../reference
After the period reached event is subscribed to, each time the number of frames collected by the audio capturer reaches the specified value, a callback is triggered and the specified value is returned.
console.info(`In this period, the Capturer reached frame: ${reachNumber}`);
});
audioCapturer.off('periodReach');// Unsubscribe from the period reached event. This event will no longer be listened for.
this.audioCapturer.off('periodReach');// Unsubscribe from the period reached event. This event will no longer be listened for.
}
```
9. If your application needs to perform some operations when the audio capturer state is updated, it can subscribe to the state change event. When the audio capturer state is updated, the application receives a callback containing the event type.
```js
audioCapturer.on('stateChange', (state) => {
async stateChange(){
this.audioCapturer.on('stateChange', (state) => {
console.info(`AudioCapturerLog: Changed State to : ${state}`)
switch (state) {
case audio.AudioState.STATE_PREPARED:
...
...
@@ -251,4 +254,5 @@ For details about the APIs, see [AudioCapturer in Audio Management](../reference
-**PREPARED**: The audio renderer enters this state by calling **create()**.
-**RUNNING**: The audio renderer enters this state by calling **start()** when it is in the **PREPARED** state or by calling **start()** when it is in the **STOPPED** state.
-**PAUSED**: The audio renderer enters this state by calling **pause()** when it is in the **RUNNING** state. When the audio playback is paused, it can call **start()** to resume the playback.
-**STOPPED**: The audio renderer enters this state by calling **stop()** when it is in the **PAUSED** or **RUNNING** state.
-**RELEASED**: The audio renderer enters this state by calling **release()** when it is in the **PREPARED**, **PAUSED**, or **STOPPED** state. In this state, the audio renderer releases all occupied hardware and software resources and will not transit to any other state.
## How to Develop
For details about the APIs, see [AudioRenderer in Audio Management](../reference/apis/js-apis-audio.md#audiorenderer8).
1. Use **createAudioRenderer()** to create an **AudioRenderer** instance.
1. Use **createAudioRenderer()** to create a global **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.
```js
importaudiofrom'@ohos.multimedia.audio';
importfsfrom'@ohos.file.fs';
// Perform a self-test on APIs related to audio rendering.
@Entry
@Component
structAudioRenderer1129{
privateaudioRenderer:audio.AudioRenderer;
privatebufferSize;// It will be used for the call of the write function in step 3.
privateaudioRenderer1:audio.AudioRenderer;// It will be used for the call in the complete example in step 14.
privateaudioRenderer2:audio.AudioRenderer;// It will be used for the call in the complete example in step 14.
@@ -71,9 +78,9 @@ For details about the APIs, see [AudioRenderer in Audio Management](../reference
return;
}
awaitaudioRenderer.start();
awaitthis.audioRenderer.start();
state=audioRenderer.state;
state=this.audioRenderer.state;
if(state==audio.AudioState.STATE_RUNNING){
console.info('Renderer started');
}else{
...
...
@@ -81,39 +88,24 @@ For details about the APIs, see [AudioRenderer in Audio Management](../reference
}
}
```
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.
Read the audio data to be played to the buffer. Call **write()** repeatedly to write the data to the buffer. Import fs from '@ohos.file.fs'; as step 1.
```js
importfsfrom'@ohos.file.fs';
importaudiofrom'@ohos.multimedia.audio';
asyncfunctionwriteBuffer(buf){
// The write operation can be performed only when the state is STATE_RUNNING.
letdir=globalThis.fileDir;// You must use the sandbox path.
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
9. (Optional) Use **on('audioInterrupt')** to subscribe to the audio interruption event, and use **off('audioInterrupt')** to unsubscribe from the event.
...
...
@@ -272,7 +261,8 @@ For details about the APIs, see [AudioRenderer in Audio Management](../reference
It should be noted that the audio interruption event subscription of the **AudioRenderer** module is slightly different from **on('interrupt')** in [AudioManager](../reference/apis/js-apis-audio.md#audiomanager). The **on('interrupt')** and **off('interrupt')** APIs are deprecated since API version 9. In the **AudioRenderer** module, you only need to call **on('audioInterrupt')** to listen for focus change events. When the **AudioRenderer** instance created by the application performs actions such as start, stop, and pause, it requests the focus, which triggers focus transfer and in return enables the related **AudioRenderer** instance to receive a notification through the callback. For instances other than **AudioRenderer**, such as frequency modulation (FM) and voice wakeup, the application does not create an instance. In this case, the application can call **on('interrupt')** in **AudioManager** to receive a focus change notification.
@@ -282,11 +272,11 @@ For details about the APIs, see [AudioRenderer in Audio Management](../reference
switch(interruptEvent.hintType){
// Forcible pausing initiated by the audio framework. To prevent data loss, stop the write operation.
caseaudio.InterruptHint.INTERRUPT_HINT_PAUSE:
isPlay=false;
console.info('isPlay is false');
break;
// Forcible stopping initiated by the audio framework. To prevent data loss, stop the write operation.
caseaudio.InterruptHint.INTERRUPT_HINT_STOP:
isPlay=false;
console.info('isPlay is false');
break;
// Forcible ducking initiated by the audio framework.
caseaudio.InterruptHint.INTERRUPT_HINT_DUCK:
...
...
@@ -299,18 +289,17 @@ For details about the APIs, see [AudioRenderer in Audio Management](../reference
switch(interruptEvent.hintType){
// Notify the application that the rendering starts.
caseaudio.InterruptHint.INTERRUPT_HINT_RESUME:
startRenderer();
this.startRenderer();
break;
// Notify the application that the audio stream is interrupted. The application then determines whether to continue. (In this example, the application pauses the rendering.)
caseaudio.InterruptHint.INTERRUPT_HINT_PAUSE:
isPlay=false;
pauseRenderer();
console.info('isPlay is false');
this.pauseRenderer();
break;
}
}
});
audioRenderer.off('audioInterrupt');// Unsubscribe from the audio interruption event. This event will no longer be listened for.
}
```
10. (Optional) Use **on('markReach')** to subscribe to the mark reached event, and use **off('markReach')** to unsubscribe from the event.
...
...
@@ -318,12 +307,14 @@ For details about the APIs, see [AudioRenderer in Audio Management](../reference
After the mark reached event is subscribed to, when the number of frames rendered by the audio renderer reaches the specified value, a callback is triggered and the specified value is returned.
audioRenderer.off('markReach');// Unsubscribe from the mark reached event. This event will no longer be listened for.
this.audioRenderer.off('markReach'); // Unsubscribe from the mark reached event. This event will no longer be listened for.
}
```
11. (Optional) Use **on('periodReach')** to subscribe to the period reached event, and use **off('periodReach')** to unsubscribe from the event.
...
...
@@ -331,12 +322,13 @@ For details about the APIs, see [AudioRenderer in Audio Management](../reference
After the period reached event is subscribed to, each time the number of frames rendered by the audio renderer reaches the specified value, a callback is triggered and the specified value is returned.
console.info(`In this period, the renderer reached frame: ${reachNumber} `);
});
audioRenderer.off('periodReach');// Unsubscribe from the period reached event. This event will no longer be listened for.
this.audioRenderer.off('periodReach'); // Unsubscribe from the period reached event. This event will no longer be listened for.
}
```
12. (Optional) Use **on('stateChange')** to subscribe to audio renderer state changes.
...
...
@@ -344,10 +336,12 @@ For details about the APIs, see [AudioRenderer in Audio Management](../reference
After the **stateChange** event is subscribed to, when the audio renderer state changes, a callback is triggered and the audio renderer state is returned.
console.info(`Current renderer state is: ${audioState}`);
});
}
```
13. (Optional) Handle exceptions of **on()**.
...
...
@@ -355,24 +349,25 @@ For details about the APIs, see [AudioRenderer in Audio Management](../reference
If the string or the parameter type passed in **on()** is incorrect , the application throws an exception. In this case, you can use **try catch** to capture the exception.
```js
async errorCall(){
try {
audioRenderer.on('invalidInput',()=>{// The string is invalid.
this.audioRenderer.on('invalidInput', () => { // The string is invalid.
})
} catch (err) {
console.info(`Call on function error, ${err}`); // The application throws exception 401.
}
try {
audioRenderer.on(1,()=>{// The type of the input parameter is incorrect.
this.audioRenderer.on(1, () => { // The type of the input parameter is incorrect.
})
} catch (err) {
console.info(`Call on function error, ${err}`); // The application throws exception 6800101.
}
}
```
14. (Optional) Refer to the complete example of **on('audioInterrupt')**.
Declare audioRenderer1 and audioRenderer2 first. For details, see step 1.
Create **AudioRender1** and **AudioRender2** in an application, configure the independent interruption mode, and call **on('audioInterrupt')** to subscribe to audio interruption events. At the beginning, **AudioRender1** has the focus. When **AudioRender2** attempts to obtain the focus, **AudioRender1** receives a focus transfer notification and the related log information is printed. If the shared mode is used, the log information will not be printed during application running.
```js
asyncrunningAudioRender1(){
letaudioStreamInfo={
...
...
@@ -392,27 +387,27 @@ For details about the APIs, see [AudioRenderer in Audio Management](../reference
| LOCAL_NETWORK_ID<sup>9+</sup> | string | Yes | No | Network ID of the local device.<br>This is a system API.<br>**System capability**: SystemCapability.Multimedia.Audio.Device |
| DEFAULT_VOLUME_GROUP_ID<sup>9+</sup> | number | Yes | No | Default volume group ID.<br>**System capability**: SystemCapability.Multimedia.Audio.Volume |
| DEFAULT_INTERRUPT_GROUP_ID<sup>9+</sup> | number | Yes | No | Default audio interruption group ID.<br>**System capability**: SystemCapability.Multimedia.Audio.Interrupt |
| LOCAL_NETWORK_ID<sup>9+</sup> | string | Yes | No | Network ID of the local device.<br>This is a system API.<br>**System capability**: SystemCapability.Multimedia.Audio.Device |
| DEFAULT_VOLUME_GROUP_ID<sup>9+</sup> | number | Yes | No | Default volume group ID.<br>**System capability**: SystemCapability.Multimedia.Audio.Volume |
| DEFAULT_INTERRUPT_GROUP_ID<sup>9+</sup> | number | Yes | No | Default audio interruption group ID.<br>**System capability**: SystemCapability.Multimedia.Audio.Interrupt |
**Example**
...
...
@@ -1795,7 +1795,7 @@ Sets a device to the active state. This API uses a promise to return the result.
> The APIs of this module are supported since API version 6. Updates will be marked with a superscript to indicate their earliest API version.
> - The APIs of this module are supported since API version 6. Updates will be marked with a superscript to indicate their earliest API version.
> - This API is deprecated since API version 9 and will be retained until API version 13.
> - Certain functionalities are changed as system APIs and can be used only by system applications. To use these functionalities, call [@ohos.filemanagement.userFileManager](js-apis-userFileManager.md).
> - The functionalities for selecting and storing media assets are still open to common applications. To use these functionalities, call [@ohos.file.picker](js-apis-file-picker.md).
Starts image preview, with the first image to preview specified. This API can be used to preview local images whose URIs start with **datashare://** or online images whose URIs start with **https://**. It uses an asynchronous callback to return the execution result.
> **NOTE**
>
> This API is deprecated since API version 9. You are advised to use the **\<[Image](../arkui-ts/ts-basic-components-image.md)>** component instead. The **\<Image>** component can be used to render and display local and online images.
> This API is supported since API version 6 and can be used only by the FA model.
> You are advised to use the **\<[Image](../arkui-ts/ts-basic-components-image.md)>** component instead. The **\<Image>** component can be used to render and display local and online images.
Starts image preview. This API can be used to preview local images whose URIs start with **datashare://** or online images whose URIs start with **https://**. It uses an asynchronous callback to return the execution result.
> **NOTE**
>
> This API is deprecated since API version 9. You are advised to use the **\<[Image](../arkui-ts/ts-basic-components-image.md)>** component instead. The **\<Image>** component can be used to render and display local and online images.
> This API is supported since API version 6 and can be used only by the FA model.
> You are advised to use the **\<[Image](../arkui-ts/ts-basic-components-image.md)>** component instead. The **\<Image>** component can be used to render and display local and online images.
Starts image preview, with the first image to preview specified. This API can be used to preview local images whose URIs start with **datashare://** or online images whose URIs start with **https://**. It uses a promise to return the execution result.
> **NOTE**
>
> This API is deprecated since API version 9. You are advised to use the **\<[Image](../arkui-ts/ts-basic-components-image.md)>** component instead. The **\<Image>** component can be used to render and display local and online images.
> This API is supported since API version 6 and can be used only by the FA model.
> You are advised to use the **\<[Image](../arkui-ts/ts-basic-components-image.md)>** component instead. The **\<Image>** component can be used to render and display local and online images.
Starts media selection. This API uses an asynchronous callback to return the list of URIs that store the selected media assets.
> **NOTE**
>
> This API is deprecated since API version 9. You are advised to use the system app Gallery instead. Gallery is a built-in visual resource access application that provides features such as image and video management and browsing. For details about how to use Gallery, visit [OpenHarmony/applications_photos](https://gitee.com/openharmony/applications_photos).
> This API is supported since API version 6 and can be used only by the FA model.
> You are advised to use the system app Gallery instead. Gallery is a built-in visual resource access application that provides features such as image and video management and browsing. For details about how to use Gallery, visit [OpenHarmony/applications_photos](https://gitee.com/openharmony/applications_photos).
| callback | AsyncCallback<Array<string>> | Yes | Callback used to return the list of URIs (starting with **datashare://**) that store the selected media assets.|
Starts media selection. This API uses a promise to return the list of URIs that store the selected media assets.
> **NOTE**
>
> This API is deprecated since API version 9. You are advised to use the system app Gallery instead. Gallery is a built-in visual resource access application that provides features such as image and video management and browsing. For details about how to use Gallery, visit [OpenHarmony/applications_photos](https://gitee.com/openharmony/applications_photos).
> This API is supported since API version 6 and can be used only by the FA model.
> You are advised to use the system app Gallery instead. Gallery is a built-in visual resource access application that provides features such as image and video management and browsing. For details about how to use Gallery, visit [OpenHarmony/applications_photos](https://gitee.com/openharmony/applications_photos).
| option | [MediaSelectOption](#mediaselectoptiondeprecated) | Yes | Media selection option.|
| option | [MediaSelectOption](#mediaselectoption) | Yes | Media selection option.|
**Return value**
...
...
@@ -1041,7 +1036,6 @@ async function example() {
Provides APIs for encapsulating file asset attributes.
> **NOTE**
>
> 1. The system attempts to parse the file content if the file is an audio or video file. The actual field values will be restored from the passed values during scanning on some devices.
> 2. Some devices may not support the modification of **orientation**. You are advised to use [ModifyImageProperty](js-apis-image.md#modifyimageproperty9) of the **image** module.
@@ -2658,17 +2647,13 @@ Implements the media asset option.
| mimeType | string | Yes | Yes | Multipurpose Internet Mail Extensions (MIME) type of the media.<br>The value can be 'image/\*', 'video/\*', 'audio/\*' or 'file\*'.|
| relativePath | string | Yes | Yes | Custom path for storing media assets, for example, 'Pictures/'. If this parameter is unspecified, media assets are stored in the default path.<br> Default path of images: 'Pictures/'<br> Default path of videos: 'Videos/'<br> Default path of audios: 'Audios/'<br> Default path of files: 'Documents/'|
| type | 'image' | 'video' | 'media' | Yes | Yes | Media type, which can be **image**, **media**, or **video**. Currently, only **media** is supported.|
| count | number | Yes | Yes | Number of media assets selected. The value starts from 1, which indicates that one media asset can be selected. |
| count | number | Yes | Yes | Maximum number of media assets that can be selected. The value starts from 1, which indicates that one media asset can be selected. |