Android 收音机音量开到最大都没有“将音量调高到安全级别以上?会损坏听力”提示

前言
         欢迎大家我分享和推荐好用的代码段~~
声明
         欢迎转载,但请保留文章原始出处:
         CSDN
http://www.csdn.net
         雨季o莫忧离:http://blog.csdn.net/luckkof

正文


[DESCRIPTION]
插入耳机端口,打开音乐。侧键音量开到最大,出现提示框“将音量调高到安全级别以上?会损坏听力”这样的提示。而同样的路径打开收音机,点击侧音量键把音量调到最大,没有相应的提示。
[SOLUTION]
修改
1,    private static final int MSG_CHECK_FM_ACTIVE = 29;
2,
    private void onCheckFmActive() {
        synchronized (mSafeMediaVolumeState) {
            if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE) {
                int device = getDeviceForStream(AudioSystem.STREAM_FM);
                if ((device & mSafeMediaVolumeDevices) != 0) {
                    sendMsg(mAudioHandler, MSG_CHECK_FM_ACTIVE, SENDMSG_REPLACE, 0, 0, null,
                            FM_ACTIVE_POLL_PERIOD_MS);
                    int index = mStreamStates[AudioSystem.STREAM_FM]
                            .getIndex(device, false /* lastAudible */);
                    if (AudioSystem.isStreamActive(AudioSystem.STREAM_FM, 0)
                            && (index > mSafeMediaVolumeIndex)) {
                        // Approximate cumulative active music time
                        mFmeMs += FM_ACTIVE_POLL_PERIOD_MS;
                        if (mFmeMs > UNSAFE_VOLUME_FM_ACTIVE_MS_MAX) {
                            setSafeMediaVolumeEnabled(true);
                            mFmeMs = 0;
                            mVolumePanel.postDisplaySafeVolumeWarning();
                        }
                    }
                }
            }
        }
    }
3,
在                case MSG_CHECK_MUSIC_ACTIVE:
                    onCheckMusicActive();
                    break;
后面增加
               /// M: safe media for FM @{
                case MSG_CHECK_FM_ACTIVE:
                    onCheckFmActive();
                    break;
                /// safe media for FM @}
4, 在    private void onSetWiredDeviceConnectionState(int device, int state, String name)
                    sendMsg(mAudioHandler,
                            MSG_CHECK_MUSIC_ACTIVE,
                            SENDMSG_REPLACE,
                            0,
                            0,
                            null,
                            MUSIC_ACTIVE_POLL_PERIOD_MS);
后面增加
                    /// safe media for FM @{
                    sendMsg(mAudioHandler, MSG_CHECK_FM_ACTIVE, SENDMSG_REPLACE, 0, 0, null,
                            FM_ACTIVE_POLL_PERIOD_MS);
                    /// safe media for FM @}
5,
在    private static final int SAFE_VOLUME_CONFIGURE_TIMEOUT_MS = 30000;  // 30s after boot completed
后面增加如下
    private int mFmeMs;
    private static final int UNSAFE_VOLUME_FM_ACTIVE_MS_MAX = (20 * 3600 * 1000); // 20
                                                                                  // hours
    private static final int FM_ACTIVE_POLL_PERIOD_MS = 60000; // 1 minute
6,以下几个函数改为如下
    private void setSafeMediaVolumeEnabled(boolean on) {
        synchronized (mSafeMediaVolumeState) {
            if ((mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_NOT_CONFIGURED)
                    && (mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_DISABLED)) {
                if (on && (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE)) {
                    mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE;
                    enforceSafeMediaVolume();
                } else if (!on && (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE)) {
                    mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_INACTIVE;
                    mMusicActiveMs = 0;
                    sendMsg(mAudioHandler, MSG_CHECK_MUSIC_ACTIVE, SENDMSG_REPLACE, 0, 0, null,
                            MUSIC_ACTIVE_POLL_PERIOD_MS);
                    mFmeMs = 0;
                    sendMsg(mAudioHandler, MSG_CHECK_FM_ACTIVE, SENDMSG_REPLACE, 0, 0, null,
                            FM_ACTIVE_POLL_PERIOD_MS);
                }
            }
        }
    }
    private void enforceSafeMediaVolume() {
        enforceSafeMediaVolume(AudioSystem.STREAM_MUSIC);
        enforceSafeMediaVolume(AudioSystem.STREAM_FM);
    }
    private void enforceSafeMediaVolume(int streamType) {
        VolumeStreamState streamState = mStreamStates[streamType];
        boolean lastAudible = (streamState.muteCount() != 0);
        int devices = mSafeMediaVolumeDevices;
        int i = 0;
        while (devices != 0) {
            int device = 1 << i++;
            if ((device & devices) == 0) {
                continue;
            }
            int index = streamState.getIndex(device, lastAudible);
            if (index > mSafeMediaVolumeIndex) {
                if (lastAudible) {
                    streamState.setLastAudibleIndex(mSafeMediaVolumeIndex, device);
                    sendMsg(mAudioHandler, MSG_PERSIST_VOLUME, SENDMSG_QUEUE, PERSIST_LAST_AUDIBLE,
                            device, streamState, PERSIST_DELAY);
                } else {
                    streamState.setIndex(mSafeMediaVolumeIndex, device, true);
                    sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, device, 0,
                            streamState, 0);
                }
            }
            devices &= ~device;
        }
    }
    private boolean checkSafeMediaVolume(int streamType, int index, int device) {
        synchronized (mSafeMediaVolumeState) {
            if ((mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE)
                    && (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC || mStreamVolumeAlias[streamType] == AudioSystem.STREAM_FM)
                    && ((device & mSafeMediaVolumeDevices) != 0) && (index > mSafeMediaVolumeIndex)) {
                mVolumePanel.postDisplaySafeVolumeWarning();
                return false;
            }
            return true;
        }
    }
/// safe media for FM @}
(alps/frameworks/base/media/java/android/media/AudioService.java)
然后
mk xxx(project_name) mm frameworks/base
adb remount
adb push framework.jar system/framework
adb reboot
即可验证功能。

标签