简体   繁体   中英

Sound stops when button is pressed again

I m trying to make a android soundboard.When i touch any button it produces sound but when i touch it again, not only sound stops but none of the other button works.I want it play one sound at a time.Here is my main activity im calling the play functions on buttons.

public class MainActivity extends AppCompatActivity {

    MediaPlayer whine, cry, weed, chup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        whine = MediaPlayer.create(this, R.raw.gone);
        cry = MediaPlayer.create(this, R.raw.mock);
        weed = MediaPlayer.create(this, R.raw.phen);
        chup = MediaPlayer.create(this, R.raw.rg);
    }
    public void playwhine(View view) {
        if (cry.isPlaying())
            cry.stop();
        if (weed.isPlaying())
            weed.stop();
        if (chup.isPlaying())
            chup.stop();
        whine.start();
    }

    public void playcry(View view) {
        if (whine.isPlaying())
            whine.stop();
        if (weed.isPlaying())
            weed.stop();
        if (chup.isPlaying())
            chup.stop();
        cry.start();
    }

    public void playweed(View view) {
        if (cry.isPlaying())
            cry.stop();
        if (whine.isPlaying())
            whine.stop();
        if (chup.isPlaying())
            chup.stop();
        weed.start();
    }

    public void playchup(View view) {
        if (cry.isPlaying())
            cry.stop();
        if (whine.isPlaying())
            whine.stop();
        if (weed.isPlaying())
            weed.stop();
        chup.start();
    }
}

This sample is taken from your last method:

public void playchup(View view) {
    if (cry.isPlaying())
        cry.stop();
    if (whine.isPlaying())
        whine.stop();
    if (weed.isPlaying())
        weed.stop();
    chup.start();
}

If you look at the last three if-statements you can see that you stop the sound. If any sound is playing it is stopped when the button is pressed.

Let me further explain:

  1. Button is pressed
  2. Call is made to one of the four methods(play())
  3. You stop the other sounds
  4. And play the sound for the related method

Possible issue

In theory, when the MediaPLayer is played through, you have to recreate it. See this question to see how you can loop the sounds and not have to recreate the mediaplayer every single time.

Meaning your code should look like this:

whine = MediaPlayer.create(this, R.raw.gone);
cry = MediaPlayer.create(this, R.raw.mock);
weed = MediaPlayer.create(this, R.raw.phen);
chup = MediaPlayer.create(this, R.raw.rg);

whine.setLooping(true);
cry.setLooping(true);
weed.setLooping(true);
chup.setLooping(true);

And if you don't want to stop every other sound when one button is pressed you have to remove the if-statements that check if other sounds are playing and stopping them.

Alternative to MediaPlayer

If you are dealing with several sounds you can use a SoundPool as it is designed to handle several and overlapping sounds at once.

Because You are creating MusicPlayer objects but not releasing them

Declare a Interface and override setOnCompletion to call release() method.

Example

 private class musicCompletionListener implements MediaPlayer.OnCompletionListener {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            mediaPlayer.release();
        }
    }

and Then setOnCompletion listener for each mediaPlayer objects.

public void playwhine(View view) {
    if (cry.isPlaying())
        cry.stop();
    if (weed.isPlaying())
        weed.stop();
    if (chup.isPlaying())
        chup.stop();
whine.setOnCompletionListener(new musicCompletionListener());
    whine.start();
}

public void playcry(View view) {
    if (whine.isPlaying())
        whine.stop();
    if (weed.isPlaying())
        weed.stop();
    if (chup.isPlaying())
        chup.stop();
cry.setOnCompletionListener(new musicCompletionListener());
    cry.start();
}

public void playweed(View view) {
    if (cry.isPlaying())
        cry.stop();
    if (whine.isPlaying())
        whine.stop();
    if (chup.isPlaying())
        chup.stop();
weed.setOnCompletionListener(new musicCompletionListener());
    weed.start();
}

public void playchup(View view) {
    if (cry.isPlaying())
        cry.stop();
    if (whine.isPlaying())
        whine.stop();
    if (weed.isPlaying())
        weed.stop();
chup.setOnCompletionListener(new musicCompletionListener());
    chup.start();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM