Use the AudioManager class. Essentially, the code is as follows:
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); audioManager.setStreamVolume(streamType, volume, flags);
The problem is that the volume of the device is not necessarily displayed from 0 to 10, like your slider. On my emulator, it is from 0 to 7. So, you need to getStreamMaxVolume(...) to know what your maximum is, and then work out your value as part of this. For example, if your user selects a volume of 8 out of 10, which is equivalent to 0.8 * 7 = 5.6, which you should round to 6 out of 7.
โstreamโ refers to things such as ringer volume, notification volume, music volume, etc. If you want to change the ringer volume, you need to make sure that all your commands have AudioManager.STREAM_RING as streamType.
Steve haley
source share