Android Recyclerview MediaPlayer previous Item sound stop, release after new one is clicked

后端 未结 1 452
太阳男子
太阳男子 2021-02-11 03:17

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

相关标签:
1条回答
  • 2021-02-11 04:09

    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 ();
                }
            });
    
    0 讨论(0)
提交回复
热议问题