Skip to content Skip to sidebar Skip to footer

Android Recyclerview Mediaplayer Previous Item Sound Stop, Release After New One Is Clicked

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. Lets

Solution 1:

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(newView.OnClickListener() {
            @OverridepublicvoidonClick(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 () {
            publicvoidOnCompletion (MediaPlayer mediaplayer) {
                mediaplayer.release ();
            }
        });

Post a Comment for "Android Recyclerview Mediaplayer Previous Item Sound Stop, Release After New One Is Clicked"