1. 框架图
2. 涉及到的文件
android.media.AudioManager.java
android.media.AudioSystem.java
android.media.IAudioService.aidl
com.android.server.audio.AudioService.java
android_media_AudioSystem.cpp
AudioPolicyService.cpp AudioPolicyService.h
AudioSystem.cpp AudioSystem.h
IAudioPolicyService.cpp IAudioPolicyService.h
IAudioPolicyServiceClient.cpp IAudioPolicyServiceClient.h
3. Read the fucking codes
Java层:
a. 业务接口
入口AudioManager: 实际上是对AudioService的封装(private static IAudioService sService;)
广播及其携带的参数:
ACTION_AUDIO_BECOMING_NOISY = "android.media.AUDIO_BECOMING_NOISY";
RINGER_MODE_CHANGED_ACTION = "android.media.RINGER_MODE_CHANGED";
INTERNAL_RINGER_MODE_CHANGED_ACTION = "android.media.INTERNAL_RINGER_MODE_CHANGED";
new ringer mode: RINGER_MODE_NORMAL RINGER_MODE_SILENT RINGER_MODE_VIBRATE
VIBRATE_SETTING_CHANGED_ACTION = "android.media.VIBRATE_SETTING_CHANGED";
VOLUME_CHANGED_ACTION = "android.media.VOLUME_CHANGED_ACTION";
STREAM_DEVICES_CHANGED_ACTION = "android.media.STREAM_DEVICES_CHANGED_ACTION";
STREAM_MUTE_CHANGED_ACTION = "android.media.STREAM_MUTE_CHANGED_ACTION";
new vibrate : EXTRA_VIBRATE_SETTING EXTRA_VIBRATE_TYPE
携带的广播(key):
EXTRA_VOLUME_STREAM_TYPE = "android.media.EXTRA_VOLUME_STREAM_TYPE";
EXTRA_VOLUME_STREAM_TYPE_ALIAS = "android.media.EXTRA_VOLUME_STREAM_TYPE_ALIAS";
EXTRA_VOLUME_STREAM_VALUE = "android.media.EXTRA_VOLUME_STREAM_VALUE";
EXTRA_PREV_VOLUME_STREAM_VALUE = "android.media.EXTRA_PREV_VOLUME_STREAM_VALUE";
。。。
b. 真正实现代码在AudioService:
public class AudioService extends IAudioService.Stub {}
c. AudioSystem.java
功能接口,操作Audio设备。
4. 举个例子
AudioManager
-
两种带Handler内部类的使用
/** *Helper class to handle the forwarding of audio focus events to theappropriate listener */ privateclass FocusEventHandlerDelegate { privatefinal Handler mHandler; FocusEventHandlerDelegate(){ Looperlooper; if((looper = Looper.myLooper()) == null) { looper= Looper.getMainLooper(); } if(looper != null) { //implement the event handler delegate to receive audio focus events mHandler= new Handler(looper) { @Override publicvoid handleMessage(Message msg) { OnAudioFocusChangeListenerlistener = null; synchronized(mFocusListenerLock){ listener= findFocusListener((String)msg.obj); } if(listener != null) { Log.d(TAG,"AudioManager dispatching nAudioFocusChange(" +msg.what + ") for " + msg.obj); listener.onAudioFocusChange(msg.what); } } }; }else { mHandler= null; } }
HandlergetHandler() { returnmHandler; } } |
/** *Helper class to handle the forwarding of native events to theappropriate listener *(potentially) handled in a different thread. */ privateclass NativeEventHandlerDelegate { privatefinal Handler mHandler;
NativeEventHandlerDelegate(finalAudioDeviceCallback callback, Handlerhandler) { //find the looper for our new event handler Looperlooper; if(handler != null) { looper= handler.getLooper(); }else { //no given handler, use the looper the addListener call was calledin looper= Looper.getMainLooper(); } //construct the event handler with this looper if(looper != null) { //implement the event handler delegate mHandler= new Handler(looper) { @Override publicvoid handleMessage(Message msg) { switch(msg.what){ caseMSG_DEVICES_CALLBACK_REGISTERED: caseMSG_DEVICES_DEVICES_ADDED: if(callback != null) { callback.onAudioDevicesAdded((AudioDeviceInfo[])msg.obj); } break; caseMSG_DEVICES_DEVICES_REMOVED: if(callback != null) { callback.onAudioDevicesRemoved((AudioDeviceInfo[])msg.obj); } break; default: Log.e(TAG,"Unknown native event type: " + msg.what); break; } } }; }else { mHandler= null; } } HandlergetHandler() { returnmHandler; } } |
-
常见应用场景
1 2 3 4 5 6 | //音量控制,初始化定义 AudioManagermAudioManager =(AudioManager)getSystemService(Context.AUDIO_SERVICE); //最大音量 int maxVolume= mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); //当前音量 int currentVolume= mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); |
直接控制音量的多少:
1 2 3 4 5 | if(isSilent){ mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,0,0); }else{ mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,tempVolume, 0);//tempVolume:音量绝对值 } |
以一步步长控制音量的增减,并弹出系统默认音量控制条:
1 2 3 4 5 6 7 8 9 10 | //降低音量,调出系统音量控制 if(flag== 0){ mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER,AudioManager.FX_FOCUS_NAVIGATION_UP); } //增加音量,调出系统音量控制 else if(flag== 1){ mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_RAISE,AudioManager.FX_FOCUS_NAVIGATION_UP); } |
3.扩展:Handler
3.1对外四个初始化接口:有无looper、callback的组合
publicHandler() { this(null,false); } publicHandler(Callback callback) { this(callback,false); } publicHandler(Looper looper) { this(looper,null, false); } publicHandler(Looper looper, Callback callback) { this(looper,callback, false); } |
3.2内部初始化接口:带looper和不带looper的
publicHandler(Callback callback, boolean async) { if(FIND_POTENTIAL_LEAKS) { finalClass<? extends Handler> klass = getClass(); if((klass.isAnonymousClass() || klass.isMemberClass() ||klass.isLocalClass()) && (klass.getModifiers()& Modifier.STATIC) == 0) { Log.w(TAG,"The following Handler class should be static or leaks mightoccur: " + klass.getCanonicalName()); } } mLooper= Looper.myLooper(); if(mLooper == null) { thrownew RuntimeException( "Can'tcreate handler inside thread that has not calledLooper.prepare()"); } mQueue= mLooper.mQueue; mCallback= callback; mAsynchronous= async; } publicHandler(Looper looper, Callback callback, boolean async) { mLooper= looper; mQueue= looper.mQueue; mCallback= callback; mAsynchronous= async; } |
3.3分发Message:分三步走。
/** *Handle system messages here. */ publicvoid dispatchMessage(Message msg) { if(msg.callback != null) { handleCallback(msg); }else { if(mCallback != null) { if(mCallback.handleMessage(msg)) { return; } } handleMessage(msg);// new handler时必须实现,见前面小节的AudioManager内部类的两种实现。 } } |