0

I have a Recycler view. Each Item view holder has a media player instance. So basically recycler view displays a list of the video item. Whenever a user starts playing an item, I want to stop any other item currently being played.

I want to know how do I get the view holder instance of the view currently playing the content so that I can stop playing it before start playing the new item.

Puja
  • 192
  • 9
Pardeep Kumar
  • 900
  • 1
  • 14
  • 30

6 Answers6

1

Just user single instance of Media Player and when you start play video just check if media player is playing then stop current playing video and start the video which you want.

1

Follow below steps

  1. Take one variable naming mCurrentPlayingPos = -1 in adapter.
  2. Apply logic on click or the event when you starts playing video. Like below

    if (mCurrentPlayingPos != -1) {
        // Find view holder using this position. If view holder is not null, then use method to release 
        // player as given mCurrentPlayingPos. 
        // If viewholder is null, that is recycled to no need to do anything. 
    } else {
        // This would be very first case    
        // Set value for mCurrentPlayingPos with positionInAdapter's value 
    }
    // Setup media player and play video below
    
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
0
int sSelectedPos = 0;

// call this on play button click 

     for (ModelMusic data : itemList) {
     if (itemList.indexOf(data) == position) {
      sSelectedPos = position;
     data.setPlaying(true);
     } else {
      data.setPlaying(false);
     }
      notifyDataSetChanged();
     }

//POJO class to manage your play state of song.
    public class ModelMusic {
        private boolean isPlaying = false;

        public boolean isPlaying() {
            return isPlaying;
        }

        public void setPlaying(boolean playing) {
            isPlaying = playing;
        }
    }
PRATEEK BHARDWAJ
  • 2,364
  • 2
  • 21
  • 35
  • 1
    No i dont think this will be good. Though it will work but its bad to refresh whole list for single item change. ( notifyDataSetChanged();) – taman neupane Sep 12 '18 at 09:32
  • How will you stop action of other playing items without notifydatasetchanged. – PRATEEK BHARDWAJ Sep 12 '18 at 09:33
  • I dont know if this is the solution . but can we use notifyItemChanged(int position) . – taman neupane Sep 12 '18 at 09:36
  • @tamanneupane how will you restrict user to select only one item in many number of rows. if you have not done this type of problem solving then you cant know about this approach. Without changes in setplaying instance nothing will reflect to current ui view. – PRATEEK BHARDWAJ Sep 12 '18 at 09:38
  • Yes i do understand you but there is only one(For first time item selected) or two(After second item select or onwards) set playing instance change. Why are you looping through entire list? And furthure notifyDataSetChanged(); is also inside for loop . That is totally wrong – taman neupane Sep 12 '18 at 09:49
  • Ok if you think its wrong then tell me how will u change value of first item after second item selection ? Share your answer if you have it. – PRATEEK BHARDWAJ Sep 12 '18 at 10:53
0

You can use two variables to store the previous click and recent click,

int pre=-1,recent=-1;boolean clicked=false;

And inside your onClick() method , use

clicked=true;
pre=recent;
recent=clickedposition;
if(pre!=-1)
this.notifyItemChanged(pre);
this.notifyItemChanged(recent);

And in your onBindViewHolder(), add this

 if(clicked)
 { 
    if(pre!=-1 && pos==pre)
      stop(pre);//your code to stop the video
    if(pos==recent)
      {
          playvideo(recent);
          clicked=false;
      }
 }
Jyoti JK
  • 2,141
  • 1
  • 17
  • 40
0

This is my idea, try to implement it; Create a state attribute for each recyclerView item example isPlaying, isPaused

public class Video {
   String url;
   boolean isPlaying;

   public void setPlaying(boolean isPlaying){
          this.isPlaying = isPlaying;
   } 

   public boolean isPlaying(){
          return isPlaying;
   }

}

When the video starts to play set isPlaying to true

public VideoAdapter extends RecyclerView.Adapter<VideoViewHolder>{
     Context ctx;
     List<Video> videos;

     public VideoAdapter(Context ctx, List<Videos> videos){
           this.ctx = ctx;
           this.videos = videos;
     }

      @Override
      public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

          View view = LayoutInflater.from(mContext).inflate(R.layout.video_view, parent, false);
          VideoViewHolder holder = new VideoViewHolder(view);
          return holder;
      }

      @Override
      public void onBindViewHolder(VideoViewHolder holder, int position) {
          holder.bind(videos.get(position));
          holder.setOnClickListener(this);
      }

      @Override
      public void onClick(View v) {
            int adapterPosition = getAdapterPosition();
            Video videos = video.get(adapterPosition)

            if (video.isPlaying()) {
                video.setPlaying(false);
            } else {
                video.setPlaying(true);
            }

            // Notify data set change will reload all view holders and play where playing is true, and cancel where cancel is false
            notifyDataSetChanged();
         }
  }




class VideoViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
VideoView videoView;

VideoViewHolder(View itemView) {
    super(itemView);
    videoView = (VideoView) itemView.findViewById(R.id.videoView);
    itemView.setOnClickListener(this);
}

public void bind(Video video) {
    if (video.isPlaying()) {
        video.play();
    } else {
        videoView.stop();
    }
}
Vincent Mungai
  • 202
  • 3
  • 4
  • How will you stop playing , if song is playing on 1st position and 3rd position on click of 5th row ? I have posted correct one. get a view on it. – PRATEEK BHARDWAJ Sep 12 '18 at 10:55
0

https://github.com/arthur3486/ARVI Refer this beautiful project u will get ur answer

Abhijith mogaveera
  • 918
  • 10
  • 16