Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 667077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:53:20+00:00 2026-05-13T23:53:20+00:00

I have a button that plays an audio file on its click listener. If

  • 0

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;

                   } 
              }

          ); 



        }
    };
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:53:20+00:00Added an answer on May 13, 2026 at 11:53 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a short audio clip that plays when a button is pressed. I
I have an activity that implements MediaController.MediaPlayerControl . The audio player plays the file
i have a button that plays a sound. When i push the button multiple
I have a button that when clicked will run a stored procedure on a
I have a button that opens a dialog when clicked. The dialog displays a
i have a simple UIButton that, once clicked, plays a 1 second sound. i
I have a button that plays a sound and i want to prevent users
I have made a simple play/pause button that works in conjunction with the audio
I am trying to play an audio file when I click the button, but
I have an audio file that I want to begin playing at a 20

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.