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();
}
}