Android
android.media
public class

android.media.AudioManager

java.lang.Object
android.media.AudioManager

AudioManager provides access to volume and ringer mode control.

Use Context.getSystemService(Context.AUDIO_SERVICE) to get an instance of this class.

Summary

Constants

      Value  
int  ADJUST_LOWER  Decrease the ringer volume.  -1  0xffffffff 
int  ADJUST_RAISE  Increase the ringer volume.  0x00000001 
int  ADJUST_SAME  Maintain the previous ringer volume.  0x00000000 
String  EXTRA_RINGER_MODE  The new ringer mode.  "android.media.EXTRA_RINGER_MODE" 
String  EXTRA_VIBRATE_SETTING  The new vibrate setting for a particular type.  "android.media.EXTRA_VIBRATE_SETTING" 
String  EXTRA_VIBRATE_TYPE  The vibrate type whose setting has changed.  "android.media.EXTRA_VIBRATE_TYPE" 
int  FLAG_ALLOW_RINGER_MODES  Whether to include ringer modes as possible options when changing volume.  0x00000002 
int  FLAG_PLAY_SOUND  Whether to play a sound when changing the volume.  0x00000004 
int  FLAG_REMOVE_SOUND_AND_VIBRATE  Removes any sounds/vibrate that may be in the queue, or are playing (related to changing volume).  0x00000008 
int  FLAG_SHOW_UI  Show a toast containing the current volume.  0x00000001 
int  FLAG_VIBRATE  Whether to vibrate if going into the vibrate ringer mode.  16  0x00000010 
int  FX_FLING_END_BOTTOM  We have hit bottom of list while flinging  0x00000008 
int  FX_FLING_END_TOP  We have hit top of list while flinging  0x00000007 
int  FX_FOCUS_NAVIGATION_DOWN  Focuse has moved down  0x00000002 
int  FX_FOCUS_NAVIGATION_LEFT  Focuse has moved left  0x00000003 
int  FX_FOCUS_NAVIGATION_RIGHT  Focuse has moved right  0x00000004 
int  FX_FOCUS_NAVIGATION_UP  Focuse has moved up  0x00000001 
int  FX_KEY_CLICK  Keyboard and direction pad click sound  0x00000000 
int  FX_LID_CLOSE  Lid close sound  0x00000006 
int  FX_LID_OPEN  Lid open sound  0x00000005 
int  MODE_CURRENT  Current audio mode.  -1  0xffffffff 
int  MODE_INVALID  Invalid audio mode.  -2  0xfffffffe 
int  MODE_IN_CALL  In call audio mode.  0x00000002 
int  MODE_NORMAL  Normal audio mode: not ringing and no call established.  0x00000000 
int  MODE_RINGTONE  Ringing audio mode.  0x00000001 
int  NUM_STREAMS  Number of audio streams   0x00000005 
String  RINGER_MODE_CHANGED_ACTION  Sticky broadcast intent action indicating that the ringer mode has changed.  "android.media.RINGER_MODE_CHANGED" 
int  RINGER_MODE_NORMAL  Ringer mode that may be audible and may vibrate.  0x00000002 
int  RINGER_MODE_SILENT  Ringer mode that will be silent and will not vibrate.  0x00000000 
int  RINGER_MODE_VIBRATE  Ringer mode that will be silent and will vibrate.  0x00000001 
int  ROUTE_ALL  Used for mask parameter of setRouting(int, int, int) 15  0x0000000f 
int  ROUTE_BLUETOOTH  Routing audio output to bluetooth   0x00000004 
int  ROUTE_EARPIECE  Routing audio output to earpiece   0x00000001 
int  ROUTE_HEADSET  Routing audio output to headset   0x00000008 
int  ROUTE_SPEAKER  Routing audio output to spaker   0x00000002 
int  STREAM_ALARM  The audio stream for alarms   0x00000004 
int  STREAM_MUSIC  The audio stream for music playback   0x00000003 
int  STREAM_RING  The audio stream for the phone ring and message alerts   0x00000002 
int  STREAM_SYSTEM  The audio stream for system sounds   0x00000001 
int  STREAM_VOICE_CALL  The audio stream for phone calls   0x00000000 
int  USE_DEFAULT_STREAM_TYPE  Suggests using the default stream type.  -2147483648  0x80000000 
String  VIBRATE_SETTING_CHANGED_ACTION  Broadcast intent action indicating that the vibrate setting has changed.  "android.media.VIBRATE_SETTING_CHANGED" 
int  VIBRATE_SETTING_OFF  Vibrate setting that suggests to never vibrate.  0x00000000 
int  VIBRATE_SETTING_ON  Vibrate setting that suggests to vibrate when possible.  0x00000001 
int  VIBRATE_SETTING_ONLY_SILENT  Vibrate setting that suggests to only vibrate when in the vibrate ringer mode.  0x00000002 
int  VIBRATE_TYPE_NOTIFICATION  Vibrate type that corresponds to notifications.  0x00000001 
int  VIBRATE_TYPE_RINGER  Vibrate type that corresponds to the ringer.  0x00000000 

Public Methods

          void  adjustStreamVolume(int streamType, int direction, int flags)
Adjusts the volume of a particular stream by one step in a direction.
          void  adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags)
Adjusts the volume of the most relevant stream, or the given fallback stream.
          void  adjustVolume(int direction, int flags)
Adjusts the volume of the most relevant stream.
          int  getMode()
Returns the current audio mode.
          int  getRingerMode()
Returns the current ringtone mode.
          int  getRouting(int mode)
Returns the current audio routing bit vector for a specified mode.
          int  getStreamMaxVolume(int streamType)
Returns the maximum volume index for a particular stream.
          int  getStreamVolume(int streamType)
Returns the current volume index for a particular stream.
          int  getVibrateSetting(int vibrateType)
Returns whether the user's vibrate setting for a vibrate type.
          boolean  isBluetoothScoOn()
Checks whether audio routing to the Bluetooth headset is on or off.
          boolean  isMicrophoneMute()
Checks whether the microphone mute is on or off.
          boolean  isMusicActive()
Checks whether any music is active.
          boolean  isSpeakerphoneOn()
Checks whether the speakerphone is on or off.
          void  playSoundEffect(Context context, int effectType)
Plays a sound effect (Key clicks, lid open/close...)
          void  setBluetoothScoOn(boolean on)
Sets audio routing to the Bluetooth headset on or off.
          void  setMicrophoneMute(boolean on)
Sets the microphone mute on or off.
          void  setMode(int mode)
Sets the audio mode.
          void  setRingerMode(int ringerMode)
Sets the ringer mode.
          void  setRouting(int mode, int routes, int mask)
Sets the audio routing for a specified mode
          void  setSpeakerphoneOn(boolean on)
Sets the speakerphone on or off.
          void  setStreamMute(int streamType, boolean state)
Mute or unmute an audio stream.
          void  setStreamSolo(int streamType, boolean state)
Solo or unsolo a particular stream.
          void  setStreamVolume(int streamType, int index, int flags)
Sets the volume index for a particular stream.
          void  setVibrateSetting(int vibrateType, int vibrateSetting)
Sets the setting for when the vibrate type should vibrate.
          boolean  shouldVibrate(int vibrateType)
Returns whether a particular type should vibrate according to user settings and the current ringer mode.
Methods inherited from class java.lang.Object

Details

Constants

public static final int ADJUST_LOWER

Decrease the ringer volume.
Constant Value: -1 (0xffffffff)

public static final int ADJUST_RAISE

Increase the ringer volume.
Constant Value: 1 (0x00000001)

public static final int ADJUST_SAME

Maintain the previous ringer volume. This may be useful when needing to show the volume toast without actually modifying the volume.
Constant Value: 0 (0x00000000)

public static final String EXTRA_RINGER_MODE

The new ringer mode.
Constant Value: "android.media.EXTRA_RINGER_MODE"

public static final String EXTRA_VIBRATE_SETTING

The new vibrate setting for a particular type.
Constant Value: "android.media.EXTRA_VIBRATE_SETTING"

public static final String EXTRA_VIBRATE_TYPE

The vibrate type whose setting has changed.
Constant Value: "android.media.EXTRA_VIBRATE_TYPE"

public static final int FLAG_ALLOW_RINGER_MODES

Whether to include ringer modes as possible options when changing volume. For example, if true and volume level is 0 and the volume is adjusted with ADJUST_LOWER, then the ringer mode may switch the silent or vibrate mode.

By default this is on for stream types that are affected by the ringer mode (for example, the ring stream type). If this flag is included, this behavior will be present regardless of the stream type being affected by the ringer mode.

Constant Value: 2 (0x00000002)

public static final int FLAG_PLAY_SOUND

Whether to play a sound when changing the volume.

If this is given to adjustVolume(int, int) or adjustSuggestedStreamVolume(int, int, int), it may be ignored in some cases (for example, the decided stream type is not STREAM_RING, or the volume is being adjusted downward).

Constant Value: 4 (0x00000004)

public static final int FLAG_REMOVE_SOUND_AND_VIBRATE

Removes any sounds/vibrate that may be in the queue, or are playing (related to changing volume).
Constant Value: 8 (0x00000008)

public static final int FLAG_SHOW_UI

Show a toast containing the current volume.
Constant Value: 1 (0x00000001)

public static final int FLAG_VIBRATE

Whether to vibrate if going into the vibrate ringer mode.
Constant Value: 16 (0x00000010)

public static final int FX_FLING_END_BOTTOM

We have hit bottom of list while flinging
Constant Value: 8 (0x00000008)

public static final int FX_FLING_END_TOP

We have hit top of list while flinging
Constant Value: 7 (0x00000007)

public static final int FX_FOCUS_NAVIGATION_DOWN

Focuse has moved down
Constant Value: 2 (0x00000002)

public static final int FX_FOCUS_NAVIGATION_LEFT

Focuse has moved left
Constant Value: 3 (0x00000003)

public static final int FX_FOCUS_NAVIGATION_RIGHT

Focuse has moved right
Constant Value: 4 (0x00000004)

public static final int FX_FOCUS_NAVIGATION_UP

Focuse has moved up
Constant Value: 1 (0x00000001)

public static final int FX_KEY_CLICK

Keyboard and direction pad click sound
Constant Value: 0 (0x00000000)

public static final int FX_LID_CLOSE

Lid close sound
Constant Value: 6 (0x00000006)

public static final int FX_LID_OPEN

Lid open sound
Constant Value: 5 (0x00000005)

public static final int MODE_CURRENT

Current audio mode. Used to apply audio routing to current mode.
Constant Value: -1 (0xffffffff)

public static final int MODE_INVALID

Invalid audio mode.
Constant Value: -2 (0xfffffffe)

public static final int MODE_IN_CALL

In call audio mode. A call is established.
Constant Value: 2 (0x00000002)

public static final int MODE_NORMAL

Normal audio mode: not ringing and no call established.
Constant Value: 0 (0x00000000)

public static final int MODE_RINGTONE

Ringing audio mode. An incoming is being signaled.
Constant Value: 1 (0x00000001)

public static final int NUM_STREAMS

Number of audio streams
Constant Value: 5 (0x00000005)

public static final String RINGER_MODE_CHANGED_ACTION

Sticky broadcast intent action indicating that the ringer mode has changed. Includes the new ringer mode.
Constant Value: "android.media.RINGER_MODE_CHANGED"

public static final int RINGER_MODE_NORMAL

Ringer mode that may be audible and may vibrate. It will be audible if the volume before changing out of this mode was audible. It will vibrate if the vibrate setting is on.
Constant Value: 2 (0x00000002)

public static final int RINGER_MODE_SILENT

Ringer mode that will be silent and will not vibrate. (This overrides the vibrate setting.)
Constant Value: 0 (0x00000000)

public static final int RINGER_MODE_VIBRATE

Ringer mode that will be silent and will vibrate. (This will cause the phone ringer to always vibrate, but the notification vibrate to only vibrate if set.)
Constant Value: 1 (0x00000001)

public static final int ROUTE_ALL

Used for mask parameter of setRouting(int, int, int).
Constant Value: 15 (0x0000000f)

public static final int ROUTE_BLUETOOTH

Routing audio output to bluetooth
Constant Value: 4 (0x00000004)

public static final int ROUTE_EARPIECE

Routing audio output to earpiece
Constant Value: 1 (0x00000001)

public static final int ROUTE_HEADSET

Routing audio output to headset
Constant Value: 8 (0x00000008)

public static final int ROUTE_SPEAKER

Routing audio output to spaker
Constant Value: 2 (0x00000002)

public static final int STREAM_ALARM

The audio stream for alarms
Constant Value: 4 (0x00000004)

public static final int STREAM_MUSIC

The audio stream for music playback
Constant Value: 3 (0x00000003)

public static final int STREAM_RING

The audio stream for the phone ring and message alerts
Constant Value: 2 (0x00000002)

public static final int STREAM_SYSTEM

The audio stream for system sounds
Constant Value: 1 (0x00000001)

public static final int STREAM_VOICE_CALL

The audio stream for phone calls
Constant Value: 0 (0x00000000)

public static final int USE_DEFAULT_STREAM_TYPE

Suggests using the default stream type. This may not be used in all places a stream type is needed.
Constant Value: -2147483648 (0x80000000)

public static final String VIBRATE_SETTING_CHANGED_ACTION

Broadcast intent action indicating that the vibrate setting has changed. Includes the vibrate type and its new setting.
Constant Value: "android.media.VIBRATE_SETTING_CHANGED"

public static final int VIBRATE_SETTING_OFF

Vibrate setting that suggests to never vibrate.
Constant Value: 0 (0x00000000)

public static final int VIBRATE_SETTING_ON

Vibrate setting that suggests to vibrate when possible.
Constant Value: 1 (0x00000001)

public static final int VIBRATE_SETTING_ONLY_SILENT

Vibrate setting that suggests to only vibrate when in the vibrate ringer mode.
Constant Value: 2 (0x00000002)

public static final int VIBRATE_TYPE_NOTIFICATION

Vibrate type that corresponds to notifications.
Constant Value: 1 (0x00000001)

public static final int VIBRATE_TYPE_RINGER

Vibrate type that corresponds to the ringer.
Constant Value: 0 (0x00000000)

Public Methods

public void adjustStreamVolume(int streamType, int direction, int flags)

Adjusts the volume of a particular stream by one step in a direction.

Parameters

streamType The stream type to adjust. One of STREAM_VOICE_CALL, STREAM_SYSTEM, STREAM_RING, STREAM_MUSIC or STREAM_ALARM
direction The direction to adjust the volume. One of ADJUST_LOWER, ADJUST_RAISE, or ADJUST_SAME.
flags One or more flags.

public void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags)

Adjusts the volume of the most relevant stream, or the given fallback stream.

Parameters

direction The direction to adjust the volume. One of ADJUST_LOWER, ADJUST_RAISE, or ADJUST_SAME.
suggestedStreamType The stream type that will be used if there isn't a relevant stream. USE_DEFAULT_STREAM_TYPE is valid here.
flags One or more flags.

public void adjustVolume(int direction, int flags)

Adjusts the volume of the most relevant stream. For example, if a call is active, it will have the highest priority regardless of if the in-call screen is showing. Another example, if music is playing in the background and a call is not active, the music stream will be adjusted.

Parameters

direction The direction to adjust the volume. One of ADJUST_LOWER, ADJUST_RAISE, or ADJUST_SAME.
flags One or more flags.

public int getMode()

Returns the current audio mode.

Returns

  • the current audio mode (NORMAL, RINGTONE, or IN_CALL). Returns the current current audio state from the HAL.

public int getRingerMode()

Returns the current ringtone mode.

Returns

public int getRouting(int mode)

Returns the current audio routing bit vector for a specified mode.

Parameters

mode audio mode to get route (e.g., MODE_RINGTONE)

Returns

  • an audio route bit vector that can be compared with ROUTE_xxx bits

public int getStreamMaxVolume(int streamType)

Returns the maximum volume index for a particular stream.

Parameters

streamType The stream type whose maximum volume index is returned.

Returns

  • The maximum valid volume index for the stream.

public int getStreamVolume(int streamType)

Returns the current volume index for a particular stream.

Parameters

streamType The stream type whose volume index is returned.

Returns

  • The current volume index for the stream.

public int getVibrateSetting(int vibrateType)

Returns whether the user's vibrate setting for a vibrate type.

This shouldn't be needed by most clients that want to vibrate, instead see shouldVibrate(int).

Parameters

vibrateType The type that is being checked.

Returns

public boolean isBluetoothScoOn()

Checks whether audio routing to the Bluetooth headset is on or off.

Returns

  • true if SCO audio is being routed to/from Bluetooth headset; false if otherwise

public boolean isMicrophoneMute()

Checks whether the microphone mute is on or off.

Returns

  • true if microphone is muted, false if it's not

public boolean isMusicActive()

Checks whether any music is active.

Returns

  • true if any music tracks are active.

public boolean isSpeakerphoneOn()

Checks whether the speakerphone is on or off.

Returns

  • true if speakerphone is on, false if it's off

public void playSoundEffect(Context context, int effectType)

Plays a sound effect (Key clicks, lid open/close...)

public void setBluetoothScoOn(boolean on)

Sets audio routing to the Bluetooth headset on or off.

Parameters

on set true to route SCO (voice) audio to/from Bluetooth headset; false to route audio to/from phone earpiece

public void setMicrophoneMute(boolean on)

Sets the microphone mute on or off.

Parameters

on set true to mute the microphone; false to turn mute off

public void setMode(int mode)

Sets the audio mode.

Parameters

mode the requested audio mode (NORMAL, RINGTONE, or IN_CALL). Informs the HAL about the current audio state so that it can route the audio appropriately.

public void setRingerMode(int ringerMode)

Sets the ringer mode.

Silent mode will mute the volume and will not vibrate. Vibrate mode will mute the volume and vibrate. Normal mode will be audible and may vibrate according to user settings.

Parameters

ringerMode The ringer mode, one of RINGER_MODE_NORMAL, RINGER_MODE_SILENT, or RINGER_MODE_VIBRATE.

See Also

public void setRouting(int mode, int routes, int mask)

Sets the audio routing for a specified mode

Parameters

mode audio mode to change route. E.g., MODE_RINGTONE.
routes bit vector of routes requested, created from one or more of ROUTE_xxx types. Set bits indicate that route should be on
mask bit vector of routes to change, created from one or more of ROUTE_xxx types. Unset bits indicate the route should be left unchanged

public void setSpeakerphoneOn(boolean on)

Sets the speakerphone on or off.

Parameters

on set true to turn on speakerphone; false to turn it off

public void setStreamMute(int streamType, boolean state)

Mute or unmute an audio stream.

The mute command is protected against client process death: if a process with an active mute request on a stream dies, this stream will be unmuted automatically.

The mute requests for a given stream are cumulative: the AudioManager can receive several mute requests from one or more clients and the stream will be unmuted only when the same number of unmute requests are received.

For a better user experience, applications MUST unmute a muted stream in onPause() and mute is again in onResume() if appropriate.

Parameters

streamType The stream to be muted/unmuted.
state The required mute state: true for mute ON, false for mute OFF

public void setStreamSolo(int streamType, boolean state)

Solo or unsolo a particular stream. All other streams are muted.

The solo command is protected against client process death: if a process with an active solo request on a stream dies, all streams that were muted because of this request will be unmuted automatically.

The solo requests for a given stream are cumulative: the AudioManager can receive several solo requests from one or more clients and the stream will be unsoloed only when the same number of unsolo requests are received.

For a better user experience, applications MUST unsolo a soloed stream in onPause() and solo is again in onResume() if appropriate.

Parameters

streamType The stream to be soloed/unsoloed.
state The required solo state: true for solo ON, false for solo OFF

public void setStreamVolume(int streamType, int index, int flags)

Sets the volume index for a particular stream.

Parameters

streamType The stream whose volume index should be set.
index The volume index to set. See getStreamMaxVolume(int) for the largest valid value.
flags One or more flags.

public void setVibrateSetting(int vibrateType, int vibrateSetting)

Sets the setting for when the vibrate type should vibrate.

Parameters

vibrateType The type of vibrate. One of VIBRATE_TYPE_NOTIFICATION or VIBRATE_TYPE_RINGER.
vibrateSetting The vibrate setting, one of VIBRATE_SETTING_ON, VIBRATE_SETTING_OFF, or VIBRATE_SETTING_ONLY_SILENT.

public boolean shouldVibrate(int vibrateType)

Returns whether a particular type should vibrate according to user settings and the current ringer mode.

This shouldn't be needed by most clients that use notifications to vibrate. The notification manager will not vibrate if the policy doesn't allow it, so the client should always set a vibrate pattern and let the notification manager control whether or not to actually vibrate.

Parameters

vibrateType The type that is being checked for vibrate on/off. One of VIBRATE_SETTING_ON, VIBRATE_SETTING_OFF, or VIBRATE_SETTING_ONLY_SILENT.

Returns

  • Whether the type should vibrate at the instant this method is called.
Copyright 2007 Google Inc. Build 0.9_r1-98467 - 14 Aug 2008 18:56