Development Guidelines on Audio Management

When to Use

You use audio management APIs to set and obtain volume, and get information about input/output devices.

Available APIs

Table 1 Audio

API

Description

getAudioManager(): AudioManager

Obtains an AudioManager instance.

AudioManager

Manages audio volume and audio device information.

AudioDeviceDescriptor

Describes audio devices.

AudioVolumeType

Enumerates audio volume types.

DeviceFlag

Enumerates flags of supported devices.

DeviceRole

Enumerates device roles.

DeviceType

Enumerates device types.

Table 2 AudioManager methods

Method

Description

setVolume(audioType: AudioVolumeType,volume: number,callback: AsyncCallback<void>): void

Sets the volume of a stream asynchronously and uses a callback to return the execution result.

setVolume(audioType: AudioVolumeType,volume: number): Promise<void>

Sets the volume of a stream asynchronously and uses a Promise to return the execution result.

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

Obtains the volume of a stream asynchronously and uses a callback to return the execution result.

getVolume(audioType: AudioVolumeType): Promise<number>

Obtains the volume of a stream asynchronously and uses a Promise to return the execution result.

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

Obtains the minimum volume of a stream asynchronously and uses a callback to return the execution result.

getMinVolume(audioType: AudioVolumeType): Promise<number>

Obtains the minimum volume of a stream asynchronously and uses a Promise to return the execution result.

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

Obtains the maximum volume of a stream asynchronously and uses a callback to return the execution result.

getMaxVolume(audioType: AudioVolumeType): Promise<number>

Obtains the maximum volume of a stream asynchronously and uses a Promise to return the execution result.

getDevices(deviceFlag: DeviceFlag, callback: AsyncCallback<AudioDeviceDescriptors>): void

Obtains the device list asynchronously and uses a callback to return the execution result.

getDevices(deviceFlag: DeviceFlag): Promise<AudioDeviceDescriptors>

Obtains the device list asynchronously and uses a Promise to return the execution result.

Table 3 AudioDeviceDescriptor attributes

Parameter

Description

deviceRole: DeviceRole

Audio device role

deviceType: DeviceType

Audio device type

Table 4 AudioVolumeType enums

Enum

Description

MEDIA = 1

Audio streams for media purpose

RINGTONE = 2

Audio streams for ring tones

Table 5 DeviceFlag enums

Enum

Description

OUTPUT_DEVICES_FLAG = 1

Output devices

INPUT_DEVICES_FLAG = 2

Input devices

ALL_DEVICES_FLAG = 3

All devices

Table 6 DeviceRole enums

Enum

Description

INPUT_DEVICE = 1

Input role

OUTPUT_DEVICE = 2

Output role

Table 7 DeviceType enums

Enum

Description

INVALID = 0

Invalid device

SPEAKER = 1

Speaker

WIRED_HEADSET = 2

Wired headset

BLUETOOTH_SCO = 3

Bluetooth device using the synchronous connection oriented link (SCO)

BLUETOOTH_A2DP = 4

Bluetooth device using advanced audio distribution profile (A2DP)

MIC = 5

Microphone

Development Procedure

  1. Obtain an audio manager.

    const audioManager = audio.getAudioManager();
    
  2. Set the audio stream volume.

    audioManager.getVolume(audio.AudioVolumeType.MEDIA, (err, value) => {
       if (err) {
    	   console.error(`failed to get volume ${err.message}`);
    	   return;
       }
       console.log(`Media getVolume  ${value}`);
    });