For Android volume adjustment, the volume can be divided into key adjust volume and settings. Let's start by talking about the volume adjustment in the settings.
First, the volume of the classification:
1.audiomanager.stream_voice_call
2.audiomanager.stream_ring
3.audiomanager.stream_music,
4.audiomanager.stream_alarm
5.audiomanager.stream_notification
Second, the volume range:
For different types of volume Android specifies a different range, in Audioservice there is an array that defines the range of different volume levels.
Private final int[] Max_stream_volume = new int[] {
5,//Stream_voice_call
7,//Stream_system
7,//stream_ring
,//Stream_music
7,//Stream_alarm
7,//Stream_notification
,//Stream_bluetooth_sco
7,//stream_system_enforced
,//STREAM_DTMF
//Stream_tts
};
Three: How to adjust the volume:
int streamvalue = Am.getstreamvolume (Streamtype); Gets the current type of volume value
Audiomanager Audiomanager = (audiomanager) getsystemservice (Context.audio_service); Get Audio Service
Audiomanager.setstreamvolume (Mstreamtype, Streamvalue, 0); Set volume
Now, let's talk. Use the keys to adjust the volume
Use the key to adjust the volume, first in the Phonewinmanager receive a key event, and then call Audioservice in the Adjuststreamvolume method, the source code is as follows:
/**
* Tell the Audio service to adjust the volume appropriate to the event.
* @param keycode
*/
void ha Ndlevolumekey (int stream, int keycode) {
Iaudioservice audioservice = Getaudioservice ();
if (Audioservice = = null) {
return;
}
Try {
//Since audio is playing, we shouldn ' t has a to-hold a wake lock
//during the "call", but we do it As a precaution for the rare possibility
//So the music stops right before we ' this
//todo:actually Hand Le MUTE.
Mbroadcastwakelock.acquire ();
Audioservice.adjuststreamvolume (stream,
KeyCode = = Keyevent.keycode_volume_up
? Audiomanager.adjust_raise
: Audiomanager.adjust_lower,
0),
} catch (RemoteException e) {
LOG.W (TAG, " Iaudioservice.adjuststreamvolume () threw remoteexception "+ e);
} finally {
Mbroadcastwakelock.release ();
}
}
The Adjuststreamvolume will start the Volumepanel, which is the interface we press the volume key to appear. The Audiomanager Setstreamvolume is called in Volumepanel to set the volume.
Android Volume adjustment