Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pause a video in a recyclerView when it is a certain percentage on the screen

I want to play a video when its view is 75% on the screen inside of the recyclerView adapter. So findFirstVisibleItem and all those would not work here. (Or if they do, I have no idea how they would work).

Currently I am waiting for views to be recycled before I am able to pause or play a video, but this begins the moment a new view is generated. Can anyone please help I have been researching this for very long and have gotten no where.

like image 371
Janwilx72 Avatar asked May 03 '17 15:05

Janwilx72


People also ask

What is ViewHolder in RecyclerView?

A ViewHolder describes an item view and metadata about its place within the RecyclerView. RecyclerView. Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive View. findViewById(int) results.

What does notifyDataSetChanged do in RecyclerView?

notifyDataSetChanged. Notify any registered observers that the data set has changed. There are two different classes of data change events, item changes and structural changes. Item changes are when a single item has its data updated but no positional changes have occurred.

How many times is called onCreateViewHolder?

By default it have 5. you can increase as per your need.


1 Answers

All you have to do is use a **Model Class**.!

        Class Video{

        String videoUrl;   //Http://
        int videoDrawable; //R.id.video
        String videoName=;
        boolean isplaying=flase;
        boolean ispuase=false;
        int lastDuration=0;



        //Constructors

        gettter and setter  here.!

        }
    ///////////////////
    Class AdapterClasss{
     int lastpostion=-1;
    VideoView videoView ;

     public MyViewHolder(View view) {
                super(view);

    videoView =(VideoView)view.findViewById(R.id.videoView1);  
    playBtn=(Button)view.findViewById(R.id.playBtn);
    pauseBtn=(Button)view.findViewById(R.id.pauseBtn);
    }

now in Your Adapter Class do few changes in onBindViewHolder

         @Override
            public void onBindViewHolder(final MyViewHolder holder, final int position) {

                final Video videoObj= videoList.get(position);
    /Creating MediaController 

    holder.playBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

            MediaController mediaController= new MediaController(this);  
                mediaController.setAnchorView(holder.videoView);  

         //specify the location of media file  
               Uri uri=Uri.parse(Environment.getExternalStorageDirectory().getPath()+
    "/media/"+videoObj.getvideoName()+".mp4");          


                  //Setting MediaController and URI, then starting the videoView  
               videoView.setMediaController(mediaController);  
               videoView.setVideoURI(uri);          
               videoView.requestFocus();  
   lastpostion=position;

     if(videoObj.getisPuase())
    {
    //now here you have to set your duration fro where you want to start your video 


if(videoView.ispause())
{
               videoView.start();  
            videoObj.setisPlaying(true);
 notifyItemChanged(position);
}

    }else if(videoObj.getisPlaying()!=true)
        {
      videoView.start();  
            videoObj.setisPlaying(true);
 notifyItemChanged(position);

}

    }


    holder.pauseBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {


       int duration = videoView.getDuration();
        videoView.pause();
        lastDuration.setLastDuration(duration);
        videoObj.setisPlaying(false);
         videoObj.setisPlaying(true);


    }



    }
like image 54
Atif AbbAsi Avatar answered Sep 21 '22 02:09

Atif AbbAsi