0

In my Custom IME I want to play a BEEP sound on the key when user presses that.

I found how-do-i-access-androids-default-beep-sound , and in my SoftKeyboard.java class I added that code in :

public void onKey(int primaryCode, int[] keyCodes) {
    // play sound on keypress
    try {
        MediaPlayer mMediaPlayer = MediaPlayer.create(context, R.raw.beep);
        mMediaPlayer.prepare();
        mMediaPlayer.start();
    } catch (Exception e) {e.printStackTrace();}
}

BUT that does not work, any ideas why?

Community
  • 1
  • 1
Arshad Ali
  • 3,082
  • 12
  • 56
  • 99

1 Answers1

0

I was calling mMediaPlayer.prepare() unnecessarily, as it's not needed when creating MediaPlayer with it's create() method. This is the solution which worked for me

public void onKey(int primaryCode, int[] keyCodes) {
    // play sound on keypress
    try {
        MediaPlayer mMediaPlayer = MediaPlayer.create(context, R.raw.beep);

        mMediaPlayer.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Melquiades
  • 8,496
  • 1
  • 31
  • 46
Arshad Ali
  • 3,082
  • 12
  • 56
  • 99