0


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 :)

  • Try to add some LogCat output from the crash. This is vital for finding out what causes the crash. – Codemonkey Mar 16 '11 at 08:57
  • Thank you for your input. I'm quite new to programming, how do i add that? :) – Simon Rosenqvist Mar 16 '11 at 11:45
  • Ahh, LogCat is so so so important! It will list the exceptions you are getting that led to your crash. http://stackoverflow.com/questions/3280051/how-to-enable-logcat-in-eclipse covers enabling LogCat. Then you can check the LogCat window when you crash - it even allows you to filter by E (Exceptions or Errors, not sure). Someone can probably figure out this issue without LogCat output, but you will want it set up for later no matter what. http://developer.android.com/reference/android/util/Log.html is worth checking out too. – Codemonkey Mar 16 '11 at 11:49
  • http://gratisupload.dk/billede/thumb/57600/full/ – Simon Rosenqvist Mar 17 '11 at 11:15
  • The link above shows the logcat. any help would be much appreciated :) – Simon Rosenqvist Mar 17 '11 at 11:16
  • Sorry to be a pain in the ass, but you gotta show the entire output. The initial output only shows the exceptions that occurred inside the Android-specific classes, so it won't tell you what the problem is with YOUR code. It's very important to distinguish and find the part of the LogCat output that references some of your code (look for your package names) – Codemonkey Mar 17 '11 at 11:23

1 Answers1

0

(I'm brand-new to Android development, so thought this interesting to figure out.)

I found this about calling release(): "Once the MediaPlayer object is in the End state, it can no longer be used and there is no way to bring it back to any other state." Hence the illegal state exception (which I was able to reproduce).

The right combination instead seems to be to call stop() and then prepare():

public void onCompletion(MediaPlayer mp) {
    mp.stop();
    try
    {
        mp.prepare();
    }
    catch( IOException ie )
    {
        throw new RuntimeException( ie ) ;
    }
}
Rodney Gitzel
  • 2,652
  • 16
  • 23