i want my application to play a sound whenever the user makes an input the EditText field. Hopefully this will create the illusion that the keyboard makes the sound whenever a key is pressed.
My code for this specific event lookes like this:
package com.textField.android;
public class textField extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText edittext = (EditText) findViewById(R.id.edittext);
final MediaPlayer mp = MediaPlayer.create(getBaseContext(),
R.raw.pistol);
edittext.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
}); } }
I can install and open the application just fine, and i can enter ONE letter and it plays the sound, but when pressing a second letter the application crashes. I don't know what's wrong. Rotating my device plays the sound aswell no matter how many times i do it. So when rotating back it plays the sound again. But then pressing a key the application crashes WITHOUT playing the sound.
Can someone help me get this right? That would be very much appreciated :)