Android MediaPlayer run with overlapping Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Android MediaPlayer run with overlapping without wasting too much if your time.

The question is published on by Tutorial Guruji team.

thanks for reading this post, I have a problem with one of the app features that I developed, which is MediaPlayer which I run through the raw folder. The problem arises when I try to play 2 MediaPlayer simultaneously at the same time, another MediaPlayer should stop automatically when the other MediaPlayer runs, but this does not, even when exiting the activity, MediaPlayer is still running, is there a solution related to this. this is my code

    MediaPlayer mediaPlayer1,mediaPlayer2,mediaPlayer3;
    ImageButton btn1, btn2, btn3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hukum_ra_tarqiq);

        btn1 = findViewById(R.id.play_toggle_ratarqiq1);
        btn1.setOnClickListener(this);
        mediaPlayer1 = MediaPlayer.create(this,R.raw.madtarqiqc1);
        btn2 = findViewById(R.id.play_toggle_ratarqiq2);
        btn2.setOnClickListener(this);
        mediaPlayer2 = MediaPlayer.create(this,R.raw.madtarqiqc2);
        btn3 = findViewById(R.id.play_toggle_ratarqiq3);
        btn3.setOnClickListener(this);
        mediaPlayer3 = MediaPlayer.create(this,R.raw.madtarqiqc3);

    }
@Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.play_toggle_ratarqiq1:
                setmpFirst();
                break;
            case R.id.play_toggle_ratarqiq2:
                setmpSecond();
                break;
            case R.id.play_toggle_ratarqiq3:
                setmpThird();
                break;
        }
    }
private void setmpFirst(){
        if (mediaPlayer1.isPlaying()){
            if (mediaPlayer1 != null){
                mediaPlayer1.pause();
                if (isFinishing()){
                    mediaPlayer1.stop();
                    mediaPlayer1.release();
                }
            }
            btn1.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
        }else {
            mediaPlayer1.start();
            btn1.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
        }
    }

    private void setmpSecond(){
        if (mediaPlayer2.isPlaying()){
            if (mediaPlayer2 != null){
                mediaPlayer2.pause();
                if (isFinishing()){
                    mediaPlayer2.stop();
                    mediaPlayer2.release();
                }
            }
            btn2.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
        }
        else {
            mediaPlayer2.start();
            btn2.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
        }
    }

    private void setmpThird(){
        if (mediaPlayer3.isPlaying()){
            if (mediaPlayer3 != null){
                mediaPlayer3.pause();
                if (isFinishing()){
                    mediaPlayer3.stop();
                    mediaPlayer3.release();
                }
            }
            btn3.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
        }
        else {
            mediaPlayer3.start();
            btn3.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
        }
    }

What I want is, when MediaPlayer above is run simultaneously, MediaPlayer is first clicked to run, and the others stop, as well as when I exit the activity, MediaPlayer automatically stops, is there a solution or from the code above is there a simplification?

Answer

make your play List. using array.

int[] playList = new int[3];
playList[0] = R.raw.madtarqiqc1;
playList[1] = R.raw.madtarqiqc2;
playList[2] = R.raw.madtarqiqc3;

and set play position in Global Variable

int play = 0;

and you can controll using mediaPlayer.setOnCompletionListener()

for example,

MediaPlayer mediaPlayer = MediaPlayer.create(this, playList[play]);
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(completionListener);
MediaPlayer.OnCompletionListener completionListener = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            play++;
            if (play < playList.length) {
                mediaPlayer = MediaPlayer.create(BackGroundSound.this, playList[play]);
                mediaPlayer.start();
            } else {
                play = 0;
                mediaPlayer = MediaPlayer.create(BackGroundSound.this, playList[play]);
                mediaPlayer.start();
            }
        }
    };

and when you make next button (or click event). just

mediaPlayer.stop(); 
mediaPlayer = MediaPlayer.create(this, playList[position]);
mediaPlayer.start();

p.s] i modify your code. check this.

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    MediaPlayer mediaPlayer;
    ImageButton btn1, btn2, btn3;
    int[] playList;
    int play = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn1 = (ImageButton) findViewById(R.id.play_toggle_ratarqiq1);
        btn2 = (ImageButton) findViewById(R.id.play_toggle_ratarqiq2);
        btn3 = (ImageButton) findViewById(R.id.play_toggle_ratarqiq3);

        int[] playList = new int[3];
        playList[0] = R.raw.madtarqiqc1;
        playList[1] = R.raw.madtarqiqc2;
        playList[2] = R.raw.madtarqiqc3;

        mediaPlayer = MediaPlayer.create(this, playList[play]);
        mediaPlayer.setOnCompletionListener(completionListener);

    }

    MediaPlayer.OnCompletionListener completionListener = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            play++;
            if (play < playList.length) {
                mediaPlayer = MediaPlayer.create(MainActivity.this, playList[play]);
                mediaPlayer.start();
            } else {
                play = 0;
                mediaPlayer = MediaPlayer.create(MainActivity.this, playList[play]);
                mediaPlayer.start();
            }
        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.play_toggle_ratarqiq1:
                playMusic(0);
                break;
            case R.id.play_toggle_ratarqiq2:
                playMusic(1);
                break;
            case R.id.play_toggle_ratarqiq3:
                playMusic(2);
                break;
        }
    }

    private void playMusic(int position) {
        mediaPlayer.stop();
        mediaPlayer = MediaPlayer.create(this, playList[position]);
        mediaPlayer.start();

        changeView(position);
    }

    private void changeView(int position) {
        if (position == 0) {
            btn1.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
            btn2.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
            btn3.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
        } else if (position == 1) {
            btn1.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
            btn2.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
            btn3.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
        } else if (position == 2) {
            btn1.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
            btn2.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
            btn3.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
        }
    }

}
We are here to answer your question about Android MediaPlayer run with overlapping - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji