閱讀971 返回首頁    go 技術社區[雲棲]


Android AudioManager控製係統聲音的流程

首先上層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);

最後更新:2017-04-03 12:55:42

  上一篇:go android:layout_weight屬性詳解
  下一篇:go SLB技術原理淺析