Android stop MediaPlayer

Android Media Player Wont Play After Stop

I have music playlist for 5 songs. I just want that play and stop buttons work as long as im in app. And that i can stop music when i want to and start another.

How this works now...The music plays on PLAY button, and when i click STOP button it stops, but then i want to play some other song, or same song again, nothing happens. Please help.

to play song again reset media player, set data source again and start

mp.reset(); mp.setDataSource(MEDIA_PATH); mp.prepare(); mp.start();

When press play button after stop ,then play button never works--for this problem we can create object again in stop button. for eg;-stop.setOnClickListener(new OnClickListener() {

You can check this Demo Example Here, i am looping a Audio clicp continously and stopping on Button Cick and playing again.

package com.Test.demo; import androidx.appcompat.app.AppCompatActivity; import android.content.DialogInterface; import android.media.MediaPlayer; import android.os.Build; import android.os.Bundle; import android.os.CountDownTimer; import android.util.Log; import android.view.View; import android.widget.Button; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import static java.util.concurrent.TimeUnit.SECONDS; public class MainActivity extends AppCompatActivity { Button scan; Button stop; MediaPlayer mp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); scan=(Button)findViewById(R.id.scan); stop=(Button)findViewById(R.id.stop); mp = MediaPlayer.create(this, R.raw.trackeralertseparation); scan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mp.setLooping(true); mp.start(); mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mediaPlayer) { mp.reset(); mp.release(); } }); } }); stop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(mp.isPlaying()){ mp.stop(); } mp=MediaPlayer.create(getApplicationContext(), R.raw.trackeralertseparation); } }); } }

The Design File is Below with simple Button.

When the play wont work after you hit the stop button... just make your mediaplayer object as an object array that should fix it..

final MediaPlayer[] mediaPlayer = {MediaPlayer.create(getApplicationContext(), R.raw.song_name)}; stop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mediaPlayer[0].stop(); mediaPlayer[0] = MediaPlayer.create(getApplicationContext(),R.raw.song_name); } });

When you stop music, must declare the song again in create.

the easy way using Android Studio: