I want to make a simple music player app that plays songs on selected playlists. I successfully retrieve the details of the songs of the device. I have a playlist of multiple songs. I want to play only one song at a time. but when I click on other songs already playing song didn't stop or pause. please give a complete solution with reason.
ArrayList<SongModel> list;
SongModel Model Class
public class SongModel {
String songImg,songName,songPath;
Boolean isClick;
public SongModel(String songImg, String songName, String songPath, Boolean isClick) {
this.songImg = songImg;
this.songName = songName;
this.songPath = songPath;
this.isClick = isClick;
}
public Boolean getClick() {
return isClick;
}
public void setClick(Boolean click) {
isClick = click;
}
public String getSongPath() {
return songPath;
}
public void setSongPath(String songPath) {
this.songPath = songPath;
}
public String getSongImg() {
return songImg;
}
public void setSongImg(String songImg) {
this.songImg = songImg;
}
public String getSongName() {
return songName;
}
public void setSongName(String songName) {
this.songName = songName;
}
}
MediaPlayer used in Adapter
MediaPlayer mediaPlayer = MediaPlayer.create(context, Uri.parse(list.get(position).getSongPath()));
if(list.get(position).getClick()){
holder.imgv_pause.setVisibility(View.VISIBLE);
holder.imgv_play.setVisibility(View.GONE);
try {
mediaPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
} else {
list.get(position).setClick(false);
holder.imgv_pause.setVisibility(View.GONE);
holder.imgv_play.setVisibility(View.VISIBLE);
try {
mediaPlayer.pause();
} catch (Exception e) {
e.printStackTrace();
}
}
holder.imgv_play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i=0;i<list.size();i++){
list.get(i).setClick(false);
}
list.get(position).setClick(true);
notifyDataSetChanged();
}
});
holder.imgv_pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
list.get(position).setClick(false);
notifyDataSetChanged();
}
});
}
You need to stop the pervious song when the user scroll in onViewRecycled
this function is called when the user scroll the recycler-view .
@Override
public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
super.onViewRecycled(holder);
//stop song by its holder here
}
only one song at a time in Android Studio use this static MediaPlayer mediaPlayer;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.