前言
简单跟一下Android中AudioManager申请焦点的代码流程,顺便记录一下,方便自己查阅。
流水账
这里介绍非Car模式(车载模式),如果是Car模式就后续以Android 13来分析。
正文
之前有记录过《》,今天就在之前基础上进行源码的跟进。
app焦点申请
int status = mFocusManager.requestAudioFocus(onAudioFocusChangeListener, AudioAttributes.USAGE_MEDIA,AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN, true);
场景类型 :AudioAttributes.USAGE_MEDIA
焦点流类型 :AudioManager.STREAM_MUSIC
焦点类型 :AudioManager.AUDIOFOCUS_GAIN,永久焦点
是否延迟获取焦点 :true
根据上面,调用的是
mAudioManager.requestAudioFocus(mAudioFocusRequest);
AudioManager.java
frameworks\base\media\java\android\media\AudioManager.java
requestAudioFocus()
public int requestAudioFocus(@NonNull AudioFocusRequest focusRequest) { return requestAudioFocus(focusRequest, null /* no AudioPolicy*/); }
requestAudioFocus()
public int requestAudioFocus(@NonNull AudioFocusRequest afr, @Nullable AudioPolicy ap) { //略 synchronized (mFocusRequestsLock) { try { //AudioService申请焦点 status = service.requestAudioFocus(afr.getAudioAttributes(), afr.getFocusGain(), mICallBack, mAudioFocusDispatcher, clientId, getContext().getOpPackageName(), afr.getFlags(), ap != null ? ap.cb() : null, sdk); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } //不是外部EXT_POLICY(Car模式或着说车载模式) if (status != AudioManager.AUDIOFOCUS_REQUEST_WAITING_FOR_EXT_POLICY) { //返回焦点状态 return status; } //略 }
这里重点在于AudioService.requestAudioFocus()
AudioService.java
\frameworks\base\services\core\java\com\android\server\audio\AudioService.java
requestAudioFocus()
public int requestAudioFocus(AudioAttributes aa, int durationHint, IBinder cb, IAudioFocusDispatcher fd, String clientId, String callingPackageName, int flags, IAudioPolicyCallback pcb, int sdk) { //略 return mMediaFocusControl.requestAudioFocus(aa, durationHint, cb, fd, clientId, callingPackageName, flags, sdk, forceFocusDuckingForAccessibility(aa, durationHint, Binder.getCallingUid())); }
这里也不干事,就一些权限或非法判断,最终调用的是MediaFocusControl中。
参考文章
© 版权声明