stop playing sound when another sound is playing in android studio

Viewed 797

hello I use MediaPlayer for play sound in my project in android studio when I click in a button and play sound , and when I click in another button that button play another sound , I want the first sound or another sound be stoped and just the last button that I clicked be played I try this code but it not worked

package azad.broooska.fartfuns;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;


public class MusicFartActivity extends AppCompatActivity implements View.OnClickListener {
    private CardView cardView1, cardView2, cardView3, cardView4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_fart);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        cardView1 = findViewById(R.id.card_music1);
        cardView2 = findViewById(R.id.card_music2);
        cardView3 = findViewById(R.id.card_music3);
        cardView4 = findViewById(R.id.card_music4);
        cardView1.setOnClickListener(this);
        cardView2.setOnClickListener(this);
        cardView3.setOnClickListener(this);
        cardView4.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        final MediaPlayer s1 = MediaPlayer.create(this, R.raw.bohemian_fartsody);
        final MediaPlayer s2 = MediaPlayer.create(this, R.raw.fart_ballade);
        final MediaPlayer s3 = MediaPlayer.create(this, R.raw.fart_uverture);
        final MediaPlayer s4 = MediaPlayer.create(this, R.raw.farting_xmas);
        if (id == R.id.card_music1) {
            s1.start();
            s2.stop();
            s3.stop();
            s4.stop();
        } else if (id == R.id.card_music2) {
            s2.start();
            s1.stop();
            s3.stop();
            s4.stop();
        } else if (id == R.id.card_music3) {
            s3.start();
            s2.stop();
            s1.stop();
            s4.stop();
        } else {
            Toast.makeText(this, "noThing", Toast.LENGTH_SHORT).show();
        }
    }
}
2 Answers

you are creating new instances of MediaPlayer each time something is clicked and assigning the variables to new objects; make s1,s2... class property;

MediaPlayer s1 ,s2,s3,s4;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_music_fart);
    //some code
    s1 = MediaPlayer.create(this, R.raw.bohemian_fartsody);
    //assign the rest;
}
public void onClick(View view) {
    int id = view.getId();
    if (id == R.id.card_music1) {
        s1.start();
        s2.stop();
        s3.stop();
        s4.stop();
    }
    //rest of the code
}

Well, its seems like you don`t want to play very long sounds because you have names like "fart_ballade" and "fart_uverture" in your code.

If your sounds are not very long it is better to work with the SoundPool class.

The MediaPlayer class sometimes can cause problems if you switch fast between sounds.

So if your sounds are not that long, you can try the SoundPool class.

Here you can learn more about the SoundPool class:

How to use SoundPool

I won`t write any code because SoundPool needs more than 5-6 lines of code to work properly.

Related