In my app, I have a custom key pad and want to play tick tone on key press. The below code is giving me the sound.
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
float vol = 1f;
am.playSoundEffect(AudioManager.FX_KEY_CLICK, vol);
But I am looking to play the same tone which comes when user touches dial pad. How do I achieve it?
To enable or disable the keyboard dial tone on an Android phone: Open the Phone app. Tap on the Menu key > Call settings . Tap on "Ringtones and keypad tones ". Enable or disable "Dialing keypad tone ". ...
I found the solution with the help of Michael comment. Posting here as it may help others :)
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int volume_level= am.getStreamVolume(AudioManager.STREAM_RING); // Highest Ring volume level is 7, lowest is 0
final ToneGenerator mToneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, volume_level * 14); // Raising volume to 100% (For eg. 7 * 14 ~ 100)
mToneGenerator.stopTone();
mToneGenerator.startTone(ToneGenerator.TONE_DTMF_1, 100); // play sound for 100ms
Similarly for other keys, choose tone from ToneGenerator.TONE_DTMF_0
to ToneGenerator.TONE_DTMF_9
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