@@ -21,38 +21,49 @@ This following figure shows the audio capturer state transitions.
...
@@ -21,38 +21,49 @@ This following figure shows the audio capturer state transitions.
## Constraints
## Constraints
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).
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#declaring-permissions-in-the-configuration-file).
## How to Develop
## How to Develop
For details about the APIs, see [AudioCapturer in Audio Management](../reference/apis/js-apis-audio.md#audiocapturer8).
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.
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
```js
importaudiofrom'@ohos.multimedia.audio';
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.
7. (Optional) Use **on('markReach')** to subscribe to the mark reached event, and use **off('markReach')** to unsubscribe from the event.
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
...
@@ -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.
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.
8. (Optional) Use **on('periodReach')** to subscribe to the period reached event, and use **off('periodReach')** to unsubscribe from the event.
...
@@ -215,40 +216,43 @@ For details about the APIs, see [AudioCapturer in Audio Management](../reference
...
@@ -215,40 +216,43 @@ 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.
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}`);
console.info('Period reach event Received');
});
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.
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
```js
audioCapturer.on('stateChange', (state) => {
async stateChange(){
console.info(`AudioCapturerLog: Changed State to : ${state}`)
this.audioCapturer.on('stateChange', (state) => {
switch (state) {
console.info(`AudioCapturerLog: Changed State to : ${state}`)
case audio.AudioState.STATE_PREPARED:
switch (state) {
console.info('--------CHANGE IN AUDIO STATE----------PREPARED--------------');
case audio.AudioState.STATE_PREPARED:
console.info('Audio State is : Prepared');
console.info('--------CHANGE IN AUDIO STATE----------PREPARED--------------');
break;
console.info('Audio State is : Prepared');
case audio.AudioState.STATE_RUNNING:
break;
console.info('--------CHANGE IN AUDIO STATE----------RUNNING--------------');
case audio.AudioState.STATE_RUNNING:
console.info('Audio State is : Running');
console.info('--------CHANGE IN AUDIO STATE----------RUNNING--------------');
break;
console.info('Audio State is : Running');
case audio.AudioState.STATE_STOPPED:
break;
console.info('--------CHANGE IN AUDIO STATE----------STOPPED--------------');
case audio.AudioState.STATE_STOPPED:
console.info('Audio State is : stopped');
console.info('--------CHANGE IN AUDIO STATE----------STOPPED--------------');
break;
console.info('Audio State is : stopped');
case audio.AudioState.STATE_RELEASED:
break;
console.info('--------CHANGE IN AUDIO STATE----------RELEASED--------------');
case audio.AudioState.STATE_RELEASED:
console.info('Audio State is : released');
console.info('--------CHANGE IN AUDIO STATE----------RELEASED--------------');
break;
console.info('Audio State is : released');
default:
break;
console.info('--------CHANGE IN AUDIO STATE----------INVALID--------------');
default:
console.info('Audio State is : invalid');
console.info('--------CHANGE IN AUDIO STATE----------INVALID--------------');
letpathDir="/data/storage/el2/base/haps/entry/files"// The path used here is an example. Obtain the path based on project requirements.
// Use getRawFileDescriptor of the resource management module to obtain the media assets in the application, and use the fdSrc attribute of the AVPlayer to initialize the media asset.
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\H264_AAC.mp4 /data/app/el2/100/base/ohos.acts.multimedia.media.avplayer/haps/entry/files" command.
// For details on the fd/offset/length parameter, see the Media API. The globalThis.abilityContext parameter is a system environment variable and is saved as a global variable on the main page during the system boost.
> All APIs of the **AVSession** module are system APIs and can be called only by system applications.
## Overview
## Overview
AVSession, short for audio and video session, is also known as media session.
AVSession, short for audio and video session, is also known as media session.
...
@@ -49,4 +53,4 @@ The **AVSession** module provides two classes: **AVSession** and **AVSessionCont
...
@@ -49,4 +53,4 @@ The **AVSession** module provides two classes: **AVSession** and **AVSessionCont
- AVSession can transmit media playback information and control commands. It does not display information or execute control commands.
- AVSession can transmit media playback information and control commands. It does not display information or execute control commands.
- Do not develop Media Controller for common applications. For common audio and video applications running on OpenHarmony, the default control end is Media Controller, which is a system application. You do not need to carry out additional development for Media Controller.
- Do not develop Media Controller for common applications. For common audio and video applications running on OpenHarmony, the default control end is Media Controller, which is a system application. You do not need to carry out additional development for Media Controller.
- If you want to develop your own system running OpenHarmony, you can develop your own Media Controller.
- If you want to develop your own system running OpenHarmony, you can develop your own Media Controller.
- For better background management of audio and video applications, the **AVSession** module enforces background control for third-party applications. Only third-party applications that have accessed AVSession can play audio in the background. Otherwise, the system forcibly pauses the playback when a third-party application switches to the background.
- For better background management of audio and video applications, the **AVSession** module enforces background control for applications. Only applications that have accessed AVSession can play audio in the background. Otherwise, the system forcibly pauses the playback when an application switches to the background.
| 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 |
| 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_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 |
| DEFAULT_INTERRUPT_GROUP_ID<sup>9+</sup> | number | Yes | No | Default audio interruption group ID.<br>**System capability**: SystemCapability.Multimedia.Audio.Interrupt |
**Example**
**Example**
...
@@ -349,7 +349,10 @@ Enumerates the audio stream types.
...
@@ -349,7 +349,10 @@ Enumerates the audio stream types.
| VOICE_CALL<sup>8+</sup> | 0 | Audio stream for voice calls.|
| VOICE_CALL<sup>8+</sup> | 0 | Audio stream for voice calls.|
| RINGTONE | 2 | Audio stream for ringtones. |
| RINGTONE | 2 | Audio stream for ringtones. |
| MEDIA | 3 | Audio stream for media purpose. |
| MEDIA | 3 | Audio stream for media purpose. |
| ALARM<sup>10+</sup> | 4 | Audio stream for alarming. |
| ACCESSIBILITY<sup>10+</sup> | 5 | Audio stream for accessibility. |
| VOICE_ASSISTANT<sup>8+</sup> | 9 | Audio stream for voice assistant.|
| VOICE_ASSISTANT<sup>8+</sup> | 9 | Audio stream for voice assistant.|
| ULTRASONIC<sup>10+</sup> | 10 | Audio stream for ultrasonic.<br>This is a system API.|
| ALL<sup>9+</sup> | 100 | All public audio streams.<br>This is a system API.|
| ALL<sup>9+</sup> | 100 | All public audio streams.<br>This is a system API.|
## InterruptRequestResultType<sup>9+</sup>
## InterruptRequestResultType<sup>9+</sup>
...
@@ -531,7 +534,7 @@ Enumerates the audio content types.
...
@@ -531,7 +534,7 @@ Enumerates the audio content types.