当拨打电话,并且使用蓝牙耳机的情况下,如果调节音量键-,使STREAM_MUSIC音频流音量为0,会导致通话静音,即无法听到声音,因此需要,此条件下直接返回-1,对按键进行拦截处理,不再向下分发。
//frameworks\base\services\core\java\com\android\server\policy\PhoneWindowManager.java
@Override
public long interceptKeyBeforeDispatching(WindowState win, KeyEvent event, int policyFlags) {
.......
else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP
|| keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
|| keyCode == KeyEvent.KEYCODE_VOLUME_MUTE) {
if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN && mTelecomManager.isInCall() && mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC) <= 1){
return -1;
}
if (mUseTvRouting || mHandleVolumeKeysInWM) {
// On TVs or when the configuration is enabled, volume keys never
// go to the foreground app.
dispatchDirectAudioEvent(event);
return -1;
}
.......
这里用mTelecomManager.isInCall()
来判断是否为通话中。
然后,后续发现,还存在2种导致通话静音的情况。
1.使用蓝牙耳机按键
2.快速连按KEYCODE_VOLUME_DOWN(音量-)
解决办法分别是在setStreamVolume和adjustSuggestedStreamVolume中添加return;的代码。
//frameworks\base\media\java\android\media\AudioManager.java
public void setStreamVolume(int streamType, int index, int flags) {
final IAudioService service = getService();
Log.d(TAG, "setStreamVolume: StreamType = " + streamType + ", index = " + index + ", isInCall = " + mTelecomManager.isInCall());
if ( mTelecomManager.isInCall() && streamType == STREAM_BLUETOOTH_SCO && index == 0){
Log.d(TAG,"before return");
return;
}
try {
service.setStreamVolume(streamType, index, flags, getContext().getOpPackageName());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
//frameworks\base\services\core\java\com\android\server\audio\AudioService.java
private void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags,
String callingPackage, String caller, int uid) {
........
if (mTelecomManager.isInCall() && direction == AudioManager.ADJUST_LOWER && getStreamVolume(AudioSystem.STREAM_MUSIC) == 1){
return;
}
........
}
在快速按下音量-的时候,interceptKeyBeforeDispatching中的拦截失效了,看log可知,调用的是adjustSuggestedStreamVolume,因此在此处拦截。
网友评论