public class AudioManager extends Object
An AudioManager instance adjusts the global volume, mutes streams, controls the audio version,
sets the ringer mode (sound, silent, or vibration), and enables or disables audio devices.
SystemCapability.Multimedia#AUDIO| 限定符和类型 | 类 | 说明 |
|---|---|---|
static class |
AudioManager.AudioRingMode |
Describes ringer modes.
|
static class |
AudioManager.AudioVolumeType |
Describes audio stream types.
|
static class |
AudioManager.CallState |
Describes call states.
|
static class |
AudioManager.RingtoneType |
Describes ringtone types.
|
| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static int |
DEFAULT_VOLUME |
Indicates the default volume.
|
static int |
DEVICE_ID_BLUETOOTH |
Indicates a Bluetooth device.
|
static int |
DEVICE_ID_SPEAKERPHONE |
Indicates a speaker device.
|
static int |
DEVICE_ID_WIRED_HEADSET |
Indicates a wired headset device.
|
static int |
MARKER_WITH_SOUND |
Indicates that a sound is played when the volume is adjusted.
|
static int |
MARKER_WITH_VOLUME_BAR |
Indicates that a volume bar is shown when the volume is adjusted.
|
static int |
RINGER_MODE_MAX |
Indicates the maximum valid volume of the sound mode.
|
static int |
VOLUME_OP_DEC |
Indicates a volume decrease.
|
static int |
VOLUME_OP_INC |
Indicates a volume increase.
|
static int |
VOLUME_OP_MAINTAIN |
Indicates that the volume remains unchanged.
|
| 构造器 | 说明 |
|---|---|
AudioManager() |
A constructor used to create an
AudioManager instance |
AudioManager(String packageName) |
A constructor used to create an
AudioManager instance according to the name of
an application installation package. |
AudioManager(Context context) |
A constructor used to create an
AudioManager instance based on the application context. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
boolean |
activateAudioInterrupt(AudioInterrupt interrupt) |
Activates audio interruption.
|
void |
attachAudioEffects() |
Loads sound effect resources.
|
boolean |
changeVolumeBy(AudioManager.AudioVolumeType volumeType,
int index) |
Increases or decreases the current volume by a certain amount.
|
boolean |
connectBluetoothSco() |
Connects to the Bluetooth SCO channel.
|
boolean |
deactivateAudioInterrupt(AudioInterrupt interrupt) |
Deactivates audio interruption.
|
void |
detachAudioEffects() |
Detaches sound effect resources.
|
boolean |
disconnectBluetoothSco() |
Disconnects from the Bluetooth SCO channel.
|
List<AudioCapturerConfig> |
getActiveCapturerConfigs() |
Obtains currently active capturer configurations of the device.
|
List<AudioRendererInfo> |
getActiveRendererConfigs() |
Obtains information about the active audio streams, including the usage type, content type, and flags.
|
String |
getAudioParameter(String key) |
Obtains the value of audio device parameters specified by
key. |
AudioManager.CallState |
getCallState() |
Obtains the current call state.
|
static AudioDeviceDescriptor[] |
getDevices(AudioDeviceDescriptor.DeviceFlag flag) |
Obtains information about audio input or output devices, or that about all audio devices.
|
static int |
getMasterOutputFrameCount() |
Obtains the number of frames in the buffer of the master output device.
|
static int |
getMasterOutputSampleRate() |
Obtains the sampling rate of the master output device.
|
int |
getMaxVolume(AudioManager.AudioVolumeType volumeType) |
Obtains the maximum volume level of a specified type of audio streams.
|
int |
getMinVolume(AudioManager.AudioVolumeType volumeType) |
Obtains the minimum volume level of a specified type of audio streams.
|
int |
getRingerMode() |
Obtains the ringer mode.
|
static Uri |
getRingerUri(Context context,
AudioManager.RingtoneType type) |
Obtains the
Uri of a specified RingtoneType. |
int |
getVersion() |
Obtains the audio kit version.
|
int |
getVolume(AudioManager.AudioVolumeType volumeType) |
Obtains the volume level of a specified type of audio streams.
|
boolean |
isBluetoothScoUsableNotInCall(Context context) |
Checks whether the current device supports synchronous connection-oriented (SCO) links when it is not in a call.
|
boolean |
isDeviceActive(int deviceType) |
Checks whether a device is turned on or off.
|
boolean |
isMasterMute() |
Checks whether audio streams are muted globally.
|
boolean |
isMicrophoneMute() |
Checks whether a microphone is muted.
|
boolean |
isMute(AudioManager.AudioVolumeType volumeType) |
Checks whether a specified type of streams are muted.
|
static boolean |
isStreamActive(AudioManager.AudioVolumeType volumeType) |
Checks whether audio streams of a specified type are active.
|
boolean |
isVolumeChangeForbidden(Context context) |
Checks whether device volume is fixed.
|
static int |
makeSessionId() |
Creates a session ID to be used by
AudioRendererInfo.Builder#sessionID(int) for setting audio
playback parameters and AudioCapturerInfo.Builder#sessionID(int) for setting recording parameters. |
boolean |
mute(AudioManager.AudioVolumeType volumeType) |
Mutes a specified type of streams.
|
void |
registerAudioCapturerCallback(AudioCapturerCallback cb) |
Registers a callback to receive notifications about capturer configuration changes.
|
void |
registerAudioRendererCallback(AudioRendererCallback cb) |
Registers an audio playback callback for the current device.
|
void |
removeAudioDeviceChangeObserver(AudioDeviceChangeObserver observer) |
Removes the
AudioDeviceChangeObserver object that is registered with the system via the
setAudioDeviceChangeObserver(ohos.media.audio.AudioDeviceChangeObserver) method. |
void |
setAudioDeviceChangeObserver(AudioDeviceChangeObserver observer) |
Sets an observer to listen for state changes of an audio device, for example, when it becomes connected or
disconnected.
|
boolean |
setAudioParameter(String key,
String value) |
Sets values of a variable number of parameters for audio devices.
|
void |
setCallState(AudioManager.CallState callState) |
Sets the current call state.
|
boolean |
setDeviceActive(int deviceType,
boolean state) |
Turns on or off a device.
|
boolean |
setMasterMute(boolean isMute) |
Mutes or unmutes global audio streams.
|
boolean |
setMicrophoneMute(boolean isMute) |
Mutes or unmutes a microphone.
|
boolean |
setRingerMode(AudioManager.AudioRingMode mode) |
Sets the ringer mode.
|
static void |
setRingerUri(Context context,
AudioManager.RingtoneType type,
Uri uri) |
Sets the
Uri of a specified RingtoneType. |
boolean |
setVolume(AudioManager.AudioVolumeType volumeType,
int volume) |
Sets the volume level of a specified type of streams.
|
boolean |
setVolume(AudioManager.AudioVolumeType volumeType,
int volume,
int markers) |
Sets the volume for an audio stream of the specified type.
|
boolean |
unmute(AudioManager.AudioVolumeType volumeType) |
Unmutes a specified type of streams.
|
void |
unregisterAudioCapturerCallback(AudioCapturerCallback cb) |
Unregisters the callback that has been registered with
registerAudioCapturerCallback(ohos.media.audio.AudioCapturerCallback). |
void |
unregisterAudioRendererCallback(AudioRendererCallback cb) |
Deregisters an audio playback callback listening the current device.
|
public static final int DEFAULT_VOLUME
public static final int DEVICE_ID_BLUETOOTH
public static final int DEVICE_ID_SPEAKERPHONE
public static final int DEVICE_ID_WIRED_HEADSET
public static final int MARKER_WITH_SOUND
public static final int MARKER_WITH_VOLUME_BAR
public static final int RINGER_MODE_MAX
public static final int VOLUME_OP_DEC
public static final int VOLUME_OP_INC
public static final int VOLUME_OP_MAINTAIN
public AudioManager()
AudioManager instancepublic AudioManager(String packageName)
AudioManager instance according to the name of
an application installation package.packageName - Indicates the name of an application installation package.public AudioManager(Context context)
AudioManager instance based on the application context.context - Indicates the application context.public int getVersion()
public boolean setVolume(AudioManager.AudioVolumeType volumeType, int volume)
volumeType - Indicates the type of streams for which the volume level is to set.volume - Indicates the volume level to set. You can obtain the maximum level value by calling
getMaxVolume(ohos.media.audio.AudioManager.AudioVolumeType), and the minimum by
getMinVolume(ohos.media.audio.AudioManager.AudioVolumeType).true if the setting is successful; returns false otherwise.getMaxVolume(AudioVolumeType),
getMinVolume(AudioVolumeType),
getVolume(AudioVolumeType)public boolean setVolume(AudioManager.AudioVolumeType volumeType, int volume, int markers)
volumeType - Indicates the type of the audio stream whose volume needs to be set.volume - Indicates the volume to set. You can use getMaxVolume(ohos.media.audio.AudioManager.AudioVolumeType) and
getMinVolume(ohos.media.audio.AudioManager.AudioVolumeType) to obtain the maximum volume and minimum volume of a
specific audio stream, respectively.markers - Indicates the markers. This parameter can be set to the values of
MARKER_WITH_VOLUME_BAR, the values ofMARKER_WITH_SOUND, and their
combinations. The value 0 means that no marker is set.true if the setting is successful; returns false otherwise.getMaxVolume(AudioVolumeType),
getMinVolume(AudioVolumeType),
getVolume(AudioVolumeType)public int getVolume(AudioManager.AudioVolumeType volumeType) throws AudioRemoteException
volumeType - Indicates the stream type.AudioRemoteException - Throws this exception if the volume level fails to be obtained.getMaxVolume(AudioVolumeType),
getMinVolume(AudioVolumeType),
setVolume(AudioVolumeType, int)public int getMinVolume(AudioManager.AudioVolumeType volumeType) throws AudioRemoteException
volumeType - Indicates the stream type.AudioRemoteException - Throws this exception if the minimum volume level fails to be obtained.getVolume(AudioVolumeType)public int getMaxVolume(AudioManager.AudioVolumeType volumeType) throws AudioRemoteException
volumeType - Indicates the stream type.AudioRemoteException - Throws this exception if the maximum volume level fails to be obtained.getVolume(AudioVolumeType)public boolean mute(AudioManager.AudioVolumeType volumeType)
volumeType - Indicates the type of streams to mute.true if the streams are muted; returns false otherwise.public boolean unmute(AudioManager.AudioVolumeType volumeType)
volumeType - Indicates the type of streams to unmute.true if the streams are unmuted; returns false otherwise.public boolean isMute(AudioManager.AudioVolumeType volumeType) throws AudioRemoteException
volumeType - Indicates the stream type.true if the streams are muted; returns false otherwise.AudioRemoteException - Throws this exception if the check fails.mute(AudioVolumeType),
unmute(AudioVolumeType)public boolean setRingerMode(AudioManager.AudioRingMode mode)
In silent mode, a device is silent and does not vibrate. In vibration mode, a device is silent and vibrates. In sound mode, a device is audible and may vibrate (depending on user settings).
mode - Indicates the ringer mode, which can be AudioRingMode#RINGER_MODE_NORMAL,
AudioRingMode#RINGER_MODE_SILENT, or AudioRingMode#RINGER_MODE_VIBRATE.true if the setting is successful; returns false otherwise.getRingerMode()public int getRingerMode()
throws AudioRemoteException
AudioRingMode#RINGER_MODE_NORMAL,
AudioRingMode#RINGER_MODE_SILENT, or AudioRingMode#RINGER_MODE_VIBRATE.AudioRemoteException - Throws this exception if the ringer mode fails to be obtained.setRingerMode(AudioRingMode)public boolean setDeviceActive(int deviceType,
boolean state)
deviceType - Indicates the device type, which can be DEVICE_ID_SPEAKERPHONE
or DEVICE_ID_BLUETOOTH.state - Indicates the turn-on or turn-off state.true if the setting is successful; returns false otherwise.public boolean isDeviceActive(int deviceType)
throws AudioRemoteException
deviceType - Indicates the device type, which can be DEVICE_ID_SPEAKERPHONE,
DEVICE_ID_BLUETOOTH or DEVICE_ID_WIRED_HEADSET.true if the device is turned on; returns false otherwise.AudioRemoteException - Throws this exception if the device state fails to be obtained.public boolean activateAudioInterrupt(AudioInterrupt interrupt)
Before playing audio, an application sends an interruption request to other applications for pausing audio playback or decreasing the volume, preventing audio conflicts.
interrupt - Indicates the interruption data structure, including interruption information and the
interruption listener.true if audio interruption is activated; returns false otherwise.deactivateAudioInterrupt(AudioInterrupt),
AudioInterruptpublic boolean deactivateAudioInterrupt(AudioInterrupt interrupt)
If an application does not need to receive interruption messages, call this method to deactivate audio interruption for the application.
interrupt - Indicates the interruption data structure, including interruption information and the
interruption listener.true if audio interruption is deactivated; returns false otherwise.activateAudioInterrupt(AudioInterrupt),
AudioInterruptpublic boolean setAudioParameter(String key, String value)
key - Indicates the key of parameters to be set.value - Indicates the value of parameters.true if the setting is successful; returns false otherwise.public String getAudioParameter(String key)
key.key - Indicates the key parameter.public void registerAudioCapturerCallback(AudioCapturerCallback cb) throws IllegalArgumentException
cb - Indicates the non-null callback to register.IllegalArgumentException - Throws this exception if cb is null.public void registerAudioRendererCallback(AudioRendererCallback cb)
cb - Indicates the audio playback callback. You need to implement this callback in your application.public void setAudioDeviceChangeObserver(AudioDeviceChangeObserver observer)
You can call this method to obtain the device changes in a timely manner.
observer - Indicates the observer to set.public void removeAudioDeviceChangeObserver(AudioDeviceChangeObserver observer)
AudioDeviceChangeObserver object that is registered with the system via the
setAudioDeviceChangeObserver(ohos.media.audio.AudioDeviceChangeObserver) method. The observer
object receives notifications of the status changes of an audio device connected to the system.observer - Indicates the AudioDeviceChangeObserver object to remove.public void unregisterAudioCapturerCallback(AudioCapturerCallback cb)
registerAudioCapturerCallback(ohos.media.audio.AudioCapturerCallback).cb - Indicates the non-null callback to unregister.public void unregisterAudioRendererCallback(AudioRendererCallback cb)
cb - Indicates the audio playback callback, which has been registered
by registerAudioRendererCallback(ohos.media.audio.AudioRendererCallback).public List<AudioCapturerConfig> getActiveCapturerConfigs()
public List<AudioRendererInfo> getActiveRendererConfigs()
public static AudioDeviceDescriptor[] getDevices(AudioDeviceDescriptor.DeviceFlag flag)
flag - Indicates input or output devices, or all audio devices.AudioDeviceDescriptor instance if
the information is obtained; returns an array whose length is 0 otherwise.public boolean setMasterMute(boolean isMute)
isMute - Indicates whether to mute global audio streams. Value true means global audio streams
will be muted, and false means the opposite.true if the setting is successful; returns false otherwise.public boolean setMicrophoneMute(boolean isMute)
isMute - Indicates whether to mute a microphone. Value true means that the microphone
will be muted, and false means the opposite.true if the setting is successful; returns false otherwise.public boolean isMicrophoneMute()
true if the microphone is muted; returns false otherwise.public static Uri getRingerUri(Context context, AudioManager.RingtoneType type)
Uri of a specified RingtoneType.context - Indicates the context used for a search.type - Indicates the ringtone type.Uri object matching the RingToneType.public static void setRingerUri(Context context, AudioManager.RingtoneType type, Uri uri)
Uri of a specified RingtoneType.context - Indicates the context used for a search.type - Indicates the ringtone type.uri - Indicates the Uri object to set.public AudioManager.CallState getCallState() throws AudioRemoteException
AudioRemoteException - Throws this exception if the call state fails to be obtained.public void setCallState(AudioManager.CallState callState)
callState - Indicates the call state to set, which is defined in CallState.public boolean changeVolumeBy(AudioManager.AudioVolumeType volumeType, int index)
If the target stream volume exceeds the maximum or is lower than the minimum allowed, the volume will be set to the maximum or minimum volume.
volumeType - Indicates the audio stream type.index - Indicates the volume change relative to the original volume. The value can be -1,
0, or 1. Value 1 indicates that the volume is increased by one level,
-1 indicates that the volume is decreased by one level,
and 0 indicates that the volume remains unchanged.true if the volume is changed; returns false otherwise.public static int getMasterOutputSampleRate()
public static int getMasterOutputFrameCount()
public static boolean isStreamActive(AudioManager.AudioVolumeType volumeType)
volumeType - Indicates the audio stream type.true if the specified audio streams are active; returns false otherwise.public boolean connectBluetoothSco()
true if the connection is successful; returns false otherwise.public boolean disconnectBluetoothSco()
true if the disconnection is successful; returns false otherwise.public static int makeSessionId()
AudioRendererInfo.Builder#sessionID(int) for setting audio
playback parameters and AudioCapturerInfo.Builder#sessionID(int) for setting recording parameters.-1 otherwise.public boolean isMasterMute()
throws AudioRemoteException
true if all audio streams are muted; returns false otherwise.AudioRemoteException - Throws this exception if the check fails.public void attachAudioEffects()
throws AudioRemoteException
AudioRemoteException - Throws this exception if the sound effects fail to be loaded.public void detachAudioEffects()
throws AudioRemoteException
AudioRemoteException - Throws this exception if the sound effects fail to be detached.public boolean isVolumeChangeForbidden(Context context) throws AudioRemoteException
Some devices may not support volume adjustment or may work at a fixed volume. You cannot mute or adjust volume
of audio streams for these devices. In this case, this method returns true.
context - Indicates the application context.true if the device volume is fixed; returns false otherwise.AudioRemoteException - Throws this exception if the operation fails.public boolean isBluetoothScoUsableNotInCall(Context context) throws AudioRemoteException
context - Indicates the application context.true if SCO links are available when the device is not in a call;
returns false otherwise.AudioRemoteException - Throws this exception if the operation fails.