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 895729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:36:22+00:00 2026-05-15T14:36:22+00:00

Right now I have two buttons. Each one needs to produce a different sound.

  • 0

Right now I have two buttons. Each one needs to produce a different sound. In the future, there will probably be about 8 buttons, but for now just two.

public class MyActivity extends Activity {
     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            final Button btnDrum1 = (Button) findViewById(R.id.btnDrum1);
            btnDrum1.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                     MediaPlayer mp = MediaPlayer.create(this, R.raw.drum1);
                        mp.start();
                        mp.release();
                }
            });

            final Button btnCym1 = (Button) findViewById(R.id.btnCym1);
            btnCym1.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                     MediaPlayer mp = MediaPlayer.create(this, R.raw.cym1);
                            mp.start();
                            mp.release();
                }
            });

        }
    }

Originally i didn’t have mp.release() and it would play the sound properly, but eventually the app would crash due to running out of memory. Now with the mp.release() it doesn’t crash, but sometimes it doesn’t play the sound when clicked.

Is this the most efficeient way to play a sound when button is clicked? Is it extensible?

  • 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-15T14:36:23+00:00Added an answer on May 15, 2026 at 2:36 pm

    I think this is due to you releasing it while it is playing. Make a global MediaPlayer for each sound and use it over and over again, release when the activity is closed (maybe even when it is paused, and reload on resume if the sound files are big). Also, since you will have many buttons, you could have a single onclicklistener on all buttons that you instantiate on onCreate():

    private class MyMagicalOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch(v.getId()) {
                case R.id.button1:
                //play sound 1
                break;
            case R.id.button2:
                //play sound 2
                break;
            }
        }
    }
    

    just comment and tell me if you are unsure on how to implement this 🙂

    edit: Per request, here is a neat implementation that should work wonders with your app. I made it so the activity implements onclicklistener instead, I think it is a bit more clean.

    public class Bluarg extends Activity implements OnClickListener{
    
        MediaPlayer mp1;
        MediaPlayer mp2;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            mp1 = MediaPlayer.create(this, R.raw.sound1);
            mp2 = MediaPlayer.create(this, R.raw.sound2);
    
            final Button button1 = (Button) findViewById(R.id.button1);
            button1.setOnClickListener(this);
    
            final Button button2 = (Button) findViewById(R.id.button2);
            button1.setOnClickListener(this);
    
        }
    
        @Override
        public void onClick(View v) {
            switch(v.getId()) {
            case R.id.button1:
                mp1.start();
                break;
            case R.id.button2:
                mp2.start();
                break;
            }
        }
    
        @Override
        protected void onDestroy() {
            mp1.release();
            mp2.release();
            super.onDestroy();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hei there, right now I'm trying to connect two seperate Subapps with one view.
I actually have right now two questions: 1) What font faces are preferred for
Right now I have a script that will get the last five files in
I have an MFC dialog in which there are two radio buttons. I have
In my application screen, i have two buttons. one is Activate and another is
I have two buttons in one class and those buttons send you to the
I have an app with two screens, and buttons that switch between them. One
I want to have two elements stay on the same row. Right now I
I have two radio buttons what I want is that when I select one
Good day! I right now have a function the drags an element from a

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.