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 18 // Create an audioVolumeGroupManager object. 19 async function loadVolumeGroupManager() { 20 const groupid = audio.DEFAULT_VOLUME_GROUP_ID; 21 audioVolumeGroupManager = await audio.getAudioManager().getVolumeManager().getVolumeGroupManager(groupid); 22 console.info('audioVolumeGroupManager create success.'); 23 } 24 ``` 25<!--Del--> 262. **(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. 27 28 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. 29 30 ```ts 31 // Listen for microphone state changes. 32 async function on() { 33 audioVolumeGroupManager.on('micStateChange', (micStateChange: audio.MicStateChangeEvent) => { 34 console.info(`Current microphone status is: ${micStateChange.mute} `); 35 }); 36 } 37 ``` 38<!--DelEnd--> 39 403. 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. 41 42 ```ts 43 // Check whether the microphone is muted. 44 async function isMicrophoneMute() { 45 await audioVolumeGroupManager.isMicrophoneMute().then((value: boolean) => { 46 console.info(`isMicrophoneMute is: ${value}.`); 47 }); 48 } 49 ``` 50<!--Del--> 514. **(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**. 52 53 ```ts 54 // Mute the microphone, with true passed. 55 async function setMicrophoneMuteTrue() { 56 await audioVolumeGroupManager.setMicMute(true).then(() => { 57 console.info('setMicrophoneMute to mute.'); 58 }); 59 } 60 61 // Unmute the microphone, with false passed. 62 async function setMicrophoneMuteFalse() { 63 await audioVolumeGroupManager.setMicMute(false).then(() => { 64 console.info('setMicrophoneMute to not mute.'); 65 }); 66 } 67 ``` 68 69## Sample Code 70 71Refer to the sample code below to complete the process of muting and unmuting the microphone. 72 73```ts 74 import { audio } from '@kit.AudioKit'; 75 76 let audioVolumeGroupManager: audio.AudioVolumeGroupManager; 77 78 async function loadVolumeGroupManager() { 79 const groupid = audio.DEFAULT_VOLUME_GROUP_ID; 80 audioVolumeGroupManager = await audio.getAudioManager().getVolumeManager().getVolumeGroupManager(groupid); 81 console.info('audioVolumeGroupManager------create-------success.'); 82 } 83 84 // Listen for microphone state changes. 85 async function on() { 86 await loadVolumeGroupManager(); 87 audioVolumeGroupManager.on('micStateChange', (micStateChange) => { 88 console.info(`Current microphone status is: ${micStateChange.mute} `); 89 }); 90 } 91 92 // Check whether the microphone is muted. 93 async function isMicrophoneMute() { 94 await audioVolumeGroupManager.isMicrophoneMute().then((value) => { 95 console.info(`isMicrophoneMute is: ${value}.`); 96 }); 97 } 98 99 // Mute the microphone. 100 async function setMicrophoneMuteTrue() { 101 await loadVolumeGroupManager(); 102 await audioVolumeGroupManager.setMicMute(true).then(() => { 103 console.info('setMicrophoneMute to mute.'); 104 }); 105 } 106 107 // Unmute the microphone. 108 async function setMicrophoneMuteFalse() { 109 await loadVolumeGroupManager(); 110 await audioVolumeGroupManager.setMicMute(false).then(() => { 111 console.info('setMicrophoneMute to not mute.'); 112 }); 113 } 114 115 async function test(){ 116 await on(); 117 await isMicrophoneMute(); 118 await setMicrophoneMuteTrue(); 119 await isMicrophoneMute(); 120 await setMicrophoneMuteFalse(); 121 await isMicrophoneMute(); 122 await setMicrophoneMuteTrue(); 123 await isMicrophoneMute(); 124 } 125``` 126<!--DelEnd--> 127