1# Microphone Management
2
3The microphone is used to record audio data. To deliver an optimal recording effect, you are advised to query the microphone state before starting recording and listen for state changes during recording.
4
5If the user mutes the microphone during audio recording, the recording process is normal, the size of the recorded file increases with the recording duration, but the data volume written into the file is 0.
6
7## How to Develop
8
9The **AudioVolumeGroupManager** class provides APIs for managing the microphone state. For details, see [API Reference](../../reference/apis-audio-kit/js-apis-audio.md#audiovolumegroupmanager9).
10
111. Create an **audioVolumeGroupManager** object.
12     
13   ```ts
14   import { audio } from '@kit.AudioKit';
15
16   let audioVolumeGroupManager: audio.AudioVolumeGroupManager;
17   async function loadVolumeGroupManager() { // Create an audioVolumeGroupManager object.
18     const groupid = audio.DEFAULT_VOLUME_GROUP_ID;
19     audioVolumeGroupManager = await audio.getAudioManager().getVolumeManager().getVolumeGroupManager(groupid);
20     console.info('audioVolumeGroupManager create success.');
21   }
22   ```
23<!--Del-->
242. **(Optional; for system applications only)** Call **on('micStateChange')** to listen for microphone state changes. When the microphone state changes, the application will be notified of the change.
25   
26   Currently, when multiple **AudioManager** instances are used in a single process, only the subscription of the last instance takes effect, and the subscription of other instances is overwritten (even if the last instance does not initiate a subscription). Therefore, you are advised to use a single **AudioManager** instance.
27
28   ```ts
29   async function on() {   // Subscribe to microphone state changes.
30     audioVolumeGroupManager.on('micStateChange', (micStateChange: audio.MicStateChangeEvent) => {
31       console.info(`Current microphone status is: ${micStateChange.mute} `);
32     });
33   }
34   ```
35<!--DelEnd-->
36
373. Call **isMicrophoneMute** to check whether the microphone is muted. If the return value is **true**, the microphone is muted; otherwise, the microphone is not muted.
38     
39   ```ts
40   async function isMicrophoneMute() { // Check whether the microphone is muted.
41     await audioVolumeGroupManager.isMicrophoneMute().then((value: boolean) => {
42       console.info(`isMicrophoneMute is: ${value}.`);
43     });
44   }
45   ```
46<!--Del-->
474. **(Optional; for system applications only)** Call **setMicrophoneMute** to mute or unmute the microphone. To mute the microphone, pass in **true**. To unmute the microphone, pass in **false**.
48     
49   ```ts
50   async function setMicrophoneMuteTrue() { // Pass in true to mute the microphone.
51     await audioVolumeGroupManager.setMicrophoneMute(true).then(() => {
52       console.info('setMicrophoneMute to mute.');
53     });
54   }
55   async function setMicrophoneMuteFalse() { // Pass in false to unmute the microphone.
56     await audioVolumeGroupManager.setMicrophoneMute(false).then(() => {
57       console.info('setMicrophoneMute to not mute.');
58     });
59   }
60   ```
61
62## Sample Code
63
64Refer to the sample code below to complete the process of muting and unmuting the microphone.
65
66```ts
67   import { audio } from '@kit.AudioKit';
68   
69   let audioVolumeGroupManager: audio.AudioVolumeGroupManager;
70   
71   async function loadVolumeGroupManager() {
72        const groupid = audio.DEFAULT_VOLUME_GROUP_ID;
73        audioVolumeGroupManager = await audio.getAudioManager().getVolumeManager().getVolumeGroupManager(groupid);
74        console.info('audioVolumeGroupManager------create-------success.');
75   }
76   
77   async function on() {   // Subscribe to microphone state changes.
78     await loadVolumeGroupManager();
79     audioVolumeGroupManager.on('micStateChange', (micStateChange) => {
80       console.info(`Current microphone status is: ${micStateChange.mute} `);
81     });
82   }
83   async function isMicrophoneMute() { // Check whether the microphone is muted.
84     await audioVolumeGroupManager.isMicrophoneMute().then((value) => {
85       console.info(`isMicrophoneMute is: ${value}.`);
86     });
87   }
88   async function setMicrophoneMuteTrue() { // Mute the microphone.
89     await loadVolumeGroupManager();
90     await audioVolumeGroupManager.setMicrophoneMute(true).then(() => {
91       console.info('setMicrophoneMute to mute.');
92     });
93   }
94   async function setMicrophoneMuteFalse() { // Unmute the microphone.
95     await loadVolumeGroupManager();
96     await audioVolumeGroupManager.setMicrophoneMute(false).then(() => {
97       console.info('setMicrophoneMute to not mute.');
98     });
99   }
100   async function test(){
101     await on();
102     await isMicrophoneMute();
103     await setMicrophoneMuteTrue();
104     await isMicrophoneMute();
105     await setMicrophoneMuteFalse();
106     await isMicrophoneMute();
107     await setMicrophoneMuteTrue();
108     await isMicrophoneMute();
109   }
110
111```
112<!--DelEnd-->
113