Android: How to handle runtime exception on playing audio files?

Posted by Maxood on Stack Overflow See other posts from Stack Overflow or by Maxood
Published on 2010-03-15T08:32:10Z Indexed on 2010/03/15 8:39 UTC
Read the original article Hit count: 511

Filed under:

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;

                   } 
              }

          ); 



        }
    };

© Stack Overflow or respective owner

Related posts about mediaplayer