简体   繁体   English

如何在我的自定义键盘上播放用户拨号盘音调?

[英]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? 我该如何实现?

I found the solution with the help of Michael comment. 我在Michael评论的帮助下找到了解决方案。 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 与其他键类似,从ToneGenerator.TONE_DTMF_0ToneGenerator.TONE_DTMF_9选择音调

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM