我已经在每个项目中建立了一个我的音乐列表(RecyclerView),有两个按钮首先播放音乐,暂停第二个停止但是当我点击播放两个音频时,两个音频启动,而我希望第一个播放停。这是我的适配器 (标志是在播放音频时将图像视图从播放更改为暂停)
public class ViewAudioListAdapter extends RecyclerView.Adapter<ViewAudioListAdapter.ViewHolder>{
private List<Audio> values;
private Context context;
private MediaPlayer mediaPlayer;
private boolean flage = true;
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView textViewName;
public ImageView ivPlay,ivStop;
public View layout;
public ViewHolder(View view) {
super(view);
this.textViewName = (TextView) view.findViewById(R.id.textViewName);
this.ivPlay = (ImageView) view.findViewById(R.id.ivPlay);
this.ivStop = (ImageView) view.findViewById(R.id.ivStop);
this.layout = view;
}
}
public void add(int position, Audio audio){
values.add(position, audio);
notifyItemInserted(position);
}
public void remove(int position){
values.remove(position);
notifyItemRemoved(position);
}
public ViewAudioListAdapter(List<Audio> myDataset, Context context) {
values = myDataset;
this.context=context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v = inflater.inflate(R.layout.view_audio_item, parent, false);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh; }
@Override
public void onBindViewHolder(final ViewAudioListAdapter.ViewHolder holder, int position) {
final Audio audio = new Audio(values.get(position));
final String name = audio.getName();
holder.textViewName.setText(name);
holder.ivPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (flage){
mediaPlayer = MediaPlayer.create(context , Uri.parse(url));
flage = false ;
}
if (mediaPlayer.isPlaying()){
mediaPlayer.pause();
holder.ivPlay.setImageResource(R.drawable.icons_play);
}else{
mediaPlayer.start();
holder.ivPlay.setImageResource(R.drawable.icons_pause);
}
}
});
holder.ivStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!flage){
mediaPlayer.stop();
mediaPlayer.release();
flage = true;
}
holder.ivPlay.setImageResource(R.drawable.icons_play);
}
});
}
@Override
public int getItemCount() {
return values.size();
}}
谢谢
答案 0 :(得分:0)
尝试在启动mediaPlayer之前添加mediaPlayer.stop();
和mediaPlayer.reset();
,然后在点击事件中使用以下代码
holder.ivPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (flage){
mediaPlayer = MediaPlayer.create(context , Uri.parse(url));
flage = false ;
}
if (mediaPlayer.isPlaying()){
mediaPlayer.pause();
holder.ivPlay.setImageResource(R.drawable.icons_play);
}else{
mediaPlayer.stop();//add here
mediaPlayer.reset();//update here
mediaPlayer = MediaPlayer.create(context , Uri.parse(url));// new update here
mediaPlayer.start();
holder.ivPlay.setImageResource(R.drawable.icons_pause);
}
}
});
这可能会有所帮助..
或
holder.ivPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (flage){
mediaPlayer = MediaPlayer.create(context , Uri.parse(url));
flage = false ;
}
if (mediaPlayer.isPlaying()){
mediaPlayer.pause();
holder.ivPlay.setImageResource(R.drawable.icons_play);
}else{
try {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = MediaPlayer.create(context , Uri.parse(url));
}
mediaPlayer.start();
holder.ivPlay.setImageResource(R.drawable.icons_pause);
} catch (Exception e) {
}
}
}
});