我想播放一个视频,当它的视图是在回收器视图适配器的屏幕上的75%.所以findFirstVisibleItem,所有这些都不会在这里工作. (或者如果他们这样做,我不知道他们会如何工作).
目前我正在等待视图被回收利用,然后才能暂停或播放视频,但是这一刻从生成新视图开始.任何人都可以帮助我一直在研究这个很长时间,没有在哪里.
解决方法
所有你需要做的是使用**模型类**.
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); }
现在在您的适配器类中,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); } }