前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >GapWorker导致RecyclerView视频播放声音残留问题

GapWorker导致RecyclerView视频播放声音残留问题

作者头像
技术小黑屋
发布2020-01-23 20:29:03
1.6K0
发布2020-01-23 20:29:03
举报
文章被收录于专栏:技术小黑屋技术小黑屋

场景描述

  • App 有两个tab,每一个都是Fragment,以FragmentA和FragmentB 代称.
  • 切到FragmentA 视频播放(在RecyclerViewA 内部),然后切到FragmentB 视频暂停.
  • 就在此刻,滑动FragmentB 的recyclerView B ,来自FragmentA的视频播放出声音,而且声音是下一条视频的声音。

这确实是一个非常奇怪的问题,不滑动不会出现视频播放声音,必须滑动一下才能出现声音。

解决思路

1.分析日志,查找播放业务相关的代码 2.增加logStackTrace(“xxx”)用来打印出调用的栈信息

辅助方法

该方法用来查看调用的层级关系,实现原理很简单,就是生成一个Throwable,然后打印stacktrace。

1 2 3 4 5

fun logStackTrace(tag: String) { if (BuildConfig.DEBUG) { Log.w("logStackTrace $tag", Throwable(tag)) } }

问题日志

于是我们得到了如下的日志

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

W logStackTrace : W logStackTrace : java.lang.Throwable: W logStackTrace : at com.xxxxx.commonsdk.utils.ExtensionKt.logStackTrace(Extension.kt:99) W logStackTrace : at com.xxxxx.xxxxx.xxx.video.DiscoveryVideoPlayer.setUp(DiscoveryVideoPlayer.java:786) W logStackTrace : at com.shuyu.gsyvideoplayer.video.base.GSYVideoView.setUp(GSYVideoView.java:446) W logStackTrace : at com.shuyu.gsyvideoplayer.video.base.GSYVideoControlView.setUp(GSYVideoControlView.java:541) W logStackTrace : at com.xxxxx.xxxxx.xxx.ui.adapter.VideoFeedAdapter.initVideo(VideoFeedAdapter.java:211) W logStackTrace : at com.xxxxx.xxxxx.xxx.ui.adapter.VideoFeedAdapter.onBindViewHolder(VideoFeedAdapter.java:127) W logStackTrace : at com.xxxxx.xxxxx.xxx.ui.adapter.VideoFeedAdapter.onBindViewHolder(VideoFeedAdapter.java:34) W logStackTrace : at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6673) W logStackTrace : at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6714) W logStackTrace : at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5647) W logStackTrace : at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5913) W logStackTrace : at android.support.v7.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:285) W logStackTrace : at android.support.v7.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:342) W logStackTrace : at android.support.v7.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:358) W logStackTrace : at android.support.v7.widget.GapWorker.prefetch(GapWorker.java:365) W logStackTrace : at android.support.v7.widget.GapWorker.run(GapWorker.java:396) W logStackTrace : at android.os.Handler.handleCallback(Handler.java:891) W logStackTrace : at android.os.Handler.dispatchMessage(Handler.java:102) W logStackTrace : at android.os.Looper.loop(Looper.java:207) W logStackTrace : at android.app.ActivityThread.main(ActivityThread.java:7470) W logStackTrace : at java.lang.reflect.Method.invoke(Native Method) W logStackTrace : at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) W logStackTrace : at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)

问题症结

问题的症结就在GapWorker调用导致了RecyclerView的item预加载处理。

解决方法

1

yourLayoutManager.setItemPrefetchEnabled(false);

为什么会这样

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

/** * Sets whether the LayoutManager should be queried for views outside of * its viewport while the UI thread is idle between frames. * * <p>If enabled, the LayoutManager will be queried for items to inflate/bind in between * view system traversals on devices running API 21 or greater. Default value is true.</p> * * <p>On platforms API level 21 and higher, the UI thread is idle between passing a frame * to RenderThread and the starting up its next frame at the next VSync pulse. By * prefetching out of window views in this time period, delays from inflation and view * binding are much less likely to cause jank and stuttering during scrolls and flings.</p> * * <p>While prefetch is enabled, it will have the side effect of expanding the effective * size of the View cache to hold prefetched views.</p> * * @param enabled <code>True</code> if items should be prefetched in between traversals. * * @see #isItemPrefetchEnabled() */

上述是setItemPrefetchEnabled的注释,item prefetch是一种用来减少滑动时卡顿的一种预加载方式。这种对于普通的RecyclerView的item没有问题,但是对于视频有声音的,就显得问题明显了。所以这里的解决方法就是关闭这个预取的设置。

以上。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 解决思路
  • 辅助方法
  • 问题日志
  • 问题症结
  • 解决方法
  • 为什么会这样
相关产品与服务
云点播
面向音视频、图片等媒体,提供制作上传、存储、转码、媒体处理、媒体 AI、加速分发播放、版权保护等一体化的高品质媒体服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档