У меня есть просмотрщик, который воспроизводит серию аудиофайлов.
https://ul.uncox.com/qa-image/65873409abe59520d537dcef733e571aa4a1d93e.png
Моя проблема в том, что если одна музыка воспроизводится, а я играю другую, они оба играют вместе.
public class AdapterSong extends RecyclerView.Adapter<AdapterSong.viewHolder> {
Context context;
List<ModelSong> model;
String url_audio;
MediaPlayer mediaplayer;
public AdapterSong(Context context, List<ModelSong> model) {
this.context = context;
this.model = model;
}
@NonNull
@Override
public AdapterSong.viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rec_song, parent, false);
return new AdapterSong.viewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final AdapterSong.viewHolder holder, int position) {
final ModelSong data = model.get(position);
holder.tittle.setText(data.getTittle());
holder.stop.setVisibility(View.GONE);
holder.play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
url_audio = "http://kanoonmedia.ir/files/sorod/" + data.getFile();
mediaplayer = new MediaPlayer();
mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaplayer.setDataSource(url_audio);
mediaplayer.setLooping(true);
mediaplayer.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
holder.play.setVisibility(View.GONE);
holder.stop.setVisibility(View.VISIBLE);
mediaplayer.start();
}
});
holder.stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.play.setVisibility(View.VISIBLE);
holder.stop.setVisibility(View.GONE);
mediaplayer.stop();
}
});
}
@Override
public int getItemCount() {
return model.size();
}
public class viewHolder extends RecyclerView.ViewHolder {
TextView tittle;
CircleImageView play, stop;
public viewHolder(View itemView) {
super(itemView);
tittle = itemView.findViewById(R.id.txt_tittle_song);
play = itemView.findViewById(R.id.img_play_song);
stop = itemView.findViewById(R.id.img_stop_song);
}
}
}
Спасибо всем