It is common for many new Android developers to think that volume controls on the device are done on a scale of 0 to 100. Granted, it doesn’t actually say the scale anywhere on the official documentation reference page:
But, a clue is in the documentation for the individual method used to control the volume in general on Android:
setStreamVolume(...). It’s highly recommended to read the documentation for a class and method that you will be using. It’ll typically help to avoid many common pitfalls.
For DTMF volume, in
AudioManager.setStreamVolume(itn streamType, int index, int flags):
- The streamType should be
AudioManager.STREAM_DTMFrather than any of the other
- The index needs to be a number between 0 and
getStreamMaxVolume(int streamType)in order to work properly.
- The flags field is used for setting more properties of the volume, which will be in the form of
AudioManager.FLAG_*to control options like isSilent and isVibrate.
Now to bring it all together, here’s how to control the DTMF tone volume programtically:
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); mSeekBar = findViewById(R.id.dtmfVolumeSeekBar); mSeekBar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_DTMF)); int volume = mSeekBar.getProgress(); // SeekBar with range of 0 to DTMF max volume. audioManager.setStreamVolume(AudioManager.STREAM_DTMF, volume, 0);
That’s it, though you don’t have to use a
SeekBar that was mentioned in the example.
Recently, I’ve answered another Android question on StackOverflow: How to control DTMF volume programmatically on Android.
And, interestingly enough, my Simply DTMF Tone Generator app doesn’t yet include volume controls specifically for DTMF. But, it’s open source for anybody to give it a go… :)