Audio Management
- Modules to Import
- Required Permissions
- getAudioManager(): AudioManager
- AudioVolumeType
- DeviceFlag
- DeviceRole
- DeviceType
- ActiveDeviceType7+
- AudioRingMode7+
- AudioManager
- setVolume(volumeType: AudioVolumeType, volume: number, callback: AsyncCallback<void>): void
- setVolume(volumeType: AudioVolumeType, volume: number): Promise<void>
- getVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void
- getVolume(volumeType: AudioVolumeType): Promise<number>
- getMinVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void
- getMinVolume(volumeType: AudioVolumeType): Promise<number>
- getMaxVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void
- getMaxVolume(volumeType: AudioVolumeType): Promise<number>
- mute(volumeType: AudioVolumeType, mute: boolean, callback: AsyncCallback<void>): void7+
- mute(volumeType: AudioVolumeType, mute: boolean): Promise<void>7+
- isMute(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): void7+
- isMute(volumeType: AudioVolumeType): Promise<boolean>7+
- isActive(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): void7+
- isActive(volumeType: AudioVolumeType): Promise<boolean>7+
- setRingerMode(mode: AudioRingMode, callback: AsyncCallback<void>): void7+
- setRingerMode(mode: AudioRingMode): Promise<void>7+
- getRingerMode(callback: AsyncCallback<AudioRingMode>): void7+
- getRingerMode(): Promise<AudioRingMode>7+
- setAudioParameter(key: string, value: string, callback: AsyncCallback<void>): void7+
- setAudioParameter(key: string, value: string): Promise<void>7+
- getAudioParameter(key: string, callback: AsyncCallback<string>): void7+
- getAudioParameter(key: string): Promise<string>7+
- getDevices(deviceFlag: DeviceFlag, callback: AsyncCallback<AudioDeviceDescriptors>): void
- getDevices(deviceFlag: DeviceFlag): Promise<AudioDeviceDescriptors>
- setDeviceActive(deviceType: ActiveDeviceType, active: boolean, callback: AsyncCallback<void>): void7+
- setDeviceActive(deviceType: ActiveDeviceType, active: boolean): Promise<void>7+
- isDeviceActive(deviceType: ActiveDeviceType, callback: AsyncCallback<boolean>): void7+
- isDeviceActive(deviceType: ActiveDeviceType): Promise<boolean>7+
- setMicrophoneMute(mute: boolean, callback: AsyncCallback<void>): void7+
- setMicrophoneMute(mute: boolean): Promise<void>7+
- isMicrophoneMute(callback: AsyncCallback<boolean>): void7+
- isMicrophoneMute(): Promise<boolean>7+
- AudioDeviceDescriptor
- AudioDeviceDescriptors
Modules to Import
import audio from '@ohos.multimedia.audio';
Required Permissions
None
getAudioManager(): AudioManager
Obtains an AudioManager instance.
Return values
Example
var audioManager = audio.getAudioManager();
AudioVolumeType
Enumerates audio stream types.
DeviceFlag
Enumerates audio device flags.
DeviceRole
Enumerates audio device roles.
DeviceType
Enumerates audio device types.
Name | Default Value | Description |
---|---|---|
INVALID | 0 | Invalid device |
EARPIECE | 1 | Earpiece |
SPEAKER | 2 | Speaker |
WIRED_HEADSET | 3 | Wired headset |
BLUETOOTH_SCO | 7 | Bluetooth device using the synchronous connection oriented (SCO) link |
BLUETOOTH_A2DP | 8 | Bluetooth device using the advanced audio distribution profile (A2DP) |
MIC | 15 | Microphone |
ActiveDeviceType7+
Enumerates active device types.
Name | Default Value | Description |
---|---|---|
SPEAKER | 2 | Speaker |
BLUETOOTH_SCO | 7 | Bluetooth device using the synchronous connection oriented (SCO) link |
AudioRingMode7+
Enumerates ringer modes.
AudioManager
Implements audio volume and audio device management.
setVolume(volumeType: AudioVolumeType, volume: number, callback: AsyncCallback<void>): void
Sets the volume for a stream. This method uses an asynchronous callback to return the execution result.
Parameters
Volume to set. The value range can be obtained by calling getMinVolume and getMaxVolume. |
|||
Return values
None
Example
audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10, (err)=>{
if (err) {
console.error('Failed to set the volume. ${err.message}');
return;
}
console.log('Callback invoked to indicate a successful volume setting.');
})
setVolume(volumeType: AudioVolumeType, volume: number): Promise<void>
Sets the volume for a stream. This method uses a promise to return the execution result.
Parameters
Volume to set. The value range can be obtained by calling getMinVolume and getMaxVolume. |
Return values
Example
audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10).then(()=>
console.log('Promise returned to indicate a successful volume setting.');
)
getVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void
Obtains the volume of a stream. This method uses an asynchronous callback to return the query result.
Parameters
Return values
None
Example
audioManager.getVolume(audio.AudioVolumeType.MEDIA, (err, value) => {
if (err) {
console.error('Failed to obtain the volume. ${err.message}');
return;
}
console.log('Callback invoked to indicate that the volume is obtained.');
})
getVolume(volumeType: AudioVolumeType): Promise<number>
Obtains the volume of a stream. This method uses a promise to return the query result.
Parameters
Return values
Example
audioManager.getVolume(audio.AudioVolumeType.MEDIA).then((value) =>
console.log('Promise returned to indicate that the volume is obtained.' + value);
)
getMinVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void
Obtains the minimum volume allowed for a stream. This method uses an asynchronous callback to return the query result.
Parameters
Return values
None
Example
audioManager.getMinVolume(audio.AudioVolumeType.MEDIA, (err, value) => {
if (err) {
console.error('Failed to obtain the minimum volume. ${err.message}');
return;
}
console.log('Callback invoked to indicate that the minimum volume is obtained.' + value);
})
getMinVolume(volumeType: AudioVolumeType): Promise<number>
Obtains the minimum volume allowed for a stream. This method uses a promise to return the query result.
Parameters
Return values
Example
audioManager.getMinVolume(audio.AudioVolumeType.MEDIA).then((value) =>
console.log('Promised returned to indicate that the minimum volume is obtained.' + value);
)
getMaxVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void
Obtains the maximum volume allowed for a stream. This method uses an asynchronous callback to return the query result.
Parameters
Return values
None
Example
audioManager.getMaxVolume(audio.AudioVolumeType.MEDIA, (err, value) => {
if (err) {
console.error('Failed to obtain the maximum volume. ${err.message}');
return;
}
console.log('Callback invoked to indicate that the maximum volume is obtained.' + value);
})
getMaxVolume(volumeType: AudioVolumeType): Promise<number>
Obtains the maximum volume allowed for a stream. This method uses a promise to return the query result.
Parameters
Return values
Example
audioManager.getMaxVolume(audio.AudioVolumeType.MEDIA).then((data)=>
console.log('Promised returned to indicate that the maximum volume is obtained.');
)
mute(volumeType: AudioVolumeType, mute: boolean, callback: AsyncCallback<void>): void7+
Mutes a stream. This method uses an asynchronous callback to return the execution result.
Parameters
Mute status to set. The value true means to mute the stream, and false means the opposite. |
|||
Return values
None
Example
audioManager.mute(audio.AudioVolumeType.MEDIA, true, (err) => {
if (err) {
console.error('Failed to mute the stream. ${err.message}');
return;
}
console.log('Callback invoked to indicate that the stream is muted.');
})
mute(volumeType: AudioVolumeType, mute: boolean): Promise<void>7+
Mutes a stream. This method uses a promise to return the execution result.
Parameters
Mute status to set. The value true means to mute the stream, and false means the opposite. |
Return values
Example
audioManager.mute(audio.AudioVolumeType.MEDIA, true).then(() =>
console.log('Promise returned to indicate that the stream is muted.');
)
isMute(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): void7+
Checks whether a stream is muted. This method uses an asynchronous callback to return the query result.
Parameters
Callback used to return the mute status of the stream. The value true means that the stream is muted, and false means the opposite. |
Return values
None
Example
audioManager.isMute(audio.AudioVolumeType.MEDIA, (err, value) => {
if (err) {
console.error('Failed to obtain the mute status. ${err.message}');
return;
}
console.log('Callback invoked to indicate that the mute status of the stream is obtained.' + value);
})
isMute(volumeType: AudioVolumeType): Promise<boolean>7+
Checks whether a stream is muted. This method uses a promise to return the execution result.
Parameters
Return values
Promise used to return the mute status of the stream. The value true means that the stream is muted, and false means the opposite. |
Example
audioManager.isMute(audio.AudioVolumeType.MEDIA).then((value) =>
console.log('Promise returned to indicate that the mute status of the stream is obtained.' + value);
)
isActive(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): void7+
Checks whether a stream is active. This method uses an asynchronous callback to return the query result.
Parameters
Callback used to return the active status of the stream. The value true means that the stream is active, and false means the opposite. |
Return values
None
Example
audioManager.isActive(audio.AudioVolumeType.MEDIA, (err, value) => {
if (err) {
console.error('Failed to obtain the active status of the stream. ${err.message}');
return;
}
console.log('Callback invoked to indicate that the active status of the stream is obtained.' + value);
})
isActive(volumeType: AudioVolumeType): Promise<boolean>7+
Checks whether a stream is active. This method uses a promise to return the query result.
Parameters
Return values
Promise used to return the active status of the stream. The value true means that the stream is active, and false means the opposite. |
Example
audioManager.isActive(audio.AudioVolumeType.MEDIA).then((value) =>
console.log('Promise returned to indicate that the active status of the stream is obtained.' + value);
)
setRingerMode(mode: AudioRingMode, callback: AsyncCallback<void>): void7+
Sets the ringer mode. This method uses an asynchronous callback to return the execution result.
Parameters
Return values
None
Example
audioManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL, (err) => {
if (err) {
console.error('Failed to set the ringer mode. ${err.message}');
return;
}
console.log('Callback invoked to indicate a successful setting of the ringer mode.');
})
setRingerMode(mode: AudioRingMode): Promise<void>7+
Sets the ringer mode. This method uses a promise to return the execution result.
Parameters
Return values
Example
audioManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL).then(() =>
console.log('Promise returned to indicate a successful setting of the ringer mode.');
)
getRingerMode(callback: AsyncCallback<AudioRingMode>): void7+
Obtains the ringer mode. This method uses an asynchronous callback to return the query result.
Parameters
AsyncCallback<AudioRingMode> |
Return values
None
Example
audioManager.getRingerMode((err, value) => {
if (err) {
console.error('Failed to obtain the ringer mode. ${err.message}');
return;
}
console.log('Callback invoked to indicate that the ringer mode is obtained.' + value);
})
getRingerMode(): Promise<AudioRingMode>7+
Obtains the ringer mode. This method uses a promise to return the query result.
Parameters
None
Return values
Promise<AudioRingMode> |
Example
audioManager.getRingerMode().then((value) =>
console.log('Promise returned to indicate that the ringer mode is obtained.' + value);
)
setAudioParameter(key: string, value: string, callback: AsyncCallback<void>): void7+
Sets an audio parameter. This method uses an asynchronous callback to return the execution result.
Parameters
Return values
None
Example
audioManager.setAudioParameter('PBits per sample', '8 bit', (err) => {
if (err) {
console.error('Failed to set the audio parameter. ${err.message}');
return;
}
console.log('Callback invoked to indicate a successful setting of the audio parameter.');
})
setAudioParameter(key: string, value: string): Promise<void>7+
Sets an audio parameter. This method uses a promise to return the execution result.
Parameters
Return values
Example
audioManager.setAudioParameter('PBits per sample', '8 bit').then(() =>
console.log('Promise returned to indicate a successful setting of the audio parameter.');
)
getAudioParameter(key: string, callback: AsyncCallback<string>): void7+
Obtains the value of an audio parameter. This method uses an asynchronous callback to return the query result.
Parameters
Return values
None
Example
audioManager.getAudioParameter('PBits per sample', (err, value) => {
if (err) {
console.error('Failed to obtain the value of the audio parameter. ${err.message}');
return;
}
console.log('Callback invoked to indicate that the value of the audio parameter is obtained.' + value);
})
getAudioParameter(key: string): Promise<string>7+
Obtains the value of an audio parameter. This method uses a promise to return the query result.
Parameters
Return values
Example
audioManager.getAudioParameter('PBits per sample').then((value) =>
console.log('Promise returned to indicate that the value of the audio parameter is obtained.' + value);
)
getDevices(deviceFlag: DeviceFlag, callback: AsyncCallback<AudioDeviceDescriptors>): void
Obtains the audio devices with a specific flag. This method uses an asynchronous callback to return the query result.
Parameters
AsyncCallback<AudioDeviceDescriptors> |
Return values
None
Example
audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG, (err, value)=>{
if (err) {
console.error('Failed to obtain the device list. ${err.message}');
return;
}
console.log('Callback invoked to indicate that the device list is obtained.');
})
getDevices(deviceFlag: DeviceFlag): Promise<AudioDeviceDescriptors>
Obtains the audio devices with a specific flag. This method uses a promise to return the query result.
Parameters
Return values
Promise<AudioDeviceDescriptors> |
Example
audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data)=>
console.log('Promise returned to indicate that the device list is obtained.');
)
setDeviceActive(deviceType: ActiveDeviceType, active: boolean, callback: AsyncCallback<void>): void7+
Sets a device to the active state. This method uses an asynchronous callback to return the execution result.
Parameters
Active status to set. The value true means to set the device to the active status, and false means the opposite. |
|||
Return values
None
Example
audioManager.setDeviceActive(audio.ActiveDeviceType.SPEAKER, true, (err)=> {
if (err) {
console.error('Failed to set the active status of the device. ${err.message}');
return;
}
console.log('Callback invoked to indicate that the device is set to the active status.');
})
setDeviceActive(deviceType: ActiveDeviceType, active: boolean): Promise<void>7+
Sets a device to the active state. This method uses a promise to return the execution result.
Parameters
Active status to set. The value true means to set the device to the active status, and false means the opposite. |
Return values
Example
audioManager.setDeviceActive(audio.ActiveDeviceType.SPEAKER, true).then(()=>
console.log('Promise returned to indicate that the device is set to the active status.');
)
isDeviceActive(deviceType: DeviceType, callback: AsyncCallback<boolean>): void7+
Checks whether a device is active. This method uses an asynchronous callback to return the query result.
Parameters
Return values
None
Example
audioManager.isDeviceActive(audio.ActiveDeviceType.SPEAKER, (err, value) => {
if (err) {
console.error('Failed to obtain the active status of the device. ${err.message}');
return;
}
console.log('Callback invoked to indicate that the active status of the device is obtained.');
})
isDeviceActive(deviceType: ActiveDeviceType): Promise<boolean>7+
Checks whether a device is active. This method uses a promise to return the query result.
Parameters
Return values
Example
audioManager.isDeviceActive(audio.ActiveDeviceType.SPEAKER).then((value) =>
console.log('Promise returned to indicate that the active status of the device is obtained.' + value);
)
setMicrophoneMute(mute: boolean, callback: AsyncCallback<void>): void7+
Mutes or unmutes the microphone. This method uses an asynchronous callback to return the execution result.
Parameters
Mute status to set. The value true means to mute the microphone, and false means the opposite. |
|||
Return values
None
Example
audioManager.setMicrophoneMute(true, (err) => {
if (err) {
console.error('Failed to mute the microphone. ${err.message}');
return;
}
console.log('Callback invoked to indicate that the microphone is muted.');
})
setMicrophoneMute(mute: boolean): Promise<void>7+
Mutes or unmutes the microphone. This method uses a promise to return the execution result.
Parameters
Mute status to set. The value true means to mute the microphone, and false means the opposite. |
Return values
Example
audioManager.setMicrophoneMute(true).then(() =>
console.log('Promise returned to indicate that the microphone is muted.');
)
isMicrophoneMute(callback: AsyncCallback<boolean>): void7+
Checks whether the microphone is muted. This method uses an asynchronous callback to return the query result.
Parameters
Callback used to return the mute status of the microphone. The value true means that the microphone is muted, and false means the opposite. |
Return values
None
Example
audioManager.isMicrophoneMute((err, value) => {
if (err) {
console.error('Failed to obtain the mute status of the microphone. ${err.message}');
return;
}
console.log('Callback invoked to indicate that the mute status of the microphone is obtained.' + value);
})
isMicrophoneMute(): Promise<boolean>7+
Checks whether the microphone is muted. This method uses a promise to return the query result.
Parameters
None
Return values
Promise used to return the mute status of the microphone. The value true means that the microphone is muted, and false means the opposite. |
Example
audioManager.isMicrophoneMute().then((value) =>
console.log('Promise returned to indicate that the mute status of the microphone is obtained.', + value);
)
AudioDeviceDescriptor
Describes an audio device.