目录

Managing Microphone Mute State

The 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.

If 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.

Volume adjustment is not supported during recording.

How to Develop

The examples below are code snippets. You can obtain the complete sample via the link at the bottom right of the sample code.

The AudioVolumeGroupManager class provides APIs for managing the microphone state. For details, see AudioVolumeGroupManager.

  1. Create an audioVolumeGroupManager object.

    import { audio } from '@kit.AudioKit';
    
    let audioVolumeGroupManager: audio.AudioVolumeGroupManager;
    // Create an audioVolumeGroupManager object.
    async function loadVolumeGroupManager(updateCallback?: (msg: string, isError: boolean) => void): Promise<void> {
      const groupid = audio.DEFAULT_VOLUME_GROUP_ID;
      audioVolumeGroupManager = await audio.getAudioManager().getVolumeManager().getVolumeGroupManager(groupid);
      console.info('audioVolumeGroupManager create success.');
      // ...
    }
  2. Call on('micStateChange') to listen for microphone state changes. When the microphone mute state changes, the application will be notified of the change.

    Currently, when multiple AudioManager instances are used in a single process, only the subscription of the last instance takes effect for this API, and subscriptions of other instances are overwritten (even if the last instance has no subscription). Therefore, you are advised to use a single AudioManager instance for development.

    // Listen for microphone state changes.
    async function on() {
      audioVolumeGroupManager.on('micStateChange', (micStateChange: audio.MicStateChangeEvent) => {
        console.info(`Current microphone status is: ${micStateChange.mute} `);
      });
    }
  3. 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.

    // Check whether the microphone is muted.
    async function isMicrophoneMute(updateCallback?: (msg: string, isError: boolean) => void): Promise<void> {
      await audioVolumeGroupManager.isMicrophoneMute().then((value: boolean) => {
        console.info(`isMicrophoneMute is: ${value}.`);
        // ...
      });
    }