I am making an app in Android Studio using MediaPlayer to use my audio clips, when I press a button, the audio plays correctly, when I press another one, the previous one stops and the new one starts, but if I press repeatedly the same button it starts to overlap, and when I hit the "back" button, only one of the audios stops while the others are still playing.
How could I do to avoid that, when pressing a button several times, it overlaps, and instead it "restarts" without overlapping.
Thank you!
Here´s my Java Code
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.denzcoskun.imageslider.ImageSlider;
import com.denzcoskun.imageslider.models.SlideModel;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class Infomezquita extends AppCompatActivity {
private MediaPlayer AZASOUND;
private MediaPlayer AZASOUNDUK;
private MediaPlayer AZARAB;
private MediaPlayer AZAFRENCH;
private MediaPlayer AZAGER;
private MediaPlayer AZAIT;
final Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mezquita);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Mosque");
@Override
public void onBackPressed() {
if (AZASOUNDUK != null && AZASOUNDUK.isPlaying()) {
AZASOUNDUK.stop(); // or mp.pause();
AZASOUNDUK.release();
}
if(AZASOUND != null && AZASOUND.isPlaying()) {
AZASOUND.stop();
//AZASOUND.release();
}
if(AZARAB != null && AZARAB.isPlaying()) {
AZARAB.stop();
//AZASOUND.release();
}
if(AZAFRENCH != null && AZAFRENCH.isPlaying()) {
AZAFRENCH.stop();
//AZASOUND.release();
}
if(AZAGER != null && AZAGER.isPlaying()) {
AZAGER.stop();
//AZASOUND.release();
}
if(AZAIT != null && AZAIT.isPlaying()) {
AZAIT.stop();
//AZASOUND.release();
}
else {
finish();
}
super.onBackPressed();
}
@Override
protected void onStop() {
super.onStop();
if (handler != null)
handler.removeCallbacks(null);
}
public void SONIDO (View view)
{
AZASOUND = MediaPlayer.create(this, R.raw.mezquitaes);
AZASOUND.start();
if(AZASOUNDUK != null && AZASOUNDUK.isPlaying())
{
//AZASOUNDUK.pause();
//AZASOUNDUK.pause();
AZASOUNDUK.stop();
}
if(AZAFRENCH != null && AZAFRENCH.isPlaying())
{
//AZASOUNDUK.pause();
AZAFRENCH.stop();
}
if(AZAGER != null && AZAGER.isPlaying())
{
//AZASOUNDUK.pause();
AZAGER.stop();
}
if(AZAIT != null && AZAIT.isPlaying())
{
//AZASOUNDUK.pause();
AZAIT.stop();
}
else if(AZARAB != null && AZARAB.isPlaying())
{
//AZASOUNDUK.pause();
AZARAB.stop();
}
super.onStop();
}
public void SONIDOUK (View view) {
AZASOUNDUK = MediaPlayer.create(this, R.raw.mezquitauk);
AZASOUNDUK.start();
if(AZASOUND != null && AZASOUND.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUND.stop();
}
if(AZAFRENCH != null && AZAFRENCH.isPlaying())
{
//AZASOUNDUK.pause();
AZAFRENCH.stop();
}
if(AZAGER != null && AZAGER.isPlaying())
{
//AZASOUNDUK.pause();
AZAGER.stop();
}
if(AZAIT != null && AZAIT.isPlaying())
{
//AZASOUNDUK.pause();
AZAIT.stop();
}
else if(AZARAB != null && AZARAB.isPlaying())
{
//AZASOUNDUK.pause();
AZARAB.stop();
}
super.onPause();
}
public void SONIDOARAB (View view) {
AZARAB = MediaPlayer.create(this, R.raw.mezquitaarab);
AZARAB.start();
Toast.makeText(Infomezquita.this, "ارفع الصوت", Toast.LENGTH_SHORT).show(); //OK
if(AZASOUND != null && AZASOUND.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUND.stop();
}
if(AZASOUNDUK != null && AZASOUNDUK.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUNDUK.stop();
}
if(AZAGER != null && AZAGER.isPlaying())
{
//AZASOUNDUK.pause();
AZAGER.stop();
}
if(AZAIT != null && AZAIT.isPlaying())
{
//AZASOUNDUK.pause();
AZAIT.stop();
}
else if(AZAFRENCH != null && AZAFRENCH.isPlaying())
{
//AZASOUNDUK.pause();
AZAFRENCH.stop();
}
super.onPause();
}
public void SONIDOGER (View view) {
AZAGER = MediaPlayer.create(this, R.raw.mezquitager);
AZAGER.start();
if(AZASOUND != null && AZASOUND.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUND.stop();
}
if(AZASOUNDUK != null && AZASOUNDUK.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUNDUK.stop();
}
if(AZAFRENCH != null && AZAFRENCH.isPlaying())
{
//AZASOUNDUK.pause();
AZAFRENCH.stop();
}
if(AZAIT != null && AZAIT.isPlaying())
{
//AZASOUNDUK.pause();
AZAIT.stop();
}
else if(AZARAB != null && AZARAB.isPlaying())
{
//AZASOUNDUK.pause();
AZARAB.stop();
}
super.onPause();
}
public void SONIDOFRENCH (View view) {
AZAFRENCH = MediaPlayer.create(this, R.raw.mezquitafr);
AZAFRENCH.start();
if(AZASOUNDUK != null && AZASOUNDUK.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUNDUK.stop();
}
if(AZASOUND != null && AZASOUND.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUND.stop();
}
if(AZAGER != null && AZAGER.isPlaying())
{
//AZASOUNDUK.pause();
AZAGER.stop();
}
if(AZAIT != null && AZAIT.isPlaying())
{
//AZASOUNDUK.pause();
AZAIT.stop();
}
else if(AZARAB != null && AZARAB.isPlaying())
{
//AZASOUNDUK.pause();
AZARAB.stop();
}
super.onPause();
}
public void SONIDOITA (View view) {
AZAIT = MediaPlayer.create(this, R.raw.mezquitait);
AZAIT.start();
if(AZASOUNDUK != null && AZASOUNDUK.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUNDUK.stop();
}
if(AZASOUND != null && AZASOUND.isPlaying())
{
//AZASOUNDUK.pause();
AZASOUND.stop();
}
if(AZAGER != null && AZAGER.isPlaying())
{
//AZASOUNDUK.pause();
AZAGER.stop();
}
if(AZAFRENCH != null && AZAFRENCH.isPlaying())
{
//AZASOUNDUK.pause();
AZAFRENCH.stop();
}
else if(AZARAB != null && AZARAB.isPlaying())
{
//AZASOUNDUK.pause();
AZARAB.stop();
}
super.onPause();
}
public void MAPA(View view) {
Intent intent = new Intent(this, MapaROMANO.class);
startActivity(intent);
}
public void FOTOS(View view) {
Intent intent = new Intent(this, SliderMain.class);
startActivity(intent);
}
public void ROM2(View view) {
Intent intent = new Intent(this, SliderMain.class);
startActivity(intent);
}
public void MAPAMEZCA(View view) {
Intent intent = new Intent(this, Mapa1.class);
startActivity(intent);
}
public void URLMEZCA (View view) {
Uri uriUrl = Uri.parse("example");
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
}
}
[1]: https://www.youtube.com/watch?v=BlUql0PqSgQ
The problem is a lot of MediaPlayer creations without any release. Please check the below code, it may help you to identify the right way.
private MediaPlayer mediaPlayer;
public void play(int rawSoundId){
stop();
mediaPlayer = MediaPlayer.create(this, rawSoundId);
mediaPlayer.start();
}
public void pause() {
if (mediaPlayer != null) {
mediaPlayer.pause();
}
}
public void stop() {
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
mediaPlayer = null;
}
}
Just call play(R.raw.mezquitait) from your view functions.