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

烈酒焚心 提交于 2019-12-03 08:58:31

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 ();
            }
        });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!