Android AudioManager控制系统声音的流程

首先上层java调用 java

XXXPlayer android

AudioManager audiomanage = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); api

audiomanager就是咱们定义的控制系统声音的对象,(若是context报错,可将其改为XXXPlayer.this) app

audiomanager.SetStreamVolume(AA,BB,CC),是咱们能够直接使用的AudioManager的成员函数,3个 参数表示的意思:AA:有内置的常量,能够在AudioManager里面查到相关的定义,咱们在此用 AudioManager.STREAM_MUSIC, BB:本身设置音量的值,CC:也是一些标示量,我在此设置为0; 函数

 

1.AudioManager.java post

public void setStreamVolume(int streamType, int index, int flags);上层接口 ui

       1)调用IAudioService service = getService(); 当程序开启时会得到service,调用此来得到 this

2.执行ServiceManager.java 
public static IBinder getService(String name)获取audio服务 spa

3.AudioService.java 
public void setStreamVolume(int streamType, int index, int flags)//服务接口
   1) private void setStreamVolumeInt(int streamType, int index, boolean force, boolean lastAudible)//服务函数
   2)调用如下函数  
sendMsg(mAudioHandler, MSG_SET_SYSTEM_VOLUME, streamType, SENDMSG_NOOP, 0, 0,streamState, 0) 
       //Post message to set system volume (it in turn will post a message
                  // to persist)
3)AudioHandler::setSystemVolume(VolumeStreamState streamState);//sendmsg(...)后执行函数
   4)调用AudioHandler::setStreamVolumeIndex(int stream, int index)
    5)AudioSystem.setStreamVolumeIndex(stream,index);//audioSystem接口 对象

static int android_media_AudioSystem_setStreamVolumeIndex(JNIEnv *env, jobject thiz, jint stream, jint index)
1)调用AudioSystem::setStreamVolumeIndex

6.status_t AudioSystem::setStreamVolumeIndex(stream_type stream, int index)(处理到这时,也能够直接走AudioFlinger路线,不通过策略) 
1)得到服务 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
2)调用aps->setStreamVolumeIndex(stream, index)

7.status_t AudioPolicyService::setStreamVolumeIndex(AudioSystem::stream_type stream, int index)
1)调用mpPolicyManager->setStreamVolumeIndex(stream, index)
status_t AudioPolicyManager::setStreamVolumeIndex(AudioSystem::stream_type stream, int index)
1)记录音量index: mStreams[stream].mIndexCur = index
2)compute and apply stream volume on all outputs:
checkAndSetVolume(stream, index, mOutputs.keyAt(i), mOutputs.valueAt(i)->device())

8.status_t AudioPolicyManager::checkAndSetVolume(int stream, int index, audio_io_handle_t output, uint32_t device, int delayMs, bool force)
1)计算音量:float volume = computeVolume(stream, index, output, device);
2)调用:mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);

9.status_t AudioPolicyService::setStreamVolume(AudioSystem::stream_type stream, float volume, audio_io_handle_t output, int delayMs)
调用mAudioCommandThread->volumeCommand((int)stream, volume, (int)output, delayMs);

10.status_t AudioPolicyService::AudioCommandThread::volumeCommand(int stream, float volume, int output, int delayMs)
调用insertCommand_l(command, delayMs);

 

补充1)在条用getService();获取服务的时候 ,实际调用的是ServiceManager.getService(context);

系统服务都是由serviceManager来管理的,要添加服务,能够调用serviceManager.AddService(context,service);

每添加一个service,都会有对应的惟一context, 当getService的时候就会根据context得到相应的服务,

可查看ServiceManager.java, ServiceManager.h/cpp

补充2) AudioService 的接口在 IaudioService.aidl中定义。添加自定义功能时( 咱们建立控制接口好比建立个音效处理的接口SetEffectVolume(XXX),能够参照SetStreamVolume(a,b,c))别忘了修 改此处,不然,AudioManager 会出现cannot find symbol..错误!!!

 

补充3)编译的时候可能会在Audiomanager.java中调用本身写的接口时出错,此时先将该文件中的调用注释掉,执行 make update-api

执行完成后,将注释去掉,而后重新编译。。。

相关文章
相关标签/搜索