播放按钮不会更新

时间:2018-04-05 16:02:23

标签: android android-mediaplayer

我的listview按钮出了问题。如果我点击播放btn的位置正在播放正确的歌曲并且btn更新暂停,反之亦然。但是如果我在播放歌曲时单击listview中的其他播放按钮暂停了最后一首歌。我用mediaPlayer.stop()和release()更新了我的代码,但现在当我点击其他播放时它停止播放它但是它没有改变我的最后一个暂停按钮来播放如果我点击播放歌曲按钮暂停它崩溃。我知道if的位置(mediaPlayer!= null),但我无法弄明白。 这是我的代码:

public class MusicAdapter extends BaseAdapter {
private Context context;
private int layout;
private ArrayList arrayList;
private MediaPlayer mediaPlayer;
private boolean flag = true;
int currentPosition;
public MusicAdapter(Context context, int layout, ArrayList arrayList) {
this.context = context;
this.layout = layout;
this.arrayList = arrayList;
}

@Override
public int getCount() {
    return arrayList.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}
private class Holder{
    TextView textName,textAlbum;
    ImageView play, stop;
}


@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final Holder holder;
    if (convertView == null) {
        holder = new Holder();
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(layout, null);
        holder.textName = (TextView) convertView.findViewById(R.id.textName);
        holder.textAlbum = (TextView) convertView.findViewById(R.id.textAlbum);
        holder.play = convertView.findViewById(R.id.play);
        holder.stop = convertView.findViewById(R.id.stop);

        convertView.setTag(holder);
    } else {
        holder = (Holder) convertView.getTag();

    }
    final Music music = (Music) arrayList.get(position);
    holder.textName.setText(music.getName());
    holder.textAlbum.setText(music.getAlbum());


    holder.play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mediaPlayer != null) {
                mediaPlayer.stop();
                mediaPlayer.reset();
                mediaPlayer.release();
            }
            mediaPlayer = MediaPlayer.create(context, music.getSong());




                flag = false;


            if (mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
                holder.play.setImageResource(R.drawable.ic_play);

            } else {
                mediaPlayer.start();
                holder.play.setImageResource(R.drawable.pause);
            }

        }
    });

    holder.stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!flag) {
                mediaPlayer.stop();
                mediaPlayer.release();
                flag = true;
            }
            holder.play.setImageResource(R.drawable.ic_play);
        }
    });
    return convertView;

}

}

0 个答案:

没有答案