(只简单描述调用过程,需对照代码看,不画图了,也不贴代码)
1.在key Event处理部分,Phonewindow会捕获到音量按键事件,
Phonewindow.onkeydown-- MediaSessionLegacyHelper.getHelper(getContext()).sendAdjustVolumeBy
2.调节音量的时候要处理3件事情,界面显示、播放提示音、调节音量,
MediaSessionManager. dispatchAdjustVolume--mService.dispatchAdjustVolume/ binder-- MediaSessionService. dispatchAdjustVolume—dispatchAdjustVolumeLocked--mAudioService.adjustSuggestedStreamVolume
(如使用AudioManager,则AudioManager 的handleKeyDown、handleKeyUp也可来设置音量。
handleKeyDown--adjustSuggestedStreamVolume--MediaSessionLegacyHelper.sendAdjustVolumeBy—
)
3.在Audio模块,
mAudioService.adjustSuggestedStreamVolume(direction,suggestedStream,
flags, packageName, TAG);/binder – AudioService.adjustSuggestedStreamVolume—adjustStreamVolume/这里做了些场景处理和判断,将APS的工作提前做了,之后,
a)调音量,走 --sendMsg(mAudioHandler,
MSG_SET_DEVICE_VOLUME,
SENDMSG_QUEUE,
device,
0,
streamState,
0);-- setDeviceVolume--mStreamStates[streamType].applyDeviceVolume_syncVSS(streamDevice)-- AudioSystem.setStreamVolumeIndex/jni -- AudioSystem.setStreamVolumeIndex-- aps->setStreamVolumeIndex(stream, index, device)/ binder-- AudioPolicyInterfaceImpl.cppsetStreamVolumeIndex-- mAudioPolicyManager->setStreamVolumeIndex—checkAndSetVolume—
调一般的音量调用outputDesc->setVolume,待后面打开output
调节音量使用,调正在播放的铃声音量不在这个地方。
调通话中音量走mpClientInterface->setVoiceVolume-- AudioPolicyService::AudioPolicyClient::setVoiceVolume-- mAudioPolicyService->setVoiceVolume -- AudioPolicyService::setVoiceVolume -- mAudioCommandThread->voiceVolumeCommand/SET_VOICE_VOLUME -- AudioSystem::setVoiceVolume -- af->setVoiceVolume -- dev->set_voice_volume--audio_hw.c adev_set_voice_volume-- voice_set_volume-- …
这个调音量的流程也是够够的了,从jni下来后,AS—APS—APM—APS—AS—AF—HAL这样走了一大圈。
b)调界面,走 —sendVolumeUpdate-- mVolumeController.postVolumeChanged(streamType,flags);-- mController.volumeChanged/ binder--
VolumeDialogController. volumeChanged-- mWorker.obtainMessage(W.VOLUME_CHANGED, streamType,flags).sendToTarget();--onVolumeChangedW--…
4.对于通过outPut设置音量的部分,如果是deepbuffer播放就不会调用下来,如果是offload播放,在audio_hw.c里面
static intout_set_volume(struct audio_stream_out *stream, float left,
float right)
out->stream.set_volume = out_set_volume;
在audioflinger里面就调整了
Threads.cpp(frameworks\av\services\audioflinger): if (mOutput->stream->set_volume) {
Threads.cpp (frameworks\av\services\audioflinger): mOutput->stream->set_volume(mOutput->stream, left, right);
5.在audio_hw.c里面,流程如下,
out_set_volume--passthru.caudio_extn_passthru_set_volume--platform.c platform_set_device_params