(L1) AudioService AudioHandler setDeviceVolume, setAllVolumes, persistVolume

前言

setDeviceVolume, setAllVolumes, persistVolume是volume中较通用的函数

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {

                case MSG_SET_DEVICE_VOLUME:
                    setDeviceVolume((VolumeStreamState) msg.obj, msg.arg1);
                    break;

                case MSG_SET_ALL_VOLUMES:
                    setAllVolumes((VolumeStreamState) msg.obj);
                    break;

                case MSG_PERSIST_VOLUME:
                    persistVolume((VolumeStreamState) msg.obj, msg.arg1);
                    break;

http://blog.csdn.net/wlia/article/details/46877123

setDeviceVolume

 
private void setDeviceVolume(VolumeStreamState streamState, int device) { synchronized (VolumeStreamState.class) { // Apply volume streamState.applyDeviceVolume_syncVSS(device); // Apply change to all streams using this one as alias int numStreamTypes = AudioSystem.getNumStreamTypes(); for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) {
</pre><pre name="code" class="java">
src stream不同(其他的VolumeStreamState),<span style="font-family: Arial, Helvetica, sans-serif;">dst stream相同 (</span>dst stream 是该VolumeStreamState的src stream),那么必须有<span style="font-family: Arial, Helvetica, sans-serif;">VolumeStreamState的</span>src stream就是dst stream, 这种stream 没有alias
</pre><pre name="code" class="java">                    if (streamType != streamState.mStreamType &&
                            mStreamVolumeAlias[streamType] == streamState.mStreamType) {
                        // Make sure volume is also maxed out on A2DP device for aliased stream
                        // that may have a different device selected
</pre><pre name="code" class="java">get device
                        int streamDevice = getDeviceForStream(streamType);
</pre><pre name="code" class="java">对A2DP有判断
                        if ((device != streamDevice) && mAvrcpAbsVolSupported &&
                                ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0)) {
                            mStreamStates[streamType].applyDeviceVolume_syncVSS(device);
                        }
apply
                        mStreamStates[streamType].applyDeviceVolume_syncVSS(streamDevice);
                    }
                }
            }
持久保存
            // Post a persist volume msg
            sendMsg(mAudioHandler,
                    MSG_PERSIST_VOLUME,
                    SENDMSG_QUEUE,
                    device,
                    0,
                    streamState,
                    PERSIST_DELAY);

        }

http://blog.csdn.net/wlia/article/details/46877123

相关函数

 

applyDeviceVolume_syncVSS

通过getIndex取得数据,那么之前setIndex,所以这里传入device即可。
A2DP index max
<pre name="code" class="java">AudioSystem.setStreamVolumeIndex下派参数
        public void applyDeviceVolume_syncVSS(int device) {
            int index;
            if (isMuted_syncVSS()) {
                index = 0;
            } else if (((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && mAvrcpAbsVolSupported)
                    || ((device & mFullVolumeDevices) != 0)) {
                index = (mIndexMax + 5)/10;
            } else {
                index = (getIndex(device) + 5)/10;
            }
            AudioSystem.setStreamVolumeIndex(mStreamType, index, device);
        }

http://blog.csdn.net/wlia/article/details/46877123

persistVolume

 
        private void persistVolume(VolumeStreamState streamState, int device) {
            if (mUseFixedVolume) {
                return;
            }
</pre><pre name="code" class="java">电视只有music才允许persist
            if (isPlatformTelevision() && (streamState.mStreamType != AudioSystem.STREAM_MUSIC)) {
                return;
            }
            System.putIntForUser(mContentResolver,
                      streamState.getSettingNameForDevice(device),
                      (streamState.getIndex(device) + 5)/ 10,
                      UserHandle.USER_CURRENT);
        }

http://blog.csdn.net/wlia/article/details/46877123

setAllVolumes

 
        private void setAllVolumes(VolumeStreamState streamState) {

            // Apply volume
            streamState.applyAllVolumes();

            // Apply change to all streams using this one as alias
            int numStreamTypes = AudioSystem.getNumStreamTypes();
            for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) {
                if (streamType != streamState.mStreamType &&
                        mStreamVolumeAlias[streamType] == streamState.mStreamType) {
                    mStreamStates[streamType].applyAllVolumes();
                }
            }
        }
 
http://blog.csdn.net/wlia/article/details/46877123

相关函数

applyAllVolumes

        public void applyAllVolumes() {

            synchronized (VolumeStreamState.class) {
<ul style="margin: 0px; padding: 0px; border: 0px; outline: 0px; list-style: none; color: rgb(67, 67, 67); font-family: Tahoma, Arial; line-height: 24px; widows: auto; background-color: rgb(242, 242, 242);"><li style="margin: 0px; padding: 0px; border: 0px; outline: 0px; list-style: none;">按照惯例,<span style="widows: auto; background-color: rgb(240, 240, 240);">apply default volume first.看来是AudioPolicyManager需要</span></li></ul>                // apply default volume first: by convention this will reset all
                // devices volumes in audio policy manager to the supplied value

                int index;
有Mute则为0
                if (isMuted_syncVSS()) {
                    index = 0;
                } else {
                    index = (getIndex(AudioSystem.DEVICE_OUT_DEFAULT) + 5)/10;
                }
下派参数
                AudioSystem.setStreamVolumeIndex(mStreamType, index, AudioSystem.DEVICE_OUT_DEFAULT);
                // then apply device specific volumes

对mIndex中每个选项进行处理
                Set set = mIndex.entrySet();
                Iterator i = set.iterator();
                while (i.hasNext()) {
                    Map.Entry entry = (Map.Entry)i.next();
                    int device <span style="font-family: Arial, Helvetica, sans-serif;">= ((Integer)entry.getKey()).intValue();</span>
<span style="font-family: Arial, Helvetica, sans-serif;">
</span>
如果是default device,则不再处理。。。这些代码真臭!if(  == out default) return;更好!Google也不过如此。

                    if (device != AudioSystem.DEVICE_OUT_DEFAULT) {
还是先处理mute
                        if (isMuted_syncVSS()) {
                            index = 0;

out A2DP, mAvrcpAbsVolSupported则max
                        } else if (((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 &&
                                mAvrcpAbsVolSupported)
                                    || ((device & mFullVolumeDevices) != 0))
                        {
                            index = (mIndexMax + 5)/10;
                        } else {
最普通的情况往往在最后
                            index = ((Integer)entry.getValue() + 5)/10;
                        }
                        AudioSystem.setStreamVolumeIndex(mStreamType, index, device);
                    }
                }
            }
        }
http://blog.csdn.net/wlia/article/details/46877123
 
发布了27 篇原创文章 · 获赞 2 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/wlia/article/details/46877123