Android AudioManager控制系统声音的流程

xiaoxiao2021-02-27  275

 

 

首先上层Java调用

XXXPlayer

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

audiomanager就是我们定义的控制系统声音的对象,(如果context报错,可将其改成XXXPlayer.this)

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

1.AudioManager.java

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

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

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

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);

 

 

android/frameworks/av/services/audiopolicy/common/managerdefinitions/src/StreamDescriptor.cpp android/frameworks/av/services/audiopolicy/common/managerdefinitions/src/Gains.cpp

getVolumeIndexMax

 

 

 

http://blog.csdn.net/newtonnl/article/details/8455136

 

android音量控制曲线和调用过程

Android音频曲线调用从ui界面往下设置,根据stream音频有不同的UI,分为7步,15步,对应到实际的100步。这个转换过程可以参考AudioPolicyManagerBase.cpp中的volIndexToAmpl函数。

 

 

 

4.1上调用流程与之前的有所不同,原理还是一样的。之前的版本我看是直线型的,新的是3段折线的。为什么是直线,人耳对声音大小的感知程度并不是线性的,而是呈对数关系。这个可以和android原理对应起来

 以10为底的对数。

 

下面是分贝公式的推导过程

 

 

 

 

 

 

音频当中db用加减表示调整音量大小。

 

目前采用3段折线,我觉得可以更方便的调整声音在不同区域的变化大小。最低声音和最高声音应该需要从更底层驱动来调整。

 

volIndexToAmpl这个函数实现了上面的转换关系。

 

该函数是通过响应音量键效益开始执行下面调用过程,完成音量大小调节的。

 

下面是音量键按下后的调用过程

 

Audiomanager.java

 

handleKeyDown

 

         adjustSuggestStreamVolume

 

                    service. adjustSuggestStreamVolume

 

AudioService.java

 

adjustSuggestStreamVolume

 

         adjustStreamVolum

 

         sendMSG(mAudioHandler,MSG_SET_DEVICE_VOLUME,SND_QUEUE,device,0,streamstate,0);

 

消息机制

 

AudioService.java

 

AudioHandler

 

handleMessage

 

         setDeviceVolume

 

                   StreamState.applyDeviceVolume

 

                            audioSystem.setStreamVolumeIndex

 

JNI机制

 

Android_media_AudioSystem.cpp

 

AudioPolicyManagerBase.cpp

 

setStreamVolumeIndex

 

         checkAndSetVolume

 

                   computeVolume

 

                            volIndexToAmpl

 

 

 

 

Audio System调节音量分析

http://blog.csdn.net/xiejinfeng850414/article/details/8207242

 

 

Android 4.4 音量调节流程分析(二)

 

 

 

http://www.cnblogs.com/Peter-Chen/p/3859498.html

 

 

转载请注明原文地址: https://www.6miu.com/read-4294.html

最新回复(0)