Android - 获取最大安全流量

Android - Get max safe stream volume

我有一个以编程方式更改流体积的用例,但是在较新的 android 体积上,将体积提高到一定限度以上(根据我的观察,60% 对应于大多数 [=26 上的步骤 9 =]s) 导致警告对话框:

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 步作为最大音量然后,发现它不是另一个 phone 的正确值。 android API 是否在任何地方公开最大安全流音量?如果不是,那么他们是否至少记录了不同 phone 对应的步骤编号?

谢谢!

有一个资源包含安全卷步骤: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。