In Android studios i made Soundboard in which i have 4 activities when i'm on first activity i can press sounds all day and they play but right after i click next for second activity when i try to play them they won't play sound. I can click like 7 times and they stop.
I look everywhere but nothing works i even changed whole code again still same thing happens.
Button button1; Button theuniverserequired, iusedthestones, etc more sounds....
iaminevitable = (Button) findViewById(R.id.iaminevitable);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.iaminevitable);
iaminevitable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mp.start();
}
});
iusedthestones = (Button) findViewById(R.id.iusedthestones);
final MediaPlayer mp2 = MediaPlayer.create(this,
R.raw.iusedthestones);
iusedthestones.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mp2.start();
}
}); etc....
button1 = findViewById(R.id.button_1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent int1 = new Intent(MainActivity.this, Activity2.class);
startActivity(int1);
}
});
there is nothing in error messages for why is this happening
First off: Declare your MPs outside your onCreate to prevent it from garbage collected when activity is paused
private MediaPlayer mp,mp2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mp = new MediaPlayer.create(...
mp2 = new MediaPlayer.create(...
Further more: Drop off resources used by MPs when activity stops to avoid memory leaks which may force your app to crash on continuous usage.
@Override
protected void onStop() {
super.onStop();
if (mp != null) {
mp.reset();
mp.release();
mp = null;
}
if (mp2 != null) {
mp2.reset();
mp2.release();
mp2 = null;
}
}