In my app i have recyclerview items, and each item play sound when i click it. Problem is that when i click few items, they all play sounds in the same time until they finish. L
You first have to check if there is a sound that is already playing and if there is, you should stop it when the user decides to start a new sound.
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaplayer.isPlaying ()) {
if (mediaplayer != null){
mediaplayer.stop ();
}
} else {
if (mediaplayer != null) {
mediaplayer.start ();
}
}
}
});
In setOnCompletionListner
you release the sound if the user listens to the whole sound clip.
mediaplayer. setOnCompletionListner (new MediaPlayer.OnCompletionListner () {
public void OnCompletion (MediaPlayer mediaplayer) {
mediaplayer.release ();
}
});