资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Android仿搜狐视频、微视等列表播放视频功能的详细解析-创新互联

这篇文章主要讲解了Android仿搜狐视频、微视等列表播放视频功能的详细解析,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

成都创新互联技术团队10余年来致力于为客户提供成都网站设计、成都网站制作、品牌网站制作成都营销网站建设、搜索引擎SEO优化等服务。经过多年发展,公司拥有经验丰富的技术团队,先后服务、推广了成百上千家网站,包括各类中小企业、企事单位、高校等机构单位。

最近项目中需要是实现在列表中自动播放视频,中间遇到了些问题,终于解决,特来跟大家分享一下:

列表使用的RecyclerView 播放视频使用MediaPlayer+TextureView。

主要思路:

1、监听RecyclerView的滑动,开始滑动时停止正在播放的item。

2、通过LinearLayoutManager 获取当前显示的第一个item及最后一个item

3、RecyclerView停止滑动后,选择item进行播放。如果当前界面只有一个item,播放当前。如果item数量大于2个,播放第二个。如当前界面有两个item则判定哪一个显示的区域比较大。播放item并记录当前position。

附上主要实现逻辑:

try { 
  int fristPos = layoutManager.findFirstVisibleItemPosition(); 
  int lastPos = layoutManager.findLastVisibleItemPosition(); 
  ViewHolder holder = null; 
  if (recyclerView.getChildCount() == 2) { 
  View fristView = recyclerView.getChildAt(0); 
  if (fristView != null) { 
   int[] location = new int[2]; 
   fristView.getLocationInWindow(location); 
   if (location[1] > 0) { 
   holder = (ViewHolder) recyclerView.findViewHolderForPosition(fristPos); 
   lastPlayPosition = fristPos; 
   } 
  } 
  if (holder == null) { 
   View lastView = recyclerView.getChildAt(1); 
   if (lastView != null) { 
   int[] lastViewLocation = new int[2]; 
   lastView.getLocationInWindow(lastViewLocation); 
   if ((lastViewLocation[1] + videoHeight) < screenHeight) { 
    holder = (ViewHolder) recyclerView.findViewHolderForPosition(lastPos); 
    lastPlayPosition = lastPos; 
   } 
   } 
 
 
  } 
  } else if (recyclerView.getChildCount() == 1) { 
  holder = (ViewHolder) recyclerView.findViewHolderForPosition(fristPos); 
  lastPlayPosition = fristPos; 
  } else { 
  holder = (ViewHolder) recyclerView.findViewHolderForPosition(fristPos + 1); 
  lastPlayPosition = fristPos + 1; 
  } 
 
 
  if (holder != null) { 
  holder.play(); 
  } 
  
 } catch (Exception e) { 
  e.printStackTrace(); 
} 

文章标题:Android仿搜狐视频、微视等列表播放视频功能的详细解析-创新互联
网页URL:http://cdkjz.cn/article/gdcdg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220