views:

65

answers:

1

I have a button that plays an audio file on its click listener. If the button is clicked again and again while the audio file is being played then the app crashes. What's the solution?

Here is some code for reference:

 private OnClickListener btnMercyListener = new OnClickListener()
    {

        public void onClick(View v)
        {                        
           // Toast.makeText(getBaseContext(), 
             //       "Mercy audio file is being played", 
               //       Toast.LENGTH_LONG).show();

            if (status==true)
            {
                mp.stop();
                mp.release();
                status = false;

            } 
            else
            {
            mp = MediaPlayer.create(iMEvil.this,R.raw.mercy); 


          //mp.start();

            try{
                mp.start();
                status= true; 
                //mp.release();
               }catch(NullPointerException e)
               {
                   Log.v("MP error",e.toString());
               }

            }

            mp.setOnCompletionListener(new OnCompletionListener(){ 

                   // @Override 
                   public void onCompletion(MediaPlayer arg0) { 
                      mp.release();
                      status = false;

                   } 
              }

          ); 



        }
    };
A: 

Two things:
1. Debug the crash and see where it's failing (which line).
2. Surround the whole statement with a try/catch and simply catch an Exception.

If you have an exception or a better idea where your code is failing, then it will be much easier to give you advice on how to fix it... as a matter of fact, you might not even need advice to fix it, you might end up solving the problem by yourself and then you will reap the fruits of your own success.

Update per comments:
The documentation for MediaPlayer indicates what might be the problem given the symptoms the OP is seeing:

To stop playback, call stop(). If you wish to later replay the media, then you must reset() and prepare() the MediaPlayer object before calling start() again. (create() calls prepare() the first time.)

It looks like if the play button is pressed too many times, then the media may end up not being in the prepared state and thus throw some exception. The idea of disabling the play button is valid and it should take care of this situation.

Here is some illustrative code on what you want your program to do:

private OnClickListener btnMercyListener = new OnClickListener()
{
    public void onClick(View v)
    {
        if(isPressed)
        {
            return;
        }

        isPressed = true;

        // create your media player
        mp = MediaPlayer.create(iMEvil.this,R.raw.mercy); 

        // set your listener
        mp.setOnCompletionListener(mp.setOnCompletionListener(new OnCompletionListener(){ 

            // @Override 
            public void onCompletion(MediaPlayer arg0) {
                    if(!isPressed)
                    {
                        return;
                    }

                    isPressed = false;

                    // re-enable your play button
                    playButton.enable();

                    // disable the pause button
                    pauseButton.disable();

                    mp.release();
                    mp.prepare();
                } 
            }
        );

        // disable the play button
        playButton.disable();

        // enable the pause button
        pauseButton.enable();

        // start playback
        mp.start();
    }
};

Of course you should have the appropriate try/catch statements in there so your app doesn't crash, but this code should give you a general idea of what to do.

Lirik
I appreciate what you have advised here. I have worked out the required code.But now i want my button to be disabled while sound is being played.And once the audio file ends, i want my button to be enabled. Any clue about that?
Maxood