音频管理
-
- 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+
导入模块
import audio from '@ohos.multimedia.audio';
权限
无
getAudioManager(): AudioManager
获取音频管理器。
返回值:
示例:
var audioManager = audio.getAudioManager();
AudioVolumeType
枚举,音频流类型。
DeviceFlag
枚举,可获取的设备种类。
DeviceRole
枚举,设备角色。
DeviceType
枚举,设备类型。
名称 | 默认值 | 描述 |
---|---|---|
INVALID | 0 | 无效设备。 |
EARPIECE | 1 | 听筒。 |
SPEAKER | 2 | 扬声器。 |
WIRED_HEADSET | 3 | 有线耳机。 |
BLUETOOTH_SCO | 7 | 蓝牙设备SCO连接(Synchronous Connection Oriented)。 |
BLUETOOTH_A2DP | 8 | 蓝牙设备A2DP连接(Advanced Audio Distribution Profile)。 |
MIC | 15 | 麦克风。 |
ActiveDeviceType7+
Enumerates active device types.
名称 | 默认值 | 描述 |
---|---|---|
SPEAKER | 2 | 扬声器。 |
BLUETOOTH_SCO | 7 | 蓝牙设备SCO连接(Synchronous Connection Oriented)。 |
AudioRingMode7+
枚举,铃声模式。
AudioManager
管理音频音量和音频设备。
setVolume(volumeType: AudioVolumeType, volume: number, callback: AsyncCallback<void>): void
设置指定流的音量,使用callback方式返回异步结果。
参数:
返回值:
无
示例:
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>
设置指定流的音量,使用promise方式返回异步结果。
参数:
返回值:
示例:
audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10).then(()=>
console.log('Promise returned to indicate a successful volume setting.');
)
getVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void
获取指定流的音量,使用callback方式返回异步结果。
参数:
返回值:
无
示例:
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>
获取指定流的音量,使用promise方式返回异步结果。
参数:
返回值:
示例:
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
获取指定流的最小音量,使用callback方式返回异步结果。
参数:
返回值:
无
示例:
audioManager.getVolume(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>
获取指定流的最小音量,使用promise方式返回异步结果。
参数:
返回值:
示例:
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
获取指定流的最大音量,使用callback方式返回异步结果。
参数:
返回值:
无
示例:
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>
获取指定流的最大音量,使用promise方式返回异步结果。
参数:
返回值:
示例:
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+
设置指定音量流静音,使用callback方式返回异步结果。
参数:
返回值:
无
示例:
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+
设置指定音量流静音,使用promise方式返回异步结果。
参数:
返回值:
示例:
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+
获取指定音量流是否被静音,使用callback方式返回异步结果。
参数:
返回值:
无
示例:
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+
获取指定音量流是否被静音,使用promise方式返回异步结果。
参数:
返回值:
示例:
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+
获取指定音量流是否为活跃状态,使用callback方式返回异步结果。
参数:
返回值:
无
示例:
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+
获取指定音量流是否为活跃状态,使用promise方式返回异步结果。
参数:
返回值:
示例:
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+
设置铃声模式,使用callback方式返回异步结果。
参数:
返回值:
无
示例:
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+
设置铃声模式,使用promise方式返回异步结果。
参数:
返回值:
示例:
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+
获取铃声模式,使用callback方式返回异步结果。
参数:
返回值:
无
示例:
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+
获取铃声模式,使用promise方式返回异步结果。
参数:
无
返回值:
示例:
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+
音频参数设置,使用callback方式返回异步结果。
参数:
返回值:
无
示例:
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+
音频参数设置,使用promise方式返回异步结果。
参数:
返回值:
示例:
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+
获取指定音频参数值,使用callback方式返回异步结果。
参数:
返回值:
无
示例:
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+
获取指定音频参数值,使用promise方式返回异步结果。
参数:
返回值:
示例:
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
获取音频设备列表,使用callback方式返回异步结果。
参数:
AsyncCallback<AudioDeviceDescriptors> |
示例:
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>
获取音频设备列表,使用promise方式返回异步结果。
参数:
返回值:
Promise<AudioDeviceDescriptors> |
示例:
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+
设置设备激活状态,使用callback方式返回异步结果。
参数:
返回值:
无
示例:
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+
设置设备激活状态,使用promise方式返回异步结果。
参数:
返回值:
示例:
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+
获取指定设备激活状态,使用callback方式返回异步结果。
参数:
返回值:
无
示例:
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+
获取指定设备激活状态,使用promise方式返回异步结果。
参数:
返回值:
示例:
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+
设置麦克风静音状态,使用callback方式返回异步结果。
参数:
返回值:
无
示例:
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+
设置麦克风静音状态,使用promise方式返回异步结果。
参数:
返回值:
示例:
audioManager.setMicrophoneMute(true).then(() =>
console.log('Promise returned to indicate that the microphone is muted.');
)
isMicrophoneMute(callback: AsyncCallback<boolean>): void7+
获取麦克风静音状态,使用callback方式返回异步结果。
参数:
返回值:
无
示例:
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+
获取麦克风静音状态,使用promise方式返回异步结果。
参数:
无
返回值:
示例:
audioManager.isMicrophoneMute().then((value) =>
console.log('Promise returned to indicate that the mute status of the microphone is obtained.', + value);
)
AudioDeviceDescriptor
描述音频设备。