Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play default soft keyboard sound when buttons are pressed in android

Tags:

android

I have developed an app which uses my own custom keyboard (well, a view that looks like a keyboard and behaves like a keyboard anyway). One thing I've yet to figure it out is how to make it play the default soft keyboard 'click' sound when the buttons are pressed. Is there any easy way to do this?

I would like to use the keyboard click sound that comes with the phone rather than providing my own. As different phones might have different keyboard click sounds, I would like to keep my application consistent. Ultimately, I want to reflect the same settings the user has chosen in their global keyboard settings (play/not play sounds, vibrate/not vibrate, etc).

like image 539
Coins Avatar asked Jun 24 '11 12:06

Coins


People also ask

Why is my phone keyboard making sounds when I type?

You might have increased the key clicks sound in the keyboard settings of your Android phone. Navigate to the sound settings for your keyboard as shown above and decrease the volume.


2 Answers

I have found a solution to this. All I needed to do was implement a OnTouchListener on the button and use the AudioManager.playSoundEffect() public method. Code is shown below:

AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
float vol = 0.5; //This will be half of the default system sound
am.playSoundEffect(AudioManager.FX_KEY_CLICK, vol);
like image 175
Coins Avatar answered Oct 12 '22 13:10

Coins


if (isSetVibration) {
        if (Build.VERSION.SDK_INT >= 26) {
            ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(50, VibrationEffect.DEFAULT_AMPLITUDE));
        } else {
            ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(50);
        }
    } else {
        if (Build.VERSION.SDK_INT >= 26) {
            ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(0, VibrationEffect.DEFAULT_AMPLITUDE));
        } else {
            ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(0);
        }
    }
like image 27
DV Infosys Avatar answered Oct 12 '22 15:10

DV Infosys