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.
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.
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.
By default it have 5. you can increase as per your need.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With