The audiomanager class is located in the Android. Media package, which provides operations to control the volume and audible mode.
Note: UseContext.getSystemService(Context.AUDIO_SERVICE)
To get an instance of this class.
Therefore, we can use the following statement:
AudioManager audiomanage = (AudioManager)getAcitivity().getSystemService(Context.AUDIO_SERVICE);
Audiomanager is the object we define to control system sound.
Here are only a few common methods:
Adjustvolume (INT direction, int flags)
Used to control the volume of the mobile phone,
When the first parameter is audiomanager. adjust_lower, you can reduce the volume by one unit,
When you pass in audiomanager. adjust_raise, you can increase the volume by one unit.
Adjuststreamvolume (INT streamtype, int direction, intflags)
Adjust the volume of the mobile phone in step size
Parameter 1: Sound type, which can be stream_voice_call, stream_system, stream_ring, stream_music, and stream_alarm)
Parameter 2: adjust the volume direction. You can select adjust_lower (lower), adjust_raise (Increase), adjust_same
Parameter 3: Optional flag
Setstreamvolume (INT streamtype, int index, intflags)
Set the volume directly
Getmode ()
Returns the current audio mode, such as normal (normal), ringtone (ringtone), or orin_call (CALL)
Setmode ()
Set the sound mode. Valid values: normal (normal), ringtone (ringtone), or in_call (CALL)
Getringermode ()
Returns the current ringtone mode. Such as ringer_mode_normal (normal), ringer_mode_silent (mute), ringer_mode_vibrate (vibrate)
Setringermode (INT ringermode)
Change ringtone Mode
Getstreamvolume (INT streamtype)
Obtain the volume of the current mobile phone. The maximum value is 7 and the minimum value is 0. When the value is 0, the mobile phone automatically adjusts the mode to "vibrate ".
Getstreammaxvolume (INT streamtype)
Get the maximum ringtone of the current phone.
Setstreammute (INT streamtype, Boolean state)
Mute or non-mute audio streams
Sets whether the specified streamtype is set to mute. If the State is true, it is set to mute; otherwise, it is not set to mute.
// Volume control, initialization definition audiomanager maudiomanager = (audiomanager) getsystemservice (context. audio_service); // max volume int maxvolume = maudiomanager. getstreammaxvolume (audiomanager. stream_music); // The current volume int currentvolume = maudiomanager. getstreamvolume (audiomanager. stream_music );
Directly control the volume:
If (issilent) {maudiomanager. setstreamvolume (audiomanager. stream_music, 0, 0);} else {maudiomanager. setstreamvolume (audiomanager. stream_music, tempvolume, 0); // tempvolume: absolute volume value}
Use one step to increase or decrease the volume. The default volume control bar is displayed:
// Reduce the volume and call out the system volume control if (flag = 0) {maudiomanager. adjuststreamvolume (audiomanager. stream_music, audiomanager. adjust_lower, audiomanager. fx_focus_navigation_up);} // increase the volume. Call the system volume control else if (flag = 1) {maudiomanager. adjuststreamvolume (audiomanager. stream_music, audiomanager. adjust_raise, audiomanager. fx_focus_navigation_up );}
Common Methods:
Android audiomanager gets the volume:
Obtain the audiomanager instance first,
Audiomanager maudiomanager = (audiomanager) getsystemservice (context.Audio_ Service );
// Call volume int max = maudiomanager. getstreammaxvolume (audiomanager. stream_voice_call); int current = maudiomanager. getstreamvolume (audiomanager. stream_voice_call); // system volume int max = maudiomanager. getstreammaxvolume (audiomanager. stream_system); current = maudiomanager. getstreamvolume (audiomanager. stream_system); // ringtone volume max = maudiomanager. getstreammaxvolume (audiomanager. stream_ring); current = maudiomanager. getstreamvolume (audiomanager. stream_ring); // Music volume max = maudiomanager. getstreammaxvolume (audiomanager. stream_music); current = maudiomanager. getstreamvolume (audiomanager. stream_music); // displays the audio volume. max = maudiomanager. getstreammaxvolume (audiomanager. stream_alarm); current = maudiomanager. getstreamvolume (audiomanager. stream_alarm );