【问题标题】:Android - Get max safe stream volumeAndroid - 获得最大安全流音量
【发布时间】:2019-10-09 04:58:10
【问题描述】:

我有一个以编程方式更改流音量的用例,但在较新的 android 音量上,将音量提高到某个限制以上(根据我的观察,60% 对应于大多数手机上的第 9 步)会导致警告对话框:

Listening at high volume for a long time may damage your hearing. Tap OK to allow the volume 
to be increased above safe levels
Cancel OK

我在 android 开发者门户中找不到任何关于此的文档,我只能找到一些引用欧洲法规的随机文章,例如 one:

According to regulations set by the European Committee for Electrotechnical Standarisation (CENELEC), all electronic devices capable of media playback sold after February 2013 must have a default output volume level of a maximum 85 dB. Users can choose to override the warning to increase the volume to a maximum of 100 dB, but in doing so the warning must re-appear after 20 hours of music playback.

所以我需要可靠地确定这个数字是多少,所以我不会导致显示此对话框的音量更改,但我也不想只使用第 9 步作为最大音量,并且,然后发现它不适合另一部手机。 android API 是否在任何地方公开最大安全流音量?如果不是,那么他们是否至少记录了不同手机对应的步骤号?

谢谢!

【问题讨论】:

    标签: android android-audiomanager


    【解决方案1】:

    有一个资源包含安全卷步骤:config_safe_media_volume_index

    // .../overlay/frameworks/base/core/res/res/values/config.xml
    <integer name="config_safe_media_volume_index">7</integer>
    

    定义为HERE

    并且使用HERE

    你可以通过以下方式获得它:

    int safeVolumeStep;
    int safeVolumeStepResourceId = 
           getResources().getIdentifier("config_safe_media_volume_index", "integer", "android");
    
    if(safeVolumeStepResourceId != 0) {
        safeVolumeStep = getResources().getInteger(safeVolumeStepResourceId);
    } else {
        Log.w("TESTS", "Resource config_safe_media_volume_index not found. Setting a hardcoded value");
        // We probably won't fall here because config_safe_media_volume_index is defined in the AOSP
        // It not a vendor specific resource...
        // For any case, try to set the safe step manually to 60% of the max volume.
        AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        safeVolumeStep = (int) (maxVolume * 0.6f);
    }
    
    Log.d("TESTS", "Safe Volume Step: " + safeVolumeStep +
                   " Safe volume step resourceID: "  + Integer.toHexString(safeVolumeStepResourceId) );
    

    我在 Galaxy S10 上进行了测试,结果为 9。

    【讨论】:

    • 完美运行!我注意到的一件事是,如果您想要最大安全索引,则需要减少检索到的值。对于 GalaxyS9 上的我来说,safeVolumeStep 是 10,如果我设置为该级别,我会收到警告,当我设置为 9 时,我不会收到警告,这与文档相匹配:安全耳机音量指数。当音乐流音量低于此指数时,耳机输出的 SPL 符合 EN 60950 对便携式音乐播放器的要求。
    • 我还应该注意,虽然这适用于三星设备,但在搭载 Android 9 的 Pixel3 上,我得到的 safeVolumeStep 为 3,因为它有 25 个音量步骤,所以它不可能准确。跨度>
    猜你喜欢
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    相关资源
    最近更新 更多