about the params Set what to make the player no sound and full sound
I have tried Android
I think we should use the function below
This function is actualy wonderful. Thanks to it you can create a volume scale with any number of steps!
Let's assume you want 50 steps:
Then to set setVolume to any value in this range (0-49) you do this:
Nice and easy! And DON'T use AudioManager to set volume! It will cause many side effects such as disabling silent mode, which will make your users mad!
Following user100858 solution I just post my exact code that works:
soundVolume is the volume you would like to set, between 0 and MAX_VOLUME. So between 0 and 100 in this example.
Why making it so complicated? I am using this simple formula:
and setting this value in media player, like:
The other answers here are not correct--or at least, they're not configured properly.
Perform the following test, using their code (e.g. that of Tomasz or ssuukk):
1) Set 100 as the "max volume"/number of steps, and submit the volume 50.
It returns: 0.150514997831991
2) Set 1000 as the "max volume"/number of steps, and submit the volume 500.
What does it return? The same value, 0.150514997831991, right?
Nope. Instead, it's: 0.100343331887994
In other words, the existing answers change how they scale the input volume-percent (i.e. the transformation curve) based on how many volume-steps you set.
I've spent the last few hours looking into this issue; enough that I don't feel like going into too much detail explaining the issue. Instead I'll just post the large code/comment block in my program concerning it. (it's in C#, for Xamarin Android, but the functionality should be the same for Java)
The documentation is sparse, so I can't know for sure if I have the right scaling-system/type-of-unit the SetVolume method expects.
Assuming it expects an Amplitude value, the code above may be the correct volume setting code for it. (taking desired Loudness, linear, as an input, and outputting/setting the Amplitude value needed for the built-in SetVolume method)
I'm not sure it's correct, though, and am too tired to confirm. If anyone has further thoughts, feel free to add them. (3+ hours is enough to spend on an issue like this, in one day)
After listening carefully, and comparing the loudness-fade effect by:
I find that option 1 seems to be closer to a linear loudness fade-in! In other words... from actually listening and comparing the basic approach, with the various transformation approaches shown here, it seems the documentation is wrong and the SetVolume method does in fact just expect the loudness value on a linear scale. (perhaps they've updated it to work more intuitively in one of the recent API versions, but haven't updated the docs?)
If so, that sure makes it easy. That's what I'm going with for now. (though I'll keep the exponentiation/scale-fixing approach as a program setting, I suppose, just to have an excuse to keep some result of all that time invested!)
The recommended answer is wrong, as stated Venryx. Log math doesn't work that way (you have to subtract, not divide logs to make them work how you want).
No matter, it looks like Android Volume setting is now proportionate to Loudness linearly... so 0.5 is 50% as loud as 1.0, and 0.1 is 10%, etc. No need for complicated Log math to convert decibels to loudness. Just set it linearly as is intuitive to most people.