title: Android media player in listview
body: <p>I'm new to android and building a chatroom app with voice messages similar to WhatApp or Messenger. So far I am able to create and upload the voice note to my server as well as play it back on the device. However, I am having an issue wrapping my head around how I should be handling the playback so that only one voice note is being played at a time. I was able to get only one message to playback at a time by initializing the media player in the ListAdapter then calling mp.stop() if the mediaplayer is playing. However, each time I chose to play another voice note my timer and seekbar for the last two voice notes activated are being updated. </p>
  
  <p>I'm thinking that maybe I'm approaching this issue incorrectly. The below code resides in my view class.</p>
  
  <pre><code>// Runnable
  private Runnable onEverySecond = new Runnable() {
      @Override
      public void run() {
          if (!isPaused){
              if (mediaPlayer.isPlaying()) {
                  // Update seekbar position
                  position = mediaPlayer.getCurrentPosition();
                  // Displaying time completed playing
                  playTime.setText(AppUtils.milliSecondsToTimer(mediaPlayer.getCurrentPosition()));
                  seekBar.setProgress(position);
                  // Running this thread after 100 milliseconds
                  seekBar.postDelayed(onEverySecond, 100);
              }else{
                  if(listener != null) listener.onAudioStoppedListener(true);
              }
          }
      }
  };
  
  // Play button
  actionButton.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
              Log.d(TAG, "Play button clicked!");
              if (isPaused) {
                  if (mediaPlayer.isPlaying()){
                      mediaPlayer.stop();
                  }
                  setMediaPlayer();
                  setIsPlaying();
                  mediaPlayer.seekTo(position);
                  mediaPlayer.start();
                  updateProgressBar();
              } else {
                  mediaPlayer.pause();
                  setPaused();
              }
          }
      });
  
  
  private void setMediaPlayer() {
      try {
          String fileName = getFileName();
          File filePath = new File(MyApp.getVoiceNoteDir() + fileName);
          if (filePath.exists()){
              mediaPlayer.reset();
              mediaPlayer.setDataSource(filePath.getPath());
              mediaPlayer.prepare();
  
              seekBar.setMax(mediaPlayer.getDuration());
              seekBar.setProgress(0);
              playTime.setText(AppUtils.milliSecondsToTimer(mediaPlayer.getDuration()));
              actionButton.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_pause_light));
              mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                  @Override
                  public void onCompletion(MediaPlayer mp) {
                      mp.stop();
                      seekBar.setProgress(0);
                      actionButton.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_play_light));
                      setPaused();
                  }
              });
          }else{
              checkFile();
          }
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  
  
  void setPaused(){
      isPaused = true;
      actionButton.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.ic_play_light));
  }
  
  void setIsPlaying(){
      if (mediaPlayer.isPlaying()) mediaPlayer.stop();
      isPaused = false;
      actionButton.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.ic_pause_light));
  }
  </code></pre>