前言
简单记录一下CarAudioManager的音量监听,设置音量,获取音量,静音等。记录于此,方便自己回顾。
流水账
正文
之前有简单记录过《
android_car.jar
需要引入android_car.jar
implementation files('../libs/android_car.jar')
监听音量
Car car = Car.createCar(MediaApp.getContext()); mCarAudioManager = (CarAudioManager) car.getCarManager(Car.AUDIO_SERVICE);
//注册监听 mCarAudioManager.registerCarVolumeCallback(this); //注销监听 mCarAudioManager.unregisterCarVolumeCallback(this);
然后重写如下方法
@Override public void onGroupVolumeChanged(int zoneId, int groupId, int flags) { super.onGroupVolumeChanged(zoneId, groupId, flags); //对应音量变化 if (groupId == mCarAudioManager.getVolumeGroupIdForUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)) { //USAGE_VOICE_COMMUNICATION } else if (groupId == mCarAudioManager.getVolumeGroupIdForUsage(AudioAttributes.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE)) { //USAGE_ASSISTANCE_NAVIGATION_GUIDANCE } else if (groupId == mCarAudioManager.getVolumeGroupIdForUsage(AudioAttributes.USAGE_MEDIA)) { //USAGE_MEDIA } else { //other usage } } @Override public void onMasterMuteChanged(int zoneId, int flags) { super.onMasterMuteChanged(zoneId, flags); } @Override public void onGroupMuteChanged(int zoneId, int groupId, int flags) { super.onGroupMuteChanged(zoneId, groupId, flags); //静音监听 }
音量
获取音量
public int getGroupVolume(int zoneId, int groupId) { if (null == mCarAudioManager) { return -1; } return mCarAudioManager.getGroupVolume(zoneId, groupId); }
设置音量
public void setGroupVolume(int groupId, int index, int flags) { if (null != mCarAudioManager) { mCarAudioManager.setGroupVolume(groupId, index, flags); } }
静音
获取静音
public boolean isMuted(int zoneId, int groupId) { if (null == mCarAudioManager) { return false; } return mCarAudioManager.isVolumeGroupMuted(zoneId, groupId); }
设置静音
public void setMute(int zoneId, int groupId, boolean mute, int flags) { if (null != mCarAudioManager) { mCarAudioManager.setVolumeGroupMute(zoneId, groupId, mute, flags); } }
参考文章
© 版权声明