openharmony 鸿蒙 arkts-apis-audio-AudioVolumeGroupManager

2026-08-25 浏览 (1)

Interface (AudioVolumeGroupManager)

This interface implements volume management for an audio group.

Before calling any API in AudioVolumeGroupManager, you must use getVolumeGroupManager to obtain an AudioVolumeGroupManager instance.

NOTE

  • The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
  • The initial APIs of this interface are supported since API version 9.

Modules to Import

import { audio } from '@kit.AudioKit';

getVolume(deprecated)

getVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void

Obtains the volume level of a stream. This API uses an asynchronous callback to return the result.

NOTE This API is supported since API version 9 and deprecated since API version 20. You are advised to use getVolumeByStream instead.

System capability: SystemCapability.Multimedia.Audio.Volume

Parameters

NameTypeMandatoryDescription
volumeTypeAudioVolumeTypeYesAudio volume type.
callbackAsyncCallback<number>YesCallback used to return the result. If the operation is successful, err is undefined and data is the stream volume level obtained; otherwise, err is an error object. The volume range of a specified stream can be obtained by calling getMinVolume and getMaxVolume.

Example

import { BusinessError } from '@kit.BasicServicesKit';

audioVolumeGroupManager.getVolume(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: number) => {
  if (err) {
    console.error(`Failed to get volume. Code: ${err.code}, message: ${err.message}`);
    return;
  }
  console.info(`Succeeded in getting volume. Volume: ${value}.`);
});

getVolume(deprecated)

getVolume(volumeType: AudioVolumeType): Promise<number>

Obtains the volume level of a stream. This API uses a promise to return the result.

NOTE This API is supported since API version 9 and deprecated since API version 20. You are advised to use getVolumeByStream instead.

System capability: SystemCapability.Multimedia.Audio.Volume

Parameters

NameTypeMandatoryDescription
volumeTypeAudioVolumeTypeYesAudio volume type.

Return value

TypeDescription
Promise<number>Promise used to return the stream volume level. The volume range of a specified stream can be obtained by calling getMinVolume and getMaxVolume.

Example

audioVolumeGroupManager.getVolume(audio.AudioVolumeType.MEDIA).then((value: number) => {
  console.info(`Succeeded in getting volume. Volume: ${value}.`);
});

getVolumeSync(deprecated)

getVolumeSync(volumeType: AudioVolumeType): number

Obtains the volume level of a stream. This API returns the result synchronously.

NOTE This API is supported since API version 10 and deprecated since API version 20. You are advised to use getVolumeByStream instead.

System capability: SystemCapability.Multimedia.Audio.Volume

Parameters

NameTypeMandatoryDescription
volumeTypeAudioVolumeTypeYesAudio volume type.

Return value

TypeDescription
numberVolume level of the stream. The volume range of a specified stream can be obtained by calling getMinVolume and getMaxVolume.

Error codes

For details about the error codes, see Universal Error Codes and Audio Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.
6800101Parameter verification failed.

Example

import { BusinessError } from '@kit.BasicServicesKit';

try {
  let value: number = audioVolumeGroupManager.getVolumeSync(audio.AudioVolumeType.MEDIA);
  console.info(`Succeeded in getting volume. Volume: ${value}.`);
} catch (err) {
  let error = err as BusinessError;
  console.error(`Failed to get volume. Code: ${error.code}, message: ${error.message}`);
}

getMinVolume(deprecated)

getMinVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void

Obtains the minimum volume level of a stream. This API uses an asynchronous callback to return the result.

NOTE This API is supported since API version 9 and deprecated since API version 20. You are advised to use getMinVolumeByStream instead.

System capability: SystemCapability.Multimedia.Audio.Volume

Parameters

NameTypeMandatoryDescription
volumeTypeAudioVolumeTypeYesAudio volume type.
callbackAsyncCallback<number>YesCallback used to return the result. If the operation is successful, err is undefined and data is the minimum stream volume level obtained; otherwise, err is an error object.

Example

import { BusinessError } from '@kit.BasicServicesKit';

audioVolumeGroupManager.getMinVolume(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: number) => {
  if (err) {
    console.error(`Failed to get minVolume. Code: ${err.code}, message: ${err.message}`);
    return;
  }
  console.info(`Succeeded in getting minVolume. Volume: ${value}.`);
});

getMinVolume(deprecated)

getMinVolume(volumeType: AudioVolumeType): Promise<number>

Obtains the minimum volume level of a stream. This API uses a promise to return the result.

NOTE This API is supported since API version 9 and deprecated since API version 20. You are advised to use getMinVolumeByStream instead.

System capability: SystemCapability.Multimedia.Audio.Volume

Parameters

NameTypeMandatoryDescription
volumeTypeAudioVolumeTypeYesAudio volume type.

Return value

TypeDescription
Promise<number>Promise used to return the minimum volume level.

Example

audioVolumeGroupManager.getMinVolume(audio.AudioVolumeType.MEDIA).then((value: number) => {
  console.info(`Succeeded in getting minVolume. Volume: ${value}.`);
});

getMinVolumeSync(deprecated)

getMinVolumeSync(volumeType: AudioVolumeType): number

Obtains the minimum volume level of a stream. This API returns the result synchronously.

NOTE This API is supported since API version 10 and deprecated since API version 20. You are advised to use getMinVolumeByStream instead.

System capability: SystemCapability.Multimedia.Audio.Volume

Parameters

NameTypeMandatoryDescription
volumeTypeAudioVolumeTypeYesAudio volume type.

Return value

TypeDescription
numberMinimum volume level.

Error codes

For details about the error codes, see Universal Error Codes and Audio Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.
6800101Parameter verification failed.

Example

import { BusinessError } from '@kit.BasicServicesKit';

try {
  let value: number = audioVolumeGroupManager.getMinVolumeSync(audio.AudioVolumeType.MEDIA);
  console.info(`Succeeded in getting minVolume. Volume: ${value}.`);
} catch (err) {
  let error = err as BusinessError;
  console.error(`Failed to get minVolume. Code: ${error.code}, message: ${error.message}`);
}

getMaxVolume(deprecated)

getMaxVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void

Obtains the maximum volume level of a stream. This API uses an asynchronous callback to return the result.

NOTE This API is supported since API version 9 and deprecated since API version 20. You are advised to use getMaxVolumeByStream instead.

System capability: SystemCapability.Multimedia.Audio.Volume

Parameters

NameTypeMandatoryDescription
volumeTypeAudioVolumeTypeYesAudio volume type.
callbackAsyncCallback<number>YesCallback used to return the result. If the operation is successful, err is undefined and data is the maximum stream volume level obtained; otherwise, err is an error object.

Example

import { BusinessError } from '@kit.BasicServicesKit';

audioVolumeGroupManager.getMaxVolume(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: number) => {
  if (err) {
    console.error(`Failed to get maxVolume. Code: ${err.code}, message: ${err.message}`);
    return;
  }
  console.info(`Succeeded in getting maxVolume. Volume: ${value}.`);
});

getMaxVolume(deprecated)

getMaxVolume(volumeType: AudioVolumeType): Promise<number>

Obtains the maximum volume level of a stream. This API uses a promise to return the result.

NOTE This API is supported since API version 9 and deprecated since API version 20. You are advised to use getMaxVolumeByStream instead.

System capability: SystemCapability.Multimedia.Audio.Volume

Parameters

NameTypeMandatoryDescription
volumeTypeAudioVolumeTypeYesAudio volume type.

Return value

TypeDescription
Promise<number>Promise used to return the maximum volume level.

Example

audioVolumeGroupManager.getMaxVolume(audio.AudioVolumeType.MEDIA).then((value: number) => {
  console.info(`Succeeded in getting maxVolume. Volume: ${value}.`);
});

getMaxVolumeSync(deprecated)

getMaxVolumeSync(volumeType: AudioVolumeType): number

Obtains the maximum volume level of a stream. This API returns the result synchronously.

NOTE This API is supported since API version 10 and deprecated since API version 20. You are advised to use getMaxVolumeByStream instead.

System capability: SystemCapability.Multimedia.Audio.Volume

Parameters

NameTypeMandatoryDescription
volumeTypeAudioVolumeTypeYesAudio volume type.

Return value

TypeDescription
numberMaximum volume level.

Error codes

For details about the error codes, see Universal Error Codes and Audio Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.
6800101Parameter verification failed.

Example

import { BusinessError } from '@kit.BasicServicesKit';

try {
  let value: number = audioVolumeGroupManager.getMaxVolumeSync(audio.AudioVolumeType.MEDIA);
  console.info(`Succeeded in getting maxVolume. Volume: ${value}.`);
} catch (err) {
  let error = err as BusinessError;
  console.error(`Failed to get maxVolume. Code: ${error.code}, message: ${error.message}`);
}

isMute(deprecated)

isMute(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): void

Checks whether a stream is muted. This API uses an asynchronous callback to return the result.

NOTE This API is supported since API version 9 and deprecated since API version 20. You are advised to use isSystemMutedForStream instead.

System capability: SystemCapability.Multimedia.Audio.Volume

Parameters

NameTypeMandatoryDescription
volumeTypeAudioVolumeTypeYesAudio volume type.
callbackAsyncCallback<boolean>YesCallback used to return the result. If the operation is successful, err is undefined and data is true if the stream is muted or false if not muted; otherwise, err is an error object.

Example

import { BusinessError } from '@kit.BasicServicesKit';

audioVolumeGroupManager.isMute(audio.AudioVolumeType.MEDIA, (err: BusinessError, value: boolean) => {
  if (err) {
    console.error(`Failed to use isMute function. Code: ${err.code}, message: ${err.message}`);
    return;
  }
  console.info(`Succeeded in using isMute function. MuteState: ${value}.`);
});

isMute(deprecated)

isMute(volumeType: AudioVolumeType): Promise<boolean>

Checks whether a stream is muted. This API uses a promise to return the result.

NOTE This API is supported since API version 9 and deprecated since API version 20. You are advised to use isSystemMutedForStream instead.

System capability: SystemCapability.Multimedia.Audio.Volume

Parameters

NameTypeMandatoryDescription
volumeTypeAudioVolumeTypeYesAudio volume type.

Return value

TypeDescription
Promise<boolean>Promise used to return the result, indicating whether the stream is muted. true if muted, false otherwise.

Example

audioVolumeGroupManager.isMute(audio.AudioVolumeType.MEDIA).then((value: boolean) => {
  console.info(`Succeeded in using isMute function. MuteState: ${value}.`);
});

isMuteSync(deprecated)

isMuteSync(volumeType: AudioVolumeType): boolean

Checks whether a stream is muted. This API returns the result synchronously.

NOTE This API is supported since API version 10 and deprecated since API version 20. You are advised to use isSystemMutedForStream instead.

System capability: SystemCapability.Multimedia.Audio.Volume

Parameters

NameTypeMandatoryDescription
volumeTypeAudioVolumeTypeYesAudio volume type.

Return value

TypeDescription
booleanCheck result for whether the stream is muted. true if muted, false otherwise.

Error codes

For details about the error codes, see Universal Error Codes and Audio Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.
6800101Parameter verification failed.

Example

import { BusinessError } from '@kit.BasicServicesKit';

try {
  let value: boolean = audioVolumeGroupManager.isMuteSync(audio.AudioVolumeType.MEDIA);
  console.info(`Succeeded in using isMuteSync function. MuteState: ${value}.`);
} catch (err) {
  let error = err as BusinessError;
  console.error(`Failed to use isMuteSync function. Code: ${error.code}, message: ${error.message}`);
}

getRingerMode9+

getRingerMode(callback: AsyncCallback<AudioRingMode>): void

Obtains the ringer mode. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Audio.Volume

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<AudioRingMode>YesCallback used to return the result. If the operation is successful, err is undefined and data is the ringer mode obtained; otherwise, err is an error object.

Example

import { BusinessError } from '@kit.BasicServicesKit';

audioVolumeGroupManager.getRingerMode((err: BusinessError, value: audio.AudioRingMode) => {
  if (err) {
    console.error(`Failed to get ringerMode. Code: ${err.code}, message: ${err.message}`);
    return;
  }
  console.info(`Succeeded in getting ringerMode. AudioRingMode: ${value}.`);
});

getRingerMode9+

getRingerMode(): Promise<AudioRingMode>

Obtains the ringer mode. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Audio.Volume

Return value

TypeDescription
Promise<AudioRingMode>Promise used to return the ringer mode.

Example

import { BusinessError } from '@kit.BasicServicesKit';

audioVolumeGroupManager.getRingerMode().then((value: audio.AudioRingMode) => {
  console.info(`Succeeded in getting ringerMode. AudioRingMode: ${value}.`);
}).catch((err: BusinessError) => {
  console.error(`Failed to get ringerMode. Code: ${err.code}, message: ${err.message}`);
});

getRingerModeSync10+

getRingerModeSync(): AudioRingMode

Obtains the ringer mode. This API returns the result synchronously.

System capability: SystemCapability.Multimedia.Audio.Volume

Return value

TypeDescription
AudioRingModeRinger mode.

Example

import { BusinessError } from '@kit.BasicServicesKit';

try {
  let value: audio.AudioRingMode = audioVolumeGroupManager.getRingerModeSync();
  console.info(`Succeeded in getting ringerMode. AudioRingMode: ${value}.`);
} catch (err) {
  let error = err as BusinessError;
  console.error(`Failed to get ringerMode. Code: ${error.code}, message: ${error.message}`);
}

on('ringerModeChange')9+

on(type: 'ringerModeChange', callback: Callback<AudioRingMode>): void

Subscribes to the ringer mode change event, which is triggered when the AudioRingMode changes. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Audio.Volume

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'ringerModeChange' is triggered when the ringer mode is changed.
callbackCallback<AudioRingMode>YesCallback used to return the changed ringer mode.

Error codes

For details about the error codes, see Universal Error Codes and Audio Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.
6800101Parameter verification failed.

Example

audioVolumeGroupManager.on('ringerModeChange', (ringerMode: audio.AudioRingMode) => {
  console.info(`Succeeded in using on function. AudioRingMode: ${ringerMode}.`);
});

off('ringerModeChange')18+

off(type: 'ringerModeChange', callback?: Callback<AudioRingMode>): void

Unsubscribes from the ringer mode change event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Audio.Volume

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'ringerModeChange' is triggered when the ringer mode is changed.
callbackCallback<AudioRingMode>NoCallback used to return the changed ringer mode.

Error codes

For details about the error codes, see Audio Error Codes.

IDError Message
6800101Parameter verification failed.

Example

// Cancel all subscriptions to the event.
audioVolumeGroupManager.off('ringerModeChange');

// For the same event, if the callback parameter passed to the off API is the same as that passed to the on API, the off API cancels the subscription registered with the specified callback parameter.
let ringerModeChangeCallback = (ringerMode: audio.AudioRingMode) => {
  console.info(`Succeeded in using on or off function. AudioRingMode: ${ringerMode}.`);
};

audioVolumeGroupManager.on('ringerModeChange', ringerModeChangeCallback);

audioVolumeGroupManager.off('ringerModeChange', ringerModeChangeCallback);

isMicrophoneMute9+

isMicrophoneMute(callback: AsyncCallback<boolean>): void

Checks whether the microphone is muted. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Audio.Volume

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<boolean>YesCallback used to return the result. If the operation is successful, err is undefined and data is true if the microphone is muted or false if not muted; otherwise, err is an error object.

Example

import { BusinessError } from '@kit.BasicServicesKit';

audioVolumeGroupManager.isMicrophoneMute((err: BusinessError, value: boolean) => {
  if (err) {
    console.error(`Failed to use isMicrophoneMute function. Code: ${err.code}, message: ${err.message}`);
    return;
  }
  console.info(`Succeeded in using isMicrophoneMute function. MuteState: ${value}.`);
});

isMicrophoneMute9+

isMicrophoneMute(): Promise<boolean>

Checks whether the microphone is muted. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Audio.Volume

Return value

TypeDescription
Promise<boolean>Promise used to return the result, indicating whether the microphone is muted. true if muted, false otherwise.

Example

import { BusinessError } from '@kit.BasicServicesKit';

audioVolumeGroupManager.isMicrophoneMute().then((value: boolean) => {
  console.info(`Succeeded in using isMicrophoneMute function. MuteState: ${value}.`);
}).catch((err: BusinessError) => {
  console.error(`Failed to use isMicrophoneMute function. Code: ${err.code}, message: ${err.message}`);
});

isMicrophoneMuteSync10+

isMicrophoneMuteSync(): boolean

Checks whether the microphone is muted. This API returns the result synchronously.

System capability: SystemCapability.Multimedia.Audio.Volume

Return value

TypeDescription
booleanCheck result for whether the microphone is muted. true if muted, false otherwise.

Example

import { BusinessError } from '@kit.BasicServicesKit';

try {
  let value: boolean = audioVolumeGroupManager.isMicrophoneMuteSync();
  console.info(`Succeeded in using isMicrophoneMuteSync function. MuteState: ${value}.`);
} catch (err) {
  let error = err as BusinessError;
  console.error(`Failed to use isMicrophoneMuteSync function. Code: ${error.code}, message: ${error.message}`);
}

on('micStateChange')9+

on(type: 'micStateChange', callback: Callback<MicStateChangeEvent>): void

Subscribes to the microphone state change event, which is triggered when the microphone state is changed. This API uses an asynchronous callback to return the result.

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.

System capability: SystemCapability.Multimedia.Audio.Volume

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'micStateChange' is triggered when the microphone state is changed.
callbackCallback<MicStateChangeEvent>YesCallback used to return the changed microphone state.

Error codes

For details about the error codes, see Universal Error Codes and Audio Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.
6800101Parameter verification failed.

Example

audioVolumeGroupManager.on('micStateChange', (micStateChange: audio.MicStateChangeEvent) => {
  console.info(`Succeeded in using on function. MicStateChangeEvent: ${JSON.stringify(micStateChange)}.`);
});

off('micStateChange')12+

off(type: 'micStateChange', callback?: Callback<MicStateChangeEvent>): void

Unsubscribes from the microphone state change event. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Audio.Volume

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'micStateChange' is triggered when the microphone state is changed.
callbackCallback<MicStateChangeEvent>NoCallback used to return the changed microphone state.

Error codes

For details about the error codes, see Universal Error Codes and Audio Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters missing; 2.Incorrect parameter types.
6800101Parameter verification failed.

Example

// Cancel all subscriptions to the event.
audioVolumeGroupManager.off('micStateChange');

// For the same event, if the callback parameter passed to the off API is the same as that passed to the on API, the off API cancels the subscription registered with the specified callback parameter.
let micStateChangeCallback = (micStateChange: audio.MicStateChangeEvent) => {
  console.info(`Succeeded in using on or off function. MicStateChangeEvent: ${JSON.stringify(micStateChange)}.`);
};

audioVolumeGroupManager.on('micStateChange', micStateChangeCallback);

audioVolumeGroupManager.off('micStateChange', micStateChangeCallback);

isVolumeUnadjustable10+

isVolumeUnadjustable(): boolean

Checks whether the fixed volume mode is enabled. When the fixed volume mode is enabled, the volume cannot be adjusted. This API returns the result synchronously.

System capability: SystemCapability.Multimedia.Audio.Volume

Return value

TypeDescription
booleanCheck result for whether the fixed volume mode is enabled. true if enabled, false otherwise.

Example

let volumeAdjustSwitch: boolean = audioVolumeGroupManager.isVolumeUnadjustable();
console.info(`Succeeded in using isVolumeUnadjustable function. VolumeUnadjustable: ${volumeAdjustSwitch}.`);

getSystemVolumeInDb(deprecated)

getSystemVolumeInDb(volumeType: AudioVolumeType, volumeLevel: number, device: DeviceType, callback: AsyncCallback<number>): void

Obtains the volume gain. This API uses an asynchronous callback to return the result.

NOTE This API is supported since API version 10 and deprecated since API version 20. You are advised to use getVolumeInUnitOfDbByStream instead.

System capability: SystemCapability.Multimedia.Audio.Volume

Parameters

NameTypeMandatoryDescription
volumeTypeAudioVolumeTypeYesAudio volume type.
volumeLevelnumberYesVolume level.
deviceDeviceTypeYesDevice type.
callbackAsyncCallback<number>YesCallback used to return the result. If the operation is successful, err is undefined and data is the volume gain obtained; otherwise, err is an error object.

Error codes

For details about the error codes, see Universal Error Codes and Audio Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.
6800101Parameter verification failed. Return by callback.
6800301System error. Return by callback.

Example

import { BusinessError } from '@kit.BasicServicesKit';

audioVolumeGroupManager.getSystemVolumeInDb(audio.AudioVolumeType.MEDIA, 3, audio.DeviceType.SPEAKER, (err: BusinessError, value: number) => {
  if (err) {
    console.error(`Failed to get system volume in db. Code: ${err.code}, message: ${err.message}`);
  } else {
    console.info(`Succeeded in getting system volume in db. DB: ${value}.`);
  }
});

getSystemVolumeInDb(deprecated)

getSystemVolumeInDb(volumeType: AudioVolumeType, volumeLevel: number, device: DeviceType): Promise<number>

Obtains the volume gain. This API uses a promise to return the result.

NOTE This API is supported since API version 10 and deprecated since API version 20. You are advised to use getVolumeInUnitOfDbByStream instead.

System capability: SystemCapability.Multimedia.Audio.Volume

Parameters

NameTypeMandatoryDescription
volumeTypeAudioVolumeTypeYesAudio volume type.
volumeLevelnumberYesVolume level.
deviceDeviceTypeYesDevice type.

Return value

TypeDescription
Promise<number>Promise used to return the volume gain (in dB).

Error codes

For details about the error codes, see Universal Error Codes and Audio Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.
6800101Parameter verification failed. Return by promise.
6800301System error. Return by promise.

Example

import { BusinessError } from '@kit.BasicServicesKit';

audioVolumeGroupManager.getSystemVolumeInDb(audio.AudioVolumeType.MEDIA, 3, audio.DeviceType.SPEAKER).then((value: number) => {
  console.info(`Succeeded in getting system volume in db. DB: ${value}.`);
}).catch((err: BusinessError) => {
  console.error(`Failed to get system volume in db. Code: ${err.code}, message: ${err.message}`);
});

getSystemVolumeInDbSync(deprecated)

getSystemVolumeInDbSync(volumeType: AudioVolumeType, volumeLevel: number, device: DeviceType): number

Obtains the volume gain. This API returns the result synchronously.

NOTE This API is supported since API version 10 and deprecated since API version 20. You are advised to use getVolumeInUnitOfDbByStream instead.

System capability: SystemCapability.Multimedia.Audio.Volume

Parameters

NameTypeMandatoryDescription
volumeTypeAudioVolumeTypeYesAudio volume type.
volumeLevelnumberYesVolume level.
deviceDeviceTypeYesDevice type.

Return value

TypeDescription
numberVolume gain (in dB).

Error codes

For details about the error codes, see Universal Error Codes and Audio Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.
6800101Parameter verification failed.

Example

import { BusinessError } from '@kit.BasicServicesKit';

try {
  let value: number = audioVolumeGroupManager.getSystemVolumeInDbSync(audio.AudioVolumeType.MEDIA, 3, audio.DeviceType.SPEAKER);
  console.info(`Succeeded in getting system volume in db. DB: ${value}.`);
} catch (err) {
  let error = err as BusinessError;
  console.error(`Failed to get system volume in db. Code: ${error.code}, message: ${error.message}`);
}

getMaxAmplitudeForInputDevice12+

getMaxAmplitudeForInputDevice(inputDevice: AudioDeviceDescriptor): Promise<number>

Obtains the maximum amplitude (in the range [0, 1]) of the audio stream for an input device. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Audio.Volume

Parameters

NameTypeMandatoryDescription
inputDeviceAudioDeviceDescriptorYesDescriptor of the target device.

Return value

TypeDescription
Promise<number>Promise used to return the maximum amplitude, which is in the range [0, 1].

Error codes

For details about the error codes, see Universal Error Codes and Audio Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.
6800101Parameter verification failed. Return by promise.
6800301System error. Return by promise.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let capturerInfo: audio.AudioCapturerInfo = {
  source: audio.SourceType.SOURCE_TYPE_MIC, // Audio source type: microphone. Set this parameter based on the service scenario.
  capturerFlags: 0 // AudioCapturer flag.
};

audio.getAudioManager().getRoutingManager().getPreferredInputDeviceForCapturerInfo(capturerInfo).then((data) => {
  audioVolumeGroupManager.getMaxAmplitudeForInputDevice(data[0]).then((value) => {
    console.info(`Succeeded in getting maxAmplitude for input device. Amplitude: ${value}.`);
  }).catch((err: BusinessError) => {
    console.error(`Failed to get maxAmplitude for input device. Code: ${err.code}, message: ${err.message}`);
  })
}).catch((err: BusinessError) => {
  console.error(`Failed to get preferred input device for capturer info. Code: ${err.code}, message: ${err.message}`);
})

getMaxAmplitudeForOutputDevice12+

getMaxAmplitudeForOutputDevice(outputDevice: AudioDeviceDescriptor): Promise<number>

Obtains the maximum amplitude (in the range [0, 1]) of the audio stream for an output device. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Audio.Volume

Parameters

NameTypeMandatoryDescription
outputDeviceAudioDeviceDescriptorYesDescriptor of the target device.

Return value

TypeDescription
Promise<number>Promise used to return the maximum amplitude, which is in the range [0, 1].

Error codes

For details about the error codes, see Universal Error Codes and Audio Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.
6800101Parameter verification failed. Return by promise.
6800301System error. Return by promise.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let rendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // Audio stream usage type: music. Set this parameter based on the service scenario.
  rendererFlags: 0 // AudioRenderer flag.
};

audio.getAudioManager().getRoutingManager().getPreferOutputDeviceForRendererInfo(rendererInfo).then((data) => {
  audioVolumeGroupManager.getMaxAmplitudeForOutputDevice(data[0]).then((value) => {
    console.info(`Succeeded in getting maxAmplitude for input device. Amplitude: ${value}.`);
  }).catch((err: BusinessError) => {
    console.error(`Failed to get maxAmplitude for input device. Code: ${err.code}, message: ${err.message}`);
  })
}).catch((err: BusinessError) => {
  console.error(`Failed to get preferred input device for capturer info. Code: ${err.code}, message: ${err.message}`);
})

setMicrophoneMute(deprecated)

setMicrophoneMute(mute: boolean, callback: AsyncCallback<void>): void

Mutes or unmutes the microphone. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 9 and deprecated since API version 11. Its substitute is available only to system applications.

Required permissions: ohos.permission.MANAGE_AUDIO_CONFIG (available only for system applications)

System capability: SystemCapability.Multimedia.Audio.Volume

Parameters

NameTypeMandatoryDescription
mutebooleanYesMute status to set. true to mute, false otherwise.
callbackAsyncCallback<void>YesCallback used to return the result. If the operation is successful, err is undefined; otherwise, err is an error object.

Example

import { BusinessError } from '@kit.BasicServicesKit';

audioVolumeGroupManager.setMicrophoneMute(true, (err: BusinessError) => {
  if (err) {
    console.error(`Failed to set microphone mute. Code: ${err.code}, message: ${err.message}`);
    return;
  }
  console.info('Succeeded in setting microphone mute.');
});

setMicrophoneMute(deprecated)

setMicrophoneMute(mute: boolean): Promise<void>

Mutes or unmutes the microphone. This API uses a promise to return the result.

NOTE

This API is supported since API version 9 and deprecated since API version 11. Its substitute is available only to system applications.

Required permissions: ohos.permission.MANAGE_AUDIO_CONFIG (available only for system applications)

System capability: SystemCapability.Multimedia.Audio.Volume

Parameters

NameTypeMandatoryDescription
mutebooleanYesMute status to set. true to mute, false otherwise.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

audioVolumeGroupManager.setMicrophoneMute(true).then(() => {
  console.info('Succeeded in setting microphone mute.');
});

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 capi-native-audiostreambuilder-h

openharmony 鸿蒙 capi-ohaudiosuite

openharmony 鸿蒙 js-apis-inner-multimedia-systemSoundPlayer

openharmony 鸿蒙 arkts-apis-audio-i

openharmony 鸿蒙 capi-ohaudio

openharmony 鸿蒙 capi-ohmidi

openharmony 鸿蒙 arkts-apis-audio-AudioManager

openharmony 鸿蒙 capi-ohaudiosuite-oh-audiosuite-spacerenderpositionparams

openharmony 鸿蒙 capi-ohaudio-oh-audiostreaminfo

openharmony 鸿蒙 errorcode-ringtone

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/8TJl6IEi