title: How to make audio player with play/pause button and seekbar in recycleview
body: <p>I need some help, I want to make a recycle view containing audio in each item, how to play the audio when press play button and set progress to seekbar and at the same time pause other sound and convert the play button to play mode in all row except the playing one be in pause mode
  such as <a href="https://i.stack.imgur.com/eny8B.png" rel="noreferrer"><img src="https://i.stack.imgur.com/eny8B.png" alt="whatsapp"></a> or chats in facebook messenger</p>
  
  <p>This is my code: 
  MyAdapter.class</p>
  
  <pre><code>    private List&lt;String&gt; positions = new ArrayList&lt;&gt;();
  
    public myAdapter(Activity activity, ArrayList&lt;Tweet&gt; list) {
      this.activity = activity;
      this.list = list;
      mPlayer = new MediaPlayer();
      }
      public void onBindViewHolder(final viewHolder holder, final int position) {
          if (!positions.contains(String.valueOf(position)))
              positions.add(String.valueOf(position));
          }
          }
  </code></pre>
  
  <p>viewHolder.class</p>
  
  <pre><code>  public class viewHolder extends RecyclerView.ViewHolder implements
          SeekBar.OnSeekBarChangeListener, View.OnClickListener {
      LinearLayout parentPanel;
      int viewType;
      private RelativeLayout pauseLayout, playLayout;
      private ImageView pauseIcon, playIcon;
      private SeekBar seekBar;
      private TextView periodTime;
      AudioCallbacks mAudioCallbacks;
      private int last_position;
  
      viewHolder(final View itemView, int viewType) {
          super(itemView);
          this.viewType = viewType;
  
              playLayout = (RelativeLayout) itemView.findViewById(R.id.play_layout);
              pauseLayout = (RelativeLayout) itemView.findViewById(R.id.pause_layout);
              playIcon = (ImageView) itemView.findViewById(R.id.play_icon);
              pauseIcon = (ImageView) itemView.findViewById(R.id.pause_icon);
              seekBar = (SeekBar) itemView.findViewById(R.id.seekBar);
              periodTime = (TextView) itemView.findViewById(R.id.period_time);
  
  
              seekBar.setOnSeekBarChangeListener(this);
              playLayout.setOnClickListener(this);
              pauseLayout.setOnClickListener(this);
              mAudioCallbacks = new AudioCallbacks() {
                  @Override
                  public void onUpdate(int percentage) {
                      seekBar.setProgress(percentage);
                      if (percentage == 100)
                          mAudioCallbacks.onStop();
                  }
                  @Override
                  public void onPause() {
                      Log.i("on pause audio", " pause");
                  }
                  @Override
                  public void onStop() {
                      Log.i("on stop audio", " stop");
                      stopPlayingAudio();
                  }
              };
      }
  
      void stopPlayingAudio() {
          if (mPlayer != null) {
              if (mPlayer.isPlaying()) {
                  updateAudioProgressBar();
                  mPlayer.stop();
                  mPlayer.reset();
                  seekBar.setProgress(0);
                  playLayout.setVisibility(View.VISIBLE);
                  pauseLayout.setVisibility(View.GONE);
              }
          }
      }
  
      void pausePlayingAudio() {
          if (mPlayer != null) {
              if (mPlayer.isPlaying()) {
                  mPlayer.pause();
                  updateAudioProgressBar();
                  mAudioCallbacks.onPause();
              }
          }
      }
      void playingAudio(Tweet message) {
          updateAudioProgressBar();
          if (mPlayer != null) {
              try {
                  mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                  mPlayer.setDataSource(message.getAudioUrl());
                  mPlayer.prepare();
                  mPlayer.start();
                  periodTime.setVisibility(View.VISIBLE);
                  periodTime.setText(String.valueOf(message.getAudioDuration()));
              } catch (Exception e) {
                  e.printStackTrace();
              }
  
          }
      }
  
      void updateAudioProgressBar() {
          durationHandler.postDelayed(mUpdateTimeTask, 100);
      }
      private Runnable mUpdateTimeTask = new Runnable() {
          public void run() {
              try {
                  if (mPlayer.isPlaying()) {
                      long totalDuration = mPlayer.getDuration();
                      long currentDuration = mPlayer.getCurrentPosition();
                      int progress = Utils.getProgressPercentage(currentDuration, totalDuration);
                      mAudioCallbacks.onUpdate(progress);
                      periodTime.setText(Utils.getFileTime(currentDuration));
                      durationHandler.postDelayed(this, 100);
                  }
              } catch (Exception e) {
                  e.printStackTrace();
              }
  
          }
      };
  
      @Override
      public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
  
      }
  
      @Override
      public void onStartTrackingTouch(SeekBar seekBar) {
          durationHandler.removeCallbacks(mUpdateTimeTask);
      }
  
      @Override
      public void onStopTrackingTouch(SeekBar seekBar) {
          int totalDuration = mPlayer.getDuration();
          int currentPosition = Utils.progressToTimer(seekBar.getProgress(), totalDuration);
          mPlayer.seekTo(currentPosition);
          updateAudioProgressBar();
      }
  
      @Override
      public void onClick(View view) {
          Log.e("getAdapterPosition()L", last_position + " /");
          for (String pos : positions) {
              if (!pos.equals(String.valueOf(getAdapterPosition())))
                  notifyItemChanged(Integer.parseInt(pos));
          }
          last_position = getAdapterPosition();
          Log.e("getAdapterPosition()F", last_position + " /");
          Tweet message = list.get(getAdapterPosition());
          switch (view.getId()) {
              case R.id.pause_layout:
                  playLayout.setVisibility(View.VISIBLE);
                  pauseLayout.setVisibility(View.GONE);
                  pausePlayingAudio();
                  break;
              case R.id.play_layout:
                  playLayout.setVisibility(View.GONE);
                  pauseLayout.setVisibility(View.VISIBLE);
                  mAudioCallbacks.onStop();
                  playingAudio(message);
                  break;
          }
      }
      }
      public interface AudioCallbacks {
      void onUpdate(int percentage);
      void onPause();
      void onStop();
      }
  </code></pre>
  
  <p>the problem of this code is :</p>
  
  <ol>
  <li>some time the image not reversed of other object when play specific item .</li>
  <li>when scrolled the pause button appear in multiple row.</li>
  <li>when play one then play another, stop the first and run the new one but when return to the previous not playing.</li>
  <li>when leave the activity or press back press button the sound not stopped.</li>
  </ol>
  
  <p>Can someone helped me, please?</p>