• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/js-apis-audio.md#audiovolumegroupmanager9).
10
111. Create an **audioVolumeGroupManager** object.
12
13   ```ts
14   import audio from '@ohos.multimedia.audio';
15
16   let 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
242. 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
29   ```ts
30   async function on() {   // Subscribe to microphone state changes.
31     audioVolumeGroupManager.on('micStateChange', (micStateChange) => {
32       console.info(`Current microphone status is: ${micStateChange.mute} `);
33     });
34   }
35   ```
36
373. Call **isMicrophoneMute** to check whether the microphone is muted. If the returned 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) => {
42       console.info(`isMicrophoneMute is: ${value}.`);
43     });
44   }
45   ```
46
474. 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
67import audio from '@ohos.multimedia.audio';
68
69@Entry
70@Component
71struct AudioVolumeGroup {
72 private audioVolumeGroupManager: audio.AudioVolumeGroupManager;
73
74  async loadVolumeGroupManager() {
75    const groupid = audio.DEFAULT_VOLUME_GROUP_ID;
76    this.audioVolumeGroupManager = await audio.getAudioManager().getVolumeManager().getVolumeGroupManager(groupid);
77    console.info('audioVolumeGroupManager------create-------success.');
78  }
79
80  async on() {   // Subscribe to microphone state changes.
81    await this.loadVolumeGroupManager();
82    this.audioVolumeGroupManager.on('micStateChange', (micStateChange) => {
83      console.info(`Current microphone status is: ${micStateChange.mute} `);
84    });
85  }
86  async isMicrophoneMute() { // Check whether the microphone is muted.
87    await this.audioVolumeGroupManager.isMicrophoneMute().then((value) => {
88      console.info(`isMicrophoneMute is: ${value}.`);
89    });
90  }
91  async setMicrophoneMuteTrue() { // Mute the microphone.
92    await this.loadVolumeGroupManager();
93    await this.audioVolumeGroupManager.setMicrophoneMute(true).then(() => {
94      console.info('setMicrophoneMute to mute.');
95    });
96  }
97  async setMicrophoneMuteFalse() { // Unmute the microphone.
98    await this.loadVolumeGroupManager();
99    await this.audioVolumeGroupManager.setMicrophoneMute(false).then(() => {
100      console.info('setMicrophoneMute to not mute.');
101    });
102  }
103  async test(){
104    await this.on();
105    await this.isMicrophoneMute();
106    await this.setMicrophoneMuteTrue();
107    await this.isMicrophoneMute();
108    await this.setMicrophoneMuteFalse();
109    await this.isMicrophoneMute();
110    await this.setMicrophoneMuteTrue();
111    await this.isMicrophoneMute();
112  }
113}
114```
115