In the previous article mentioned the sound adjustment of the two operations, the next step is to specifically analyze the volume adjustment of the approximate steps, respectively involving two parts:
Android\frameworks\base\media\java\android\media\audioservice.java
Android\frameworks\base\media\java\android\media\iaudioservice.aidl
Where the declaration of the Adjuststreamvolume (int streamtype, int direction, int flags) function is in audioservice.aidl, and the function is implemented in Audioservice.java.
The following specific can see the source:
/** @see audiomanager#adjuststreamvolume (int, int, int) */public void Adjuststreamvolume (int streamtype, int direc tion, int flags) {if (Debug_vol) log.d (TAG, "Adjuststreamvolume () stream=" +streamtype+ ", dir=" +direction "); Ensurevaliddirection (direction); Ensurevalidstreamtype (Streamtype); Use stream type alias here so this streams with same alias has the same behavior,//including with regard to s Ilent mode control (e.g the use of stream_ring below, and in//Checkforringermodechange () in place of Stream_ring O R stream_notification) int streamtypealias = Mstreamvolumealias[streamtype]; Volumestreamstate streamstate = Mstreamstates[streamtypealias]; Final int device = Getdeviceforstream (Streamtypealias); Get last audible index if stream was muted, current index otherwise final int aliasindex = Streamstate.getindex (d Evice, (Streamstate.mutecount ()! = 0)/* lastaudible */); Boolean adjustvolume = true; Convert one UI step (+/-1) into a number of internal units on the stream alias int step = Rescaleindex (Strea Mtype, Streamtypealias); If either the client forces allowing ringer modes for this adjustment,//or the stream type was one that was Affe CTED by Ringer Modes if (((Flags & audiomanager.flag_allow_ringer_modes)! = 0) | | (Streamtypealias = = Getmasterstreamtype ())) {int ringermode = Getringermode (); Vibrate if already in vibrate mode if (Ringermode = = audiomanager.ringer_mode_vibrate) { Flags &= ~audiomanager.flag_vibrate; }//Check If the ringer mode changes with this volume adjustment. IF//It does, it'll handle adjusting the volume, so we won ' t below Adjustvolume = Checkforringerm Odechange (Aliasindex, direction, step); if ((Streamtypealias = = Getmasterstreamtype ()) && (Mringermode = = audiomanager.ringer_mode_silent)) { Streamstate.setlastaudibleindex (0, device); }}//If stream is muted, adjust last audible index only int index; Final int oldindex = Mstreamstates[streamtype].getindex (device, (Mstreamstates[streamtype].mutecount ()! = 0 )/* lastaudible */); if (streamstate.mutecount () = 0) {if (Adjustvolume) {//Post a Persist volume msg No need to persist volume on all streams sharing the same alias Streamstate.adjustlastaudibleindex (d Irection * step, device); Sendmsg (Maudiohandler, Msg_persist_volume, Sendmsg_queue, Persist_last_audible, device, Streamstate, Persis T_delay); } index = mStreamstates[streamtype].getindex (device, TRUE/* lastaudible */); } else {if (Adjustvolume && streamstate.adjustindex (Direction * step, device)) {//Pos T message to set system volume (it in turn would post a message//to persist). Don't change volume if stream is muted. Sendmsg (Maudiohandler, Msg_set_device_volume, Sendmsg_queue, Device, 0, streamstate, 0); index = Mstreamstates[streamtype].getindex (device, FALSE/* lastaudible */); } sendvolumeupdate (Streamtype, Oldindex, index, flags); }Parsing the above function shows that at the beginning of the function there is a test for the validity of the first two parameters, and the specific detection code is posted:
private void ensurevaliddirection (int direction) { if (Direction < Audiomanager.adjust_lower | | direction > AUD Iomanager.adjust_raise) { throw new illegalargumentexception ("bad direction" + direction); } } private void Ensurevalidsteps (int steps) { if (Math.Abs (steps) > Max_batch_volume_adjust_steps) { throw new IllegalArgumentException ("Bad volume Adjust steps" + steps); } } private void Ensurevalidstreamtype (int streamtype) { if (Streamtype < 0 | | Streamtype >= mstreamstates.length) { throw new illegalargumentexception ("bad stream type" + Streamtype); } }
Continue to follow the source code, you can see
int step = Rescaleindex (Ten, Streamtype, Streamtypealias);
This parameter is for adjusting the step value
The following judgment condition if (streamstate.mutecount ()! = 0) Determines whether the volume adjustment is initiated from the mute state for different operations.
At the end of the function
Sendvolumeupdate (Streamtype, Oldindex, index, flags);
This function is used to update the display of the system volume bar
//UI update and broadcast Intent private void sendvolumeupdate (int streamtype, int oldindex, int index, int flags) {if (!MV Oicecapable && (Streamtype = = audiosystem.stream_ring)) {streamtype = audiosystem.stream_notification; } mvolumepanel.postvolumechanged (Streamtype, flags); Oldindex = (Oldindex + 5)/10; Index = (index + 5)/10; Intent Intent = new Intent (audiomanager.volume_changed_action); if (Streamtype = = audiomanager.stream_voice_call) Streamtype = Audiomanager.stream_music; Intent.putextra (Audiomanager.extra_volume_stream_type, Streamtype); Intent.putextra (audiomanager.extra_volume_stream_value, index); Intent.putextra (Audiomanager.extra_prev_volume_stream_value, Oldindex); Mcontext.sendbroadcast (Intent); }
You can see that the source code calls the
\android\frameworks\base\core\java\android\view\volumepanel.java
Where Volumepanel.java is used to display the display of the system volume bar, where the specific display can refer to the code. At the same time, this part of the code will be related to the system all the volume of the icon display, need to make changes to beautify and other operations can be specifically found resource files to modify.
The specific path can be
Android\frameworks\base\core\res\res
Another way to set the volume of the method setstreamvolume (int streamtype, int index, int flags) is also the same can be found in the above code, about the volume adjustment part of the resolution to this basic end, temporarily can understand the above process.
An analysis of the Android Framework Audio Management Audiomanager (cont.)