Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play user dial pad tone on my custom keypad press in android?

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?

like image 685
Bhargav Kumar R Avatar asked Mar 15 '16 11:03

Bhargav Kumar R


People also ask

How do I turn on the dialpad Sound on my Android?

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 ". ...


1 Answers

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

like image 150
Bhargav Kumar R Avatar answered Sep 28 '22 11:09

Bhargav Kumar R