Android Question Audio Safe Volume

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I have my 10" (yes really it is 10") Android connected FULL Time to my in house stereo and running my own home grown music player. I need the volume to be high because Zone 2 on stereo has no way of increasing the volume.

I keep getting the darn Samsung message about listening to music at high volumes...

Is there ANY I can do the below code in my Music player software?


I came across this article: https://www.xda-developers.com/how-to-automatically-disable-the-high-volume-warning-without-root/

Which pointed me to this area: https://android.googlesource.com/pl...28/media/java/android/media/AudioService.java


JUST a part of the code below

B4X:
/==========================================================================================
// Safe media volume management.
// MUSIC stream volume level is limited when headphones are connected according to safety
// regulation. When the user attempts to raise the volume above the limit, a warning is
// displayed and the user has to acknowlegde before the volume is actually changed.
// The volume index corresponding to the limit is stored in config_safe_media_volume_index
// property. Platforms with a different limit must set this property accordingly in their
// overlay.
//==========================================================================================

// mSafeMediaVolumeState indicates whether the media volume is limited over headphones.
// It is SAFE_MEDIA_VOLUME_NOT_CONFIGURED at boot time until a network service is connected
// or the configure time is elapsed. It is then set to SAFE_MEDIA_VOLUME_ACTIVE or
// SAFE_MEDIA_VOLUME_DISABLED according to country option. If not SAFE_MEDIA_VOLUME_DISABLED, it
// can be set to SAFE_MEDIA_VOLUME_INACTIVE by calling AudioService.disableSafeMediaVolume()
// (when user opts out).
private final int SAFE_MEDIA_VOLUME_NOT_CONFIGURED = 0;
private final int SAFE_MEDIA_VOLUME_DISABLED = 1;
private final int SAFE_MEDIA_VOLUME_INACTIVE = 2;
private final int SAFE_MEDIA_VOLUME_ACTIVE = 3;
private Integer mSafeMediaVolumeState;

private int mMcc = 0;
// mSafeMediaVolumeIndex is the cached value of config_safe_media_volume_index property
private int mSafeMediaVolumeIndex;
// 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;
// mMusicActiveMs is the cumulative time of music activity since safe volume was disabled.
// When this time reaches UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX, the safe media volume is re-enabled
// automatically. mMusicActiveMs is rounded to a multiple of MUSIC_ACTIVE_POLL_PERIOD_MS.

private int mMusicActiveMs;
private static final int UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX = (20 * 3600 * 1000); // 20 hours
private static final int MUSIC_ACTIVE_POLL_PERIOD_MS = 60000; // 1 minute polling interval
private static final int SAFE_VOLUME_CONFIGURE_TIMEOUT_MS = 30000; // 30s after boot completed


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);
}
}
}
}

B4X:
mSafeMediaVolumeState = new Integer(Settings.Global.getInt(mContentResolver,
Settings.Global.AUDIO_SAFE_VOLUME_STATE,
SAFE_MEDIA_VOLUME_NOT_CONFIGURED));
// The default safe volume index read here will be replaced by the actual value when
// the mcc is read by onConfigureSafeVolume()


Thanks

BobVal
 

DonManfred

Expert
Licensed User
Longtime User
But couldn't I give my app the special permissions the same way
you mean using
B4X:
adb shell pm grant com.joaomgcd.autotools android.permission.WRITE_SECURE_SETTINGS
? Probably. You need to try
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I'm sorry Don. Yes I understand that. But I need to be able to run the java code to do the setting first. Then I can run adb on my app then from that forward when my app would start it would do the fix. There tools app fixes it for everything. My app would just fix when running.

But if I cant convert the java code to run on b4a then I'm stuck using there app to fox for always instead of needed

Can you help converting the code to run b4a I believe I would be able to take from there
 
Upvote 0
Top