回收视图中的媒体播放器在同一时间播放

本教程将介绍回收视图中的媒体播放器在同一时间播放的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。

回收视图中的媒体播放器在同一时间播放 教程 第1张

问题描述

我在回收器视图中使用媒体播放器,问题是当不同项目的播放按钮被单击时,它们都会同时播放。怎么停止上一个并开始新的?

 public void onBindViewHolder(@NonNull MyAdapter.ViewHolder viewHolder, int position) {
  finalMediaPlayer mediaPlayer = new MediaPlayer();
  try {
mediaPlayer.setDataSource(item.get(position).getAudio());
mediaPlayer.prepare();
  } catch (IOException e) {
e.printStackTrace();
  }

viewHolder.play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
 if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
viewHolder.play.setImageResource(R.drawable.play);

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

推荐答案

这是我的recyclerView适配器,您必须将数组列表放入此适配器

public class MyAdapter2 extends RecyclerView.Adapter<MyAdapter2.AudioItemsViewHolder> {

static MediaPlayer mediaPlayer;
Activity activity;


private final ArrayList<GroupItems> audioItems;//change it() to your items
private int currentPlayingPosition;
private final SeekBarUpdater seekBarUpdater;
private AudioItemsViewHolder playingHolder;



public MyAdapter2(Activity activity, ArrayList<GroupItems> items_pro) {
 this.audioItems = items_pro;
 this.currentPlayingPosition = -1;
 seekBarUpdater = new SeekBarUpdater();
 this.activity = activity;

} 


@Override
public AudioItemsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  //put YourItemsLayout;
 return new AudioItemsViewHolder(LayoutInflater.from(parent.getContext()).inflate(YourItemsLayout, parent, false));
}

@Override
public void onBindViewHolder(AudioItemsViewHolder holder, int position) {
if (position == currentPlayingPosition) {
  playingHolder = holder;
  updatePlayingView();
 } else {
  updateNonPlayingView(holder);
 }
}
private void updateNonPlayingView(AudioItemsViewHolder holder) {
 holder.sbProgress.removeCallbacks(seekBarUpdater);
 holder.sbProgress.setEnabled(false);
 holder.sbProgress.setProgress(0);
 holder.ivPlayPause.setImageResource(R.drawable.ic_baseline_play_arrow_24);
}

private void updatePlayingView() {
 playingHolder.sbProgress.setMax(mediaPlayer.getDuration());
 playingHolder.sbProgress.setProgress(mediaPlayer.getCurrentPosition());
 playingHolder.sbProgress.setEnabled(true);
 if (mediaPlayer.isPlaying()) {
  playingHolder.sbProgress.postDelayed(seekBarUpdater, 100);
  playingHolder.ivPlayPause.setImageResource(R.drawable.ic_pause);
 } else {
  playingHolder.sbProgress.removeCallbacks(seekBarUpdater);
  playingHolder.ivPlayPause.setImageResource(R.drawable.ic_baseline_play_arrow_24);
 }
}
  private class SeekBarUpdater implements Runnable {
 @Override
 public void run() {
  if (null != playingHolder && null != mediaPlayer) {
playingHolder.sbProgress.setProgress(mediaPlayer.getCurrentPosition());
playingHolder.sbProgress.postDelayed(this, 100);
  }
 }
}

@Override
public int getItemCount() {
 return audioItems.size();
}
 
 class AudioItemsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, SeekBar.OnSeekBarChangeListener {
 SeekBar sbProgress;
 ImageView ivPlayPause;
 AudioItemsViewHolder(View itemView) {
  super(itemView);
  ivPlayPause = itemView.findViewById(R.id.sound_btn);
  ivPlayPause.setOnClickListener(this);
  sbProgress = itemView.findViewById(R.id.seekBar);
  sbProgress.setOnSeekBarChangeListener(this);
 }
 @Override
 public void onClick(View v) {


  switch (v.getId()) {
case R.id.seekBar:
 break;

case R.id.sound_btn: {
 if (getAdapterPosition() == currentPlayingPosition) {
  if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.pause();
  } else {
if (mediaPlayer != null)
 mediaPlayer.start();
  }
 } else {
  currentPlayingPosition = getAdapterPosition();
  if (mediaPlayer != null) {
if (null != playingHolder) {
 updateNonPlayingView(playingHolder);
}
mediaPlayer.release();
  }
  playingHolder = this;



 
PlaySound(YOUR AUDIO FILE);//put your audio file


 }
 if (mediaPlayer != null)
  updatePlayingView();
}
break;
 }


 }


  @Override
 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  if (fromUser) {
mediaPlayer.seekTo(progress);
  }
 }

 @Override
 public void onStartTrackingTouch(SeekBar seekBar) {
 }

 @Override
 public void onStopTrackingTouch(SeekBar seekBar) {
 }
}
 private void PlaySound(File filesound) {

 mediaPlayer = MediaPlayer.create(activity, Uri.parse(String.valueOf(filesound)));

 mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
  @Override
  public void onCompletion(MediaPlayer mp) {
releaseMediaPlayer();
  }
 });
 mediaPlayer.start();
}
 private void releaseMediaPlayer() {
 if (null != playingHolder) {
  updateNonPlayingView(playingHolder);
 }
 if (outputFile.exists())
  outputFile.delete();

 mediaPlayer.release();
 mediaPlayer = null;
 currentPlayingPosition = -1;
}
}

好了关于回收视图中的媒体播放器在同一时间播放的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。