Android中绝对音量和相对音量设置
前言:
绝对音量: 手机端不处理音量,只把当前音量告诉耳机,耳机端处理音量。
相对音量: 手机端处理,耳机端不确定是默认最大,还是有一个默认音量还是按照之前设置的绝对音量的值处理的。
手机音量转换绝对音量,此处会有一个计算过程,大致就是手机侧计算出当前音量和最大音量的百分比然后设置给耳机,耳机端收到百分比去处理。不然每个手机的音量范围不同,不做归一化就很难适配。
当通过音量按键调节音量时,会通过input事件分发,input把event分发给mediassesion, mediasession调用audiomanager的adjuststreamvolume调节音量。
当通过拖动音量条调节音量时,settings app会调用audiomanager的setstreamvolume调节音量。
adjuststreamvolume和setstreamvolume处理过程类似,接下来只看setstreamvolume。
setstreamvolume调用
// frameworks/base/media/java/android/media/audiomanager.java public void setstreamvolume(int streamtype, int index, int flags) { final iaudioservice service = getservice(); try { service.setstreamvolume(streamtype, index, flags, getcontext().getoppackagename()); // audiomanager调用的是audioservice的setstreamvolume } catch (remoteexception e) { throw e.rethrowfromsystemserver(); } }
audioservice的调用过程:
// frameworks/base/services/core/java/com/android/server/audio/audioservice.java private void setstreamvolume(int streamtype, int index, int flags, string callingpackage, string caller, int uid, boolean hasmodifyaudiosettings) { ensurevalidstreamtype(streamtype); int streamtypealias = mstreamvolumealias[streamtype]; volumestreamstate streamstate = mstreamstates[streamtypealias]; inal int device = getdeviceforstream(streamtype); // 获取streamtype对应的device int oldindex; synchronized (msafemediavolumestatelock) { // reset any pending volume command mpendingvolumecommand = null; oldindex = streamstate.getindex(device); android.media.audioserviceinjector.moriginalindexwhensetstreamvolume = index; index = rescaleindex(index * 10, streamtype, streamtypealias); // 设置绝对音量 if (streamtypealias == audiosystem.stream_music && audiosystem.device_out_all_a2dp_set.contains(device) && (flags & audiomanager.flag_bluetooth_abs_volume) == 0) { mdevicebroker.postsetavrcpabsolutevolumeindex(index / 10); } if (streamtypealias == audiosystem.stream_music) { setsystemaudiovolume(oldindex, index, getstreammaxvolume(streamtype), flags); // 这里是和hdmi相关,不用管 } flags &= ~audiomanager.flag_fixed_volume; if (streamtypealias == audiosystem.stream_music && isfixedvolumedevice(device)) { flags |= audiomanager.flag_fixed_volume; // volume is either 0 or max allowed for fixed volume devices if (index != 0) { if (msafemediavolumestate == safe_media_volume_active && msafemediavolumedevices.contains(device)) { index = safemediavolumeindex(device); } else { index = streamstate.getmaxindex(); } } } if (!checksafemediavolume(streamtypealias, index, device)) { mvolumecontroller.postdisplaysafevolumewarning(flags); // 这里应该就是安全提醒,比如音量过大损伤听力的提醒 mpendingvolumecommand = new streamvolumecommand(streamtype, index, flags, device); } else { onsetstreamvolume(streamtype, index, flags, device, caller, hasmodifyaudiosettings); // 调用onsetstreamvolume设置音量 index = mstreamstates[streamtype].getindex(device); } } sendvolumeupdate(streamtype, oldindex, index, flags, device); }
onsetstreamvolume 和 sendvolumeupdate的作用:
onsetstreamvolume -> 设置音量到底层并处理一些静音逻辑。
sendvolumeupdate -> 通知ui更新音量值
这里插入个当音量设置到0时自动静音的处理:
// frameworks/base/services/core/java/com/android/server/audio/audioservice.java private void onsetstreamvolume(int streamtype, int index, int flags, int device, string caller, boolean hasmodifyaudiosettings) { final int stream = mstreamvolumealias[streamtype]; setstreamvolumeint(stream, index, device, false, caller, hasmodifyaudiosettings); // 设置音量到底层 // setting volume on ui sounds stream type also controls silent mode if (((flags & audiomanager.flag_allow_ringer_modes) != 0) || (stream == getuisoundsstreamtype())) { int ringermode = getnewringermode(stream, index, flags); // 获取ringmode miuiringermode = miui.util.audiomanagerhelper.getvalidatedringermode(mcontext, miuiringermode); // 获取ringmode setringermode(ringermode, tag + ".onsetstreamvolume", false /*external*/); // 设置ringmode } } private int getnewringermode(int stream, int index, int flags) { if (((flags & audiomanager.flag_allow_ringer_modes) != 0) || (stream == getuisoundsstreamtype())) { int newringermode; if (index == 0) { // 如果设置的音量是0 // 如果有震动就设置ringmode是震动模式,否则判断volumedowntoentersilent决定设置静音还是不静音 newringermode = mhasvibrator ? audiomanager.ringer_mode_vibrate : mvolumepolicy.volumedowntoentersilent ? audiomanager.ringer_mode_silent : audiomanager.ringer_mode_normal; } else { newringermode = audiomanager.ringer_mode_normal; // 如果音量不是0,ringmode设置为非静音 } return newringermode; } return getringermodeexternal(); }
设置绝对音量: postsetavrcpabsolutevolumeindex
// frameworks/base/services/core/java/com/android/server/audio/audiodevicebroker.java void postsetavrcpabsolutevolumeindex(int index) { sendimsgnodelay(msg_i_set_avrcp_absolute_volume, sendmsg_replace, index); } // 收到消息msg_i_set_avrcp_absolute_volume,调用mbthelper.setavrcpabsolutevolumeindex(msg.arg1); // frameworks/base/services/core/java/com/android/server/audio/bthelper.java synchronized void setavrcpabsolutevolumeindex(int index) { ma2dp.setavrcpabsolutevolume(index); }
蓝牙侧绝对音量处理:
// vendor/qcom/opensource/commonsys/packages/apps/bluetooth/src/com/android/bluetooth/a2dp/a2dpservice.java public void setavrcpabsolutevolume(int volume) { if (mfactory.getavrcptargetservice() != null) { mfactory.getavrcptargetservice().sendvolumechanged(volume); return; } if(apmconstintf.getleaudioenabled()) { volumemanagerintf mvolumemanager = volumemanagerintf.get(); mvolumemanager.setmediaabsolutevolume(volume); return; } synchronized(mbtavrcplock) { if (mavrcp_ext != null) { mavrcp_ext.setabsolutevolume(volume); return; } if (mavrcp != null) { mavrcp.setabsolutevolume(volume); } } } // vendor/qcom/opensource/commonsys/bluetooth_ext/packages_apps_bluetooth_ext/src/avrcp/avrcp_ext.java public void setabsolutevolume(int volume) { message msg = mhandler.obtainmessage(msg_set_absolute_volume, volume, 0); mhandler.sendmessage(msg); } 收到消息msg_set_absolute_volume之后调用如下 case msg_set_absolute_volume: { int avrcpvolume = converttoavrcpvolume(msg.arg1); ...... } // 计算avrcpvolume private int converttoavrcpvolume(int volume) { if(maudiostreammax == 150) { return (int) math.round((double) volume*avrcp_max_vol/maudiostreammax); } return (int) math.ceil((double) volume*avrcp_max_vol/maudiostreammax); }
从这里可以看到,蓝牙侧传给耳机的音量其实就是百分比。
从相对音量切换到绝对音量的处理:
蓝牙app有一个notificationreceiver,当绝对音量的开关发生变化notificationreceiver就会收到信息,然后做如下处理:
private class notificationreceiver extends broadcastreceiver { public void onreceive(context context, intent intent) { if(abs_volume_action.equals(intent.getaction())) { handledeviceabsvolume(mac, value); } } } // handledeviceabsvolume就会做对应的处理,把音量值发送给耳机侧。
此处每次切换的时候会有log打印,log mask: handledeviceabsvolume|setvolumenative
切换之后播放声音就会调用checkandsetvolume把音量值设置到最大,保证手机侧无音量处理。
从绝对音量切换到相对音量的处理:
和上面一样,蓝牙app notificationreceiver会收到绝对音量开关关闭的消息,然后把消息发给耳机端。然后还会调用audio侧设置stream的音量。
log mask: handledeviceabsvolume|setstreamvolume
切换之后播放声音就会调用checkandsetvolume把音量值设置到当前实际的音量值。
总结
到此这篇关于android中绝对音量和相对音量设置的文章就介绍到这了,更多相关android音量设置内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!