ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android AudioService安全音量设置逻辑

2021-06-07 15:33:21  阅读:151  来源: 互联网

标签:volume int MEDIA SAFE VOLUME 音量 AudioService device Android


问题点描述:
还原出厂设置后,第一次启动安卓系统会自动降音量设成10,导致开机时音量不一致问题。

原因:安全音量逻辑将音量设置为10

安全音量配置和音量值
\frameworks\base\core\res\res\values\config.xml

    <!-- Safe headphone volume index. When music stream volume is below this index
    the SPL on headphone output is compliant to EN 60950 requirements for portable music
    players. -->
    <integer name="config_safe_media_volume_index">10</integer>

    <!-- Whether safe headphone volume is enabled or not (country specific). -->
    <bool name="config_safe_media_volume_enabled">true</bool>

安全音量逻辑:
frameworks\base\services\core\java\com\android\server\audio\AudioService.java

// mSafeMediaVolumeDevices lists the devices for which safe media volume is enforced,
private final int mSafeMediaVolumeDevices = AudioSystem.DEVICE_OUT_WIRED_HEADSET |
                                           	AudioSystem.DEVICE_OUT_WIRED_HEADPHONE |
                                           	AudioSystem.DEVICE_OUT_USB_HEADSET;


///
// Construction
///
/** @hide */
public AudioService(Context context) {
	//...
	
    // The default safe volume index read here will be replaced by the actual value when
    // the mcc is read by onConfigureSafeVolume()
    mSafeMediaVolumeIndex = mContext.getResources().getInteger(
            com.android.internal.R.integer.config_safe_media_volume_index) * 10;
	
	//...
}

public void onSystemReady() {
	//...
    sendMsg(mAudioHandler,
            MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED,
            SENDMSG_REPLACE,
            0,
            0,
            TAG,
            SystemProperties.getBoolean("audio.safemedia.bypass", false) ?
                    0 : SAFE_VOLUME_CONFIGURE_TIMEOUT_MS);
	//...
}

/** Handles internal volume messages in separate volume thread. */
private class AudioHandler extends Handler {
	//...
	@Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED:
            case MSG_CONFIGURE_SAFE_MEDIA_VOLUME:
                onConfigureSafeVolume((msg.what == MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED),
                        (String) msg.obj);
                break;
        }
    }
	//...
}

private void onConfigureSafeVolume(boolean force, String caller) {
    synchronized (mSafeMediaVolumeState) {
        int mcc = mContext.getResources().getConfiguration().mcc;
        if ((mMcc != mcc) || ((mMcc == 0) && force)) {
            mSafeMediaVolumeIndex = mContext.getResources().getInteger(
                    com.android.internal.R.integer.config_safe_media_volume_index) * 10;

            mSafeUsbMediaVolumeIndex = getSafeUsbMediaVolumeIndex();

            boolean safeMediaVolumeEnabled =
                    SystemProperties.getBoolean("audio.safemedia.force", false)
                    || mContext.getResources().getBoolean(
                            com.android.internal.R.bool.config_safe_media_volume_enabled);

            boolean safeMediaVolumeBypass =
                    SystemProperties.getBoolean("audio.safemedia.bypass", false);

            // The persisted state is either "disabled" or "active": this is the state applied
            // next time we boot and cannot be "inactive"
            int persistedState;
            if (safeMediaVolumeEnabled && !safeMediaVolumeBypass) {
                persistedState = SAFE_MEDIA_VOLUME_ACTIVE;
                // The state can already be "inactive" here if the user has forced it before
                // the 30 seconds timeout for forced configuration. In this case we don't reset
                // it to "active".
                if (mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_INACTIVE) {
                    if (mMusicActiveMs == 0) {
                        mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE;
                        enforceSafeMediaVolume(caller);
                    } else {
                        // We have existing playback time recorded, already confirmed.
                        mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_INACTIVE;
                    }
                }
            } else {
                persistedState = SAFE_MEDIA_VOLUME_DISABLED;
                mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_DISABLED;
            }
            mMcc = mcc;
            sendMsg(mAudioHandler,
                    MSG_PERSIST_SAFE_VOLUME_STATE,
                    SENDMSG_QUEUE,
                    persistedState,
                    0,
                    null,
                    0);
        }
    }
}

private void enforceSafeMediaVolume(String caller) {
    VolumeStreamState streamState = mStreamStates[AudioSystem.STREAM_MUSIC];
    int devices = mSafeMediaVolumeDevices;
    int i = 0;

    while (devices != 0) {
        int device = 1 << i++;
        if ((device & devices) == 0) {
            continue;
        }
        int index = streamState.getIndex(device);
        if (index > safeMediaVolumeIndex(device)) {
            streamState.setIndex(safeMediaVolumeIndex(device), device, caller);
            sendMsg(mAudioHandler,
                    MSG_SET_DEVICE_VOLUME,
                    SENDMSG_QUEUE,
                    device,
                    0,
                    streamState,
                    0);
        }
        devices &= ~device;
    }
}

private int safeMediaVolumeIndex(int device) {
    if ((device & mSafeMediaVolumeDevices) == 0) {
        return MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC];
    }
    if (device == AudioSystem.DEVICE_OUT_USB_HEADSET) {
        return mSafeUsbMediaVolumeIndex;
    } else {
        return mSafeMediaVolumeIndex;
    }
}

config_safe_media_volume_enabled的配置是一个关键,决定了安全音量的功能是否有效。

标签:volume,int,MEDIA,SAFE,VOLUME,音量,AudioService,device,Android
来源: https://blog.csdn.net/Sunxiaolin2016/article/details/117654459

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有