audio-volume-manager.md 5.8 KB
Newer Older
G
Gloria 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# 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

G
Gloria 已提交
19
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, see [Permission Application Guide](../security/accesstoken-guidelines.md#declaring-permissions-in-the-configuration-file).
G
Gloria 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

## 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.
   
G
Gloria 已提交
41
   To obtain the volume information of an audio stream (such as the ringtone, voice call, media, and voice assistant) 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).
G
Gloria 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

   ```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.');
   }
G
Gloria 已提交
92
   
G
Gloria 已提交
93 94 95 96 97 98
   async on() {   // Subscribe to microphone state changes.
     await loadVolumeGroupManager();
     await audioVolumeGroupManager.audioVolumeGroupManager.on('micStateChange', (micStateChange) => {
       console.info(`Current microphone status is: ${micStateChange.mute} `);
     });
   }
G
Gloria 已提交
99
   
G
Gloria 已提交
100 101 102 103 104
   async isMicrophoneMute() { // Check whether the microphone is muted.
     await audioVolumeGroupManager.audioVolumeGroupManager.isMicrophoneMute().then((value) => {
       console.info(`isMicrophoneMute is: ${value}.`);
     });
   }
G
Gloria 已提交
105
    
G
Gloria 已提交
106 107 108 109 110 111
   async setMicrophoneMuteTrue() { // Mute the microphone.
     await loadVolumeGroupManager();
     await audioVolumeGroupManager.audioVolumeGroupManager.setMicrophoneMute(true).then(() => {
       console.info('setMicrophoneMute to mute.');
     });
   }
G
Gloria 已提交
112
    
G
Gloria 已提交
113 114 115 116 117 118
   async setMicrophoneMuteFalse() { // Unmute the microphone.
     await loadVolumeGroupManager();
     await audioVolumeGroupManager.audioVolumeGroupManager.setMicrophoneMute(false).then(() => {
       console.info('setMicrophoneMute to not mute.');
     });
   }
G
Gloria 已提交
119
   async test(){  // Complete process: Subscribe to microphone state changes, obtain the microphone state, mute the microphone, obtain the microphone state, and then unmute the microphone.
G
Gloria 已提交
120 121 122 123 124 125 126
     await on();
     await isMicrophoneMute();
     await setMicrophoneMuteTrue();
     await isMicrophoneMute();
     await setMicrophoneMuteFalse();
   }
   ```