1

So I've been trying to do a Soundbox app from youtube tutorials. So far it works fine but I have some issues and I don't know how to fix them... I'm 100% beginner and I've tried to search on previous topics but nothing works. So the sounds play when I press a button but the problem is when I play another button, it stops playing the first sound. I would like to keep the first sound playing while the second one starts when I press the button. Is it possible?

Ps: How can I also do a "stop" button to stop all songs?

Thanks

Here is my code:

package com.example.appcamp;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;public class MainActivity extends AppCompatActivity {
private MediaPlayer mPlayer = null;

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

    Button criloin= (Button) findViewById(R.id.criloin1);
    criloin.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
            playSound(R.raw.cri);
        }
    });

    Button criloin2= (Button) findViewById(R.id.criloin2);
    criloin2.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
            playSound(R.raw.cri2);
        }
    });

    Button criloin3= (Button) findViewById(R.id.criloin3);
    criloin3.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
            playSound(R.raw.cri3);
        }
    });

    Button criloin4= (Button) findViewById(R.id.criloin4);
    criloin4.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
            playSound(R.raw.cri4);
        }
    });

    Button ambiance1= (Button) findViewById(R.id.ambiance1);
    ambiance1.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
            playSound(R.raw.marais);
        }
    });

}

@Override
public void onPause(){
    super.onPause();
    if(mPlayer !=null){
        mPlayer.stop();
        mPlayer.release();
    }
}

private void playSound(int resId){
    if(mPlayer != null){
        mPlayer.stop();
        mPlayer.release();
    }
    mPlayer = MediaPlayer.create(this, resId);
    mPlayer.start();

   }
}
Dimigia
  • 11
  • 1

2 Answers2

1

Try to remove

if(mPlayer != null){
        mPlayer.stop();
        mPlayer.release();
    }

from playSound() function.

It should look something like this:

private void playSound(int resId) {
    mPlayer = MediaPlayer.create(this, resId);
    mPlayer.start();
}

and to stop all the music you should use individual mediaPlayer Objects for each music to stop and implement stop all at once function like mp1.stop(); mp2.stop(); and so on.

Hope that works for you.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • dont put your comment as answer , use add comment section for that... and for stop all music at once try this method shown in this https://stackoverflow.com/questions/29919101/how-to-stop-all-sounds-in-activity – Jeet Chhatrala Jun 13 '20 at 07:48
0

Thank you! The stacking part works perfectly but I didn't really get how to do the stop button...

Dimigia
  • 11
  • 1