android Audio设置音量流程及其binder通讯原理
Audio java部分代码流程(4.1.2 version): 在frameworks/base/media/java/android/media中:
IAudioService.aidl提供了所有对外的接口函数,如下:
[cpp]
inte易做图ce IAudioService {
void adjustVolume(int direction, int flags);
void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags);
void adjustStreamVolume(int streamType, int direction, int flags);
void setStreamVolume(int streamType, int index, int flags);
void setStreamSolo(int streamType, boolean state, IBinder cb);
void setStreamMute(int streamType, boolean state, IBinder cb);
boolean isStreamMute(int streamType);
int getStreamVolume(int streamType);
int getStreamMaxVolume(int streamType);
int getLastAudibleStreamVolume(int streamType);
void setRingerMode(int ringerMode);
.......................
}
这些函数的实现在服务AudioService.java中:
[cpp]
public class AudioService extends IAudioService.Stub {
........
}
该服务在services/java/com/android/server/SystemServer.java中注册进servicemanager中:
[cpp]
492 try {
493 Slog.i(TAG, "Audio Service");
494 ServiceManager.addService(Context.AUDIO_SERVICE, new AudioService(context));
495 } catch (Throwable e) {
496 reportWtf("starting Audio Service", e);
497 }
这样在frameworks中可以通过ServiceManager.getService(Context.AUDIO_SERVICE)来获得该服务。
而AudioManager.java则实现这些函数了对外应用的接口,比如:
[cpp]
496 public void adjustVolume(int direction, int flags) {
497 IAudioService service = getService(); //
498 try {
499 service.adjustVolume(direction, flags);
500 } catch (RemoteException e) {
501 Log.e(TAG, "Dead object in adjustVolume", e);
502 }
503 }
这里的getService()获得的是前面的AudioService,如下:
[cpp]
365 private static IAudioService getService()
366 {
367 if (sService != null) {
368 return sService;
369 }
370 IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE);
371 sService = IAudioService.Stub.asInte易做图ce(b);
372 return sService;
373 }
要把AudioManager.java中的对外公开,则要在 core/java/android/app/ContextImpl.java中注册:
[cpp]
281 registerService(AUDIO_SERVICE, new ServiceFetcher() {
282 public Object createService(ContextImpl ctx) {
283 return new AudioManager(ctx);
284 }});
而且,还要把这些函数变量等在api/current.txt中声明:
[cpp]
10353 public class AudioManager {
10354 method public int abandonAudioFocus(android.media.AudioManager.OnAudioFocusChangeListener);
10355 method public void adjustStreamVolume(int, int, int);
10356 method public void adjustSuggestedStreamVolume(int, int, int);
10357 method public void adjustVolume(int, int);
10358 method public int getMode();
10359 method public java.lang.String getParameters(java.lang.String);
10360 method public int getRingerMode();
10361 method public deprecated int getRouting(int);
10362 method public int getStreamMaxVolume(int);
10363 method public int getStreamVolume(int);
10364 method public int getVibrateSetting(int);
10365 method public boolean isBluetoothA2dpOn();
这样在应用中用(AudioManager) getSystemService(Context.AUDIO_SERVICE);即可获得服务,从而使用AudioManager.java中的方法然后看看比如setStreamVolume是如何从java调用到底层的:
1、应用调用比如mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
2、AudioManager.java调用public void setStreamVolume(int streamType, int index, int flags){};
3、AudioService.java调用public void setStreamVolume(int streamType, int index, int flags){},在该函数中调用
[cpp]
setStreamVolumeInt(mStreamVolumeAlias[streamType],
831 index,
832 device,
833  
补充:移动开发 , Android ,