6

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?

Bhargav Kumar R
  • 2,190
  • 3
  • 22
  • 38

1 Answers1

7

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

Bhargav Kumar R
  • 2,190
  • 3
  • 22
  • 38