in the image, I'm playing one sound on looping total code is working fine but when I press the back button or home button on the image play sound didn't stop please help me I'm new I'm making this app by my own
This is my code:
MY JAVA CODE:
ImageView imgPlay;
ImageView imgPause;
int sound1 = -1;
SoundPool soundPool;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
imgPlay = findViewById(R.id.imgPlay);
imgPause = findViewById(R.id.imgPause);
final MediaPlayer sound = MediaPlayer.create(play.this, R.raw.song1);
this.soundPool = new SoundPool(2, 3, 0);
this.sound1 = this.soundPool.load(this, R.raw.sound1, 1)
imgPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sound.start();
sound.setLooping(true);
}
});
imgPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sound.pause();
}
});
((Button) findViewById(R.id.button1)).setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
play.this.soundPool.autoPause();
play.this.soundPool.play(play.this.sound1, 1.0f, 1.0f, 0, 0, 1.0f);
}
});
}
}
You need to override onPause lifecycle of your activity and pause the sound there.