提交 536aef8e 编写于 作者: G Gloria

Update docs against 11222

Signed-off-by: wusongqing<wusongqing@huawei.com>
上级 7224a63f
......@@ -10,11 +10,16 @@
- [OpenSL ES Audio Playback Development](opensles-playback.md)
- [OpenSL ES Audio Recording Development](opensles-capture.md)
- [Audio Interruption Mode Development](audio-interruptmode.md)
- [Volume Management Development](audio-volume-manager.md)
- [Audio Routing and Device Management Development](audio-routing-manager.md)
- Video
- [Video Playback Development](video-playback.md)
- [Video Recording Development](video-recorder.md)
- AVSession
- [AVSession Overview](avsession-overview.md)
- [AVSession Development](avsession-guidelines.md)
- Image
- [Image Development](image.md)
......
# Audio Routing and Device Management Development
## Overview
The **AudioRoutingManager** module provides APIs for audio routing and device management. You can use the APIs to obtain the current input and output audio devices, listen for connection status changes of audio devices, and activate communication devices.
## Working Principles
The figure below shows the common APIs provided by the **AudioRoutingManager** module.
**Figure 1** Common APIs of AudioRoutingManager
![en-us_image_audio_routing_manager](figures/en-us_image_audio_routing_manager.png)
You can use these APIs to obtain the device list, subscribe to or unsubscribe from device connection status changes, activate communication devices, and obtain their activation status. For details, see [Audio Management](../reference/apis/js-apis-audio.md).
## How to Develop
For details about the APIs, see [AudioRoutingManager in Audio Management](../reference/apis/js-apis-audio.md#audioroutingmanager9).
1. Obtain an **AudioRoutingManager** instance.
Before using an API in **AudioRoutingManager**, you must use **getRoutingManager()** to obtain an **AudioRoutingManager** instance.
```js
import audio from '@ohos.multimedia.audio';
async loadAudioRoutingManager() {
var audioRoutingManager = await audio.getAudioManager().getRoutingManager();
console.info('audioRoutingManager------create-------success.');
}
```
2. (Optional) Obtain the device list and subscribe to device connection status changes.
To obtain the device list (such as input, output, distributed input, and distributed output devices) or listen for connection status changes of audio devices, refer to the following code:
```js
import audio from '@ohos.multimedia.audio';
// Obtain an AudioRoutingManager instance.
async loadAudioRoutingManager() {
var audioRoutingManager = await audio.getAudioManager().getRoutingManager();
console.info('audioRoutingManager------create-------success.');
}
// Obtain information about all audio devices. (You can set DeviceFlag as required.)
async getDevices() {
await loadAudioRoutingManager();
await audioRoutingManager.getDevices(audio.DeviceFlag.ALL_DEVICES_FLAG).then((data) => {
console.info(`getDevices success and data is: ${JSON.stringify(data)}.`);
});
}
// Subscribe to connection status changes of audio devices.
async onDeviceChange() {
await loadAudioRoutingManager();
await audioRoutingManager.on('deviceChange', audio.DeviceFlag.ALL_DEVICES_FLAG, (deviceChanged) => {
console.info('on device change type : ' + deviceChanged.type);
console.info('on device descriptor size : ' + deviceChanged.deviceDescriptors.length);
console.info('on device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceRole);
console.info('on device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceType);
});
}
// Unsubscribe from the connection status changes of audio devices.
async offDeviceChange() {
await loadAudioRoutingManager();
await audioRoutingManager.off('deviceChange', (deviceChanged) => {
console.info('off device change type : ' + deviceChanged.type);
console.info('off device descriptor size : ' + deviceChanged.deviceDescriptors.length);
console.info('off device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceRole);
console.info('off device change descriptor : ' + deviceChanged.deviceDescriptors[0].deviceType);
});
}
// Complete process: Call APIs to obtain all devices and subscribe to device changes, then manually change the connection status of a device (for example, wired headset), and finally call APIs to obtain all devices and unsubscribe from the device changes.
async test(){
await getDevices();
await onDeviceChange()();
// Manually disconnect or connect devices.
await getDevices();
await offDeviceChange();
}
```
3. (Optional) Activate a communication device and obtain its activation status.
```js
import audio from '@ohos.multimedia.audio';
// Obtain an AudioRoutingManager instance.
async loadAudioRoutingManager() {
var audioRoutingManager = await audio.getAudioManager().getRoutingManager();
console.info('audioRoutingManager------create-------success.');
}
// Activate a communication device.
async setCommunicationDevice() {
await loadAudioRoutingManager();
await audioRoutingManager.setCommunicationDevice(audio.CommunicationDeviceType.SPEAKER, true).then(() => {
console.info('setCommunicationDevice true is success.');
});
}
// Obtain the activation status of the communication device.
async isCommunicationDeviceActive() {
await loadAudioRoutingManager();
await audioRoutingManager.isCommunicationDeviceActive(audio.CommunicationDeviceType.SPEAKER).then((value) => {
console.info(`CommunicationDevice state is: ${value}.`);
});
}
// Complete process: Activate a device and obtain the activation status.
async test(){
await setCommunicationDevice();
await isCommunicationDeviceActive();
}
```
# Volume Management Development
## Overview
The **AudioVolumeManager** module provides APIs for volume management. You can use the APIs to obtain the volume of a stream, listen for ringer mode changes, and mute a microphone.
## Working Principles
The figure below shows the common APIs provided by the **AudioVolumeManager** module.
**Figure 1** Common APIs of AudioVolumeManager
![en-us_image_audio_volume_manager](figures/en-us_image_audio_volume_manager.png)
**AudioVolumeManager** provides the APIs for subscribing to system volume changes and obtaining the audio volume group manager (an **AudioVolumeGroupManager** instance). Before calling any API in **AudioVolumeGroupManager**, you must call **getVolumeGroupManager** to obtain an **AudioVolumeGroupManager** instance. You can use the APIs provided by **AudioVolumeGroupManager** to obtain the volume of a stream, mute a microphone, and listen for microphone state changes. For details, see [Audio Management](../reference/apis/js-apis-audio.md).
## Constraints
Before developing a microphone management application, configure the permission **ohos.permission.MICROPHONE** for the application. To set the microphone state, configure the permission **ohos.permission.MANAGE_AUDIO_CONFIG** (a system permission). For details about the permission configuration, see [Permission Application Guide](../security/accesstoken-guidelines.md).
## How to Develop
For details about the APIs, see [AudioVolumeManager in Audio Management](../reference/apis/js-apis-audio.md#audiovolumemanager9)
1. Obtain an **AudioVolumeGroupManager** instance.
Before using an API in **AudioVolumeGroupManager**, you must use **getVolumeGroupManager()** to obtain an **AudioStreamManager** instance.
```js
import audio from '@ohos.multimedia.audio';
async loadVolumeGroupManager() {
const groupid = audio.DEFAULT_VOLUME_GROUP_ID;
var audioVolumeGroupManager = await audio.getAudioManager().getVolumeManager().getVolumeGroupManager(groupid);
console.error('audioVolumeGroupManager create success.');
}
```
2. (Optional) Obtain the volume information and ringer mode.
To obtain the volume information (such as the ringtone, voice call, media, and voice assistant) of an audio stream or obtain the ringer mode (silent, vibration, or normal) of the current device, refer to the code below. For more details, see [Audio Management](../reference/apis/js-apis-audio.md).
```js
import audio from '@ohos.multimedia.audio';
async loadVolumeGroupManager() {
const groupid = audio.DEFAULT_VOLUME_GROUP_ID;
var audioVolumeGroupManager = await audio.getAudioManager().getVolumeManager().getVolumeGroupManager(groupid);
console.info('audioVolumeGroupManager create success.');
}
// Obtain the volume of a stream. The value ranges from 0 to 15.
async getVolume() {
await loadVolumeGroupManager();
await audioVolumeGroupManager.getVolume(audio.AudioVolumeType.MEDIA).then((value) => {
console.info(`getVolume success and volume is: ${value}.`);
});
}
// Obtain the minimum volume of a stream.
async getMinVolume() {
await loadVolumeGroupManager();
await audioVolumeGroupManager.getMinVolume(audio.AudioVolumeType.MEDIA).then((value) => {
console.info(`getMinVolume success and volume is: ${value}.`);
});
}
// Obtain the maximum volume of a stream.
async getMaxVolume() {
await loadVolumeGroupManager();
await audioVolumeGroupManager.getMaxVolume(audio.AudioVolumeType.MEDIA).then((value) => {
console.info(`getMaxVolume success and volume is: ${value}.`);
});
}
// Obtain the ringer mode in use: silent (0) | vibrate (1) | normal (2).
async getRingerMode() {
await loadVolumeGroupManager();
await audioVolumeGroupManager.getRingerMode().then((value) => {
console.info(`getRingerMode success and RingerMode is: ${value}.`);
});
}
```
3. (Optional) Obtain and set the microphone state, and subscribe to microphone state changes.
To obtain and set the microphone state or subscribe to microphone state changes, refer to the following code:
```js
import audio from '@ohos.multimedia.audio';
async loadVolumeGroupManager() {
const groupid = audio.DEFAULT_VOLUME_GROUP_ID;
var audioVolumeGroupManager = await audio.getAudioManager().getVolumeManager().getVolumeGroupManager(groupid);
console.info('audioVolumeGroupManager create success.');
}
async on() { // Subscribe to microphone state changes.
await loadVolumeGroupManager();
await audioVolumeGroupManager.audioVolumeGroupManager.on('micStateChange', (micStateChange) => {
console.info(`Current microphone status is: ${micStateChange.mute} `);
});
}
async isMicrophoneMute() { // Check whether the microphone is muted.
await audioVolumeGroupManager.audioVolumeGroupManager.isMicrophoneMute().then((value) => {
console.info(`isMicrophoneMute is: ${value}.`);
});
}
async setMicrophoneMuteTrue() { // Mute the microphone.
await loadVolumeGroupManager();
await audioVolumeGroupManager.audioVolumeGroupManager.setMicrophoneMute(true).then(() => {
console.info('setMicrophoneMute to mute.');
});
}
async setMicrophoneMuteFalse() { // Unmute the microphone.
await loadVolumeGroupManager();
await audioVolumeGroupManager.audioVolumeGroupManager.setMicrophoneMute(false).then(() => {
console.info('setMicrophoneMute to not mute.');
});
}
async test(){ // Complete process: Subscribe to microphone state changes, obtain the microphone state, mute the microphone, obtain the microphone state, and unmute the microphone.
await on();
await isMicrophoneMute();
await setMicrophoneMuteTrue();
await isMicrophoneMute();
await setMicrophoneMuteFalse();
}
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册