//设置stream音量
mRingVolumePlayer.setStreamVolume(index);
在这里,streamVolume就是我们要设置的音量大小,接着就是调用AudioManager的方法进行设置。
3.AudioManager.java
publicvoidsetStreamVolume(intstreamType,intindex,intflags){
//IAudioService是AudioService的本地代理
IAudioServiceservice=getService();
try{
service.setStreamVolume(streamType,index,flags);
}catch(RemoteExceptione){
Log.e(TAG,"DeadobjectinsetStreamVolume",e);
}
}
这就开始调用AudioService的相关方法设置音量:streamType假设为STREAM_RING,
flags为FLAG_REMOVE_SOUND_AND_VIBRATE
4.AudioService.java
publicvoidsetStreamVolume(intstreamType,intindex,intflags){
//获取stream的状态,在AudioService的构造函数中createStreamStates()时创建
VolumeStreamStatestreamState=mStreamStates[STREAM_VOLUME_ALIAS[streamType]];
//原来stream的音量
finalintoldIndex=(streamState.muteCount()!=0)?streamState.mLastAudibleIndex: streamState.mIndex;
……
//获取stream的音量和最大音量一半的平均值
index=rescaleIndex(index*10,streamType,STREAM_VOLUME_ALIAS[streamType]);
setStreamVolumeInt(STREAM_VOLUME_ALIAS[streamType],index,false,true);
}
privatevoidsetStreamVolumeInt(intstreamType,intindex,booleanforce,boolean lastAudible)
{
VolumeStreamStatestreamState=mStreamStates[streamType];
//假设没有被静音
if(streamState.setIndex(index,lastAudible)||force){
//发送baseid为MSG_SET_SYSTEM_VOLUME的msg
sendMsg(mAudioHandler,MSG_SET_SYSTEM_VOLUME,streamType,SENDMSG_NOOP,0,0,
streamState,0);
}
}
//SetIndex实际上就是判断当前的Index和要设置的index是否一致,不同才进行变更
publicbooleansetIndex(intindex,booleanlastAudible){
intoldIndex=mIndex;
mIndex=getValidIndex(index);
if(oldIndex!=mIndex){
if(lastAudible){
mLastAudibleIndex=mIndex;
}
//Applychangetoallstreamsusingthisoneasalias
intnumStreamTypes=AudioSystem.getNumStreamTypes();
for(intstreamType=numStreamTypes-1;streamType>=0;streamType--){
if(streamType!=mStreamType
&&STREAM_VOLUME_ALIAS[streamType]==mStreamType){
mStreamStates[streamType].setIndex(rescaleIndex(mIndex,mStreamType, streamType),lastAudible);
}
}
returntrue;
}else{
returnfalse;
}
}
在处理刚才发送那个Message时:
AudioHandler{
publicvoidhandleMessage(Messagemsg){
//一般对于内置的stream都是返回0
intbaseMsgWhat=getMsgBase(msg.what);
switch(baseMsgWhat){
caseMSG_SET_SYSTEM_VOLUME:
setSystemVolume((VolumeStreamState)msg.obj);
break;
}
}
privatevoidsetSystemVolume(VolumeStreamStatestreamState){
//这一步调用AudioSystem的方法,再通过JNI对底层直接操作
setStreamVolumeIndex(streamState.mStreamType,streamState.mIndex);
//Applychangetoallstreamsusingthisoneasalias
intnumStreamTypes=AudioSystem.getNumStreamTypes();
for(intstreamType=numStreamTypes-1;streamType>=0;streamType--){
if(streamType!=streamState.mStreamType&&
STREAM_VOLUME_ALIAS[streamType]==streamState.mStreamType){
setStreamVolumeIndex(streamType,mStreamStates[streamType].mIndex);
}
}
//Postapersistvolumemsg
sendMsg(mAudioHandler,MSG_PERSIST_VOLUME,streamState.mStreamType,
SENDMSG_REPLACE,1,1,streamState,PERSIST_DELAY);
}
重点看:setStreamVolumeIndex()
AudioSystem下
publicstaticnativeintsetStreamVolumeIndex(intstream,intindex);
它的实现在:
5./frameworks/base/media/libmedia/android_media_AudioSystem.cpp
staticint
android_media_AudioSystem_setStreamVolumeIndex(JNIEnv*env,jobjectthiz,jintstream, jintindex)
{
returncheck_AudioSystem_Command(AudioSystem::setStreamVolumeIndex(static_cast <audio_stream_type_t>(stream),index));
}
6.AudioSystem.cpp
//设置stream的音量
status_tAudioSystem::setStreamVolumeIndex(audio_stream_type_tstream,intindex)
{
status_tAudioSystem::setStreamVolumeIndex(audio_stream_type_tstream,intindex)
{
//获取AudioSystem中的AudioPolicyService的本地代理
constsp<IAudioPolicyService>&aps=AudioSystem::get_audio_policy_service();
if(aps==0)returnPERMISSION_DENIED;
returnaps->setStreamVolumeIndex(stream,index);
}
}
constsp<IAudioPolicyService>&AudioSystem::get_audio_policy_service()
{
gLock.lock();
if(gAudioPolicyService.get()==0){
//获取ServiceManager
sp<IServiceManager>sm=defaultServiceManager();
sp<IBinder>binder;
do{
//获取audiopolicyservice
binder=sm->getService(String16("media.audio_policy"));
if(binder!=0)
break;
//0.5s
usleep(500000);
}while(true);
if(gAudioPolicyServiceClient==NULL){
gAudioPolicyServiceClient=newAudioPolicyServiceClient();
}
binder->linkToDeath(gAudioPolicyServiceClient);
//这一步是获取获取AudioPolicyService的BpBinder形式,然后将它赋给gAudioPolicyService
//的mRemote
gAudioPolicyService=interface_cast<IAudioPolicyService>(binder);
gLock.unlock();
}else{
gLock.unlock();
}
returngAudioPolicyService;
}
AudioPolicyService.cpp
status_tAudioPolicyService::setStreamVolumeIndex(audio_stream_type_tstream,intindex)
{
//这是一个指针:structaudio_policy*mpAudioPolicy(AudioPolicyService.h)
if(mpAudioPolicy==NULL){
returnNO_INIT;
}
if(!checkPermission()){
returnPERMISSION_DENIED;
}
if(stream<0||stream>=AUDIO_STREAM_CNT){
returnBAD_VALUE;
}
returnmpAudioPolicy->set_stream_volume_index(mpAudioPolicy,stream,index);
}
在这里正式转入对驱动的操作。