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

场景描述

  • 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.
*
*

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.

*
*

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.

*
*

While prefetch is enabled, it will have the side effect of expanding the effective
* size of the View cache to hold prefetched views.

*
* @param enabled True if items should be prefetched in between traversals.
*
* @see #isItemPrefetchEnabled()
*/

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

以上。

下载说明:
1、本站所有资源均从互联网上收集整理而来,仅供学习交流之用,因此不包含技术服务请大家谅解!
2、本站不提供任何实质性的付费和支付资源,所有需要积分下载的资源均为网站运营赞助费用或者线下劳务费用!
3、本站所有资源仅用于学习及研究使用,您必须在下载后的24小时内删除所下载资源,切勿用于商业用途,否则由此引发的法律纠纷及连带责任本站和发布者概不承担!
4、本站站内提供的所有可下载资源,本站保证未做任何负面改动(不包含修复bug和完善功能等正面优化或二次开发),但本站不保证资源的准确性、安全性和完整性,用户下载后自行斟酌,我们以交流学习为目的,并不是所有的源码都100%无错或无bug!如有链接无法下载、失效或广告,请联系客服处理!
5、本站资源除标明原创外均来自网络整理,版权归原作者或本站特约原创作者所有,如侵犯到您的合法权益,请立即告知本站,本站将及时予与删除并致以最深的歉意!
6、如果您也有好的资源或教程,您可以投稿发布,成功分享后有站币奖励和额外收入!
7、如果您喜欢该资源,请支持官方正版资源,以得到更好的正版服务!
8、请您认真阅读上述内容,注册本站用户或下载本站资源即您同意上述内容!
原文链接:https://www.dandroid.cn/20799,转载请注明出处。
0

评论0

显示验证码
没有账号?注册  忘记密码?