In order for the user to control the volume , my android application has a menu consisting of a slider that provides int values from 0 to 10 , when dragged. After I obtain a value , I must set the volume to the corresponding value chosen by the user , and well , this is the part that I don't know to implement and I 'd like to find about it.
Use the AudioManager class. Essentially the code goes as follows:
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(streamType, volume, flags);
The problem is that the volume of the device isn't necessarily mapped from 0 to 10 as you have in your slider. On my emulator, it's from 0 to 7. So what you need to do is getStreamMaxVolume(...)
to know what your max is, and then work out your value as a fraction of that. As an example, if your user chooses volume 8 out of 10, that's equivalent to 0.8 * 7 = 5.6, which you should round to 6 out of 7.
The "stream" refers to things like ringer volume, notification volume, music volume, etc. If you want to change the volume of the ringer, you need to make sure all your commands have AudioManager.STREAM_RING
as the streamType.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With