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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:46:06+00:00 2026-06-01T13:46:06+00:00

i have a button that plays a sound. When i push the button multiple

  • 0

i have a button that plays a sound.
When i push the button multiple times, it plays the sound multiple times.
This is oke, i want this.
But when i click the stop button it must stop all sounds currently playing.
I used:

   while (mediaPlayer.isPlaying()){mediaPlayer.stop();}

but it is not working, the sounds keep on playing. Can someone help me?
Here is my full code:

public class HelloSoundboard extends Activity {
MediaPlayer mediaPlayer;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button item1 = (Button)findViewById(R.id.item1);
    item1.setOnClickListener(new View.OnClickListener() {
       public void onClick(View view) {
           mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.atyourservice);
           mediaPlayer.start();
       }
    });

    Button stop = (Button)findViewById(R.id.stop);
    stop.setOnClickListener(new View.OnClickListener() {
       public void onClick(View view) {
          while (mediaPlayer.isPlaying()){mediaPlayer.stop();}
        //  mediaPlayer.stop();
       }
     });
 }
}
  • 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-06-01T13:46:07+00:00Added an answer on June 1, 2026 at 1:46 pm

    SoundPool is a much better alternative for this purpose. I would caution strongly against instantiating multiple MediaPlayer instances as most systems do not have the resources to generate many parallel active instances. You wil find on many device that hitting the button upwards of 5 times will cause a memory based crash.

    As far as stopping all active streams, there is not baked-in function for this, but it’s easy to accomplish in a manner to similar to your existing code. As a side note, there is an autoPause() method, which halts all streams, but it doesn’t truly end their playback (as the method name insinuates). Here is a simple example to manage your audio streams:

    //SoundPool initialization somewhere
    SoundPool pool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
    //Load your sound effect into the pool
    int soundId = pool.load(...); //There are several versions of this, pick which fits your sound
    
    List<Integer> streams = new ArrayList<Integer>();
    Button item1 = (Button)findViewById(R.id.item1);
    item1.setOnClickListener(new View.OnClickListener() {
       public void onClick(View view) {
           int streamId = pool.play(soundId, 1.0f, 1.0f, 1, 0, 1.0f);
           streams.add(streamId);
       }
    });
    
    Button stop = (Button)findViewById(R.id.stop);
    stop.setOnClickListener(new View.OnClickListener() {
       public void onClick(View view) {
          for (Integer stream : streams) {
              pool.stop(stream);
          }
          streams.clear();
       }
    });
    

    It is much more memory efficient to manage a list of streamID values than MediaPlayer instances, and your users will thank you. Also, note that it is safe to call SoundPool.stop() even if the streamID is no longer valid, so you don’t need to check for existing playback.

    HTH

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

Sidebar

Related Questions

I have a website that plays sound when you click a button. This is
I have a button that plays a sound and i want to prevent users
I have a button that plays an audio file on its click listener. If
i have a button i want three functionality for that on first click it
I simply have a sound that executes on a button click. Everything works fine
i have a simple UIButton that, once clicked, plays a 1 second sound. i
I want to create a soundboard, so when you click on button it plays
i want to play sound with click event of my button click event in
I want to create an app that shows text, plays sound (mp3 files) and
I have a button that will control a sound effect. When the button is

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.