使用暴力方式解決閃退問題:加大ViewPager可以容許的page數量

This commit is contained in:
Raymond Yang 2023-03-06 09:31:20 +08:00
parent d8d351831c
commit ed8f2a7c0f
3 changed files with 36 additions and 35 deletions

View File

@ -65,27 +65,19 @@ class MainActivity : AppCompatActivity() {
viewPager.apply {
adapter = splitVideoViewAdapter
offscreenPageLimit = 1
offscreenPageLimit = 100
setPageTransformer(null)
registerOnPageChangeCallback(object: ViewPager2.OnPageChangeCallback() {
private var oldPage = 0
override fun onPageScrollStateChanged(state: Int) {
super.onPageScrollStateChanged(state)
if (state == ViewPager2.SCROLL_STATE_DRAGGING) {
/*if (state == ViewPager2.SCROLL_STATE_DRAGGING) {
oldPage = currentPage
splitVideoViewAdapter.stop(currentPage)
}
if (state == ViewPager2.SCROLL_STATE_IDLE && currentPage == oldPage) {
splitVideoViewAdapter.play(currentPage)
}
/*Log.d("Split", "onPageScrollStateChanged: ${
when(state) {
ViewPager2.SCROLL_STATE_IDLE -> "SCROLL_STATE_IDLE"
ViewPager2.SCROLL_STATE_DRAGGING -> "SCROLL_STATE_DRAGGING"
ViewPager2.SCROLL_STATE_SETTLING -> "SCROLL_STATE_SETTLING"
else -> ""
}
}")*/
}*/
//Log.d("Split", "oldPage: $oldPage, currentPage: $currentPage")
}
override fun onPageScrolled(

View File

@ -57,38 +57,42 @@ class SplitViewFragment : Fragment() {
initView()
viewModel.activePage.observe(viewLifecycleOwner) {
/*viewModel.activePage.observe(viewLifecycleOwner) {
if (it == null) return@observe
if (it == this.mPageNum) {
playAll()
MainScope().launch {
//delay(1000)
//playAll()
}
} else {
stopAll()
//stopAll()
}
}
}*/
}
override fun onPause() {
stopAll()
//stopAll()
super.onPause()
Log.d("${TAG}_$mPageNum", "onPause()")
}
override fun onResume() {
super.onResume()
if (viewModel.activePage.value == mPageNum) {
//if (viewModel.activePage.value == mPageNum) {
MainScope().launch {
delay(splitMode * Constants.CONF_DELAY_BASE_MILLIS)
//delay(splitMode * Constants.CONF_DELAY_BASE_MILLIS)
playAll()
}
}
//}
Log.d("${TAG}_$mPageNum", "onResume()")
}
/*override fun onDestroy() {
override fun onDestroy() {
super.onDestroy()
destroyAll()
//destroyAll()
stopAll()
Log.d("${TAG}_$mPageNum", "onDestroy()")
}*/
}
private fun initView() {
// 生成 VideoView 分割畫面
@ -164,7 +168,7 @@ class SplitViewFragment : Fragment() {
MainScope().launch {
stopAll()
//delay(splitMode * Constants.CONF_DELAY_BASE_MILLIS)
delay((splitMode * 100) + Constants.CONF_DELAY_BASE_MILLIS)
//delay((splitMode * 100) + Constants.CONF_DELAY_BASE_MILLIS)
val item = data[position]
val bundle = Bundle().apply {
//putInt(MonitoringActivity.BUNDLE_DEVICE_ID, item.id)
@ -191,32 +195,34 @@ class SplitViewFragment : Fragment() {
}
}
fun playAll() = MainScope().launch(Dispatchers.Main) {
if (videoViews.isEmpty()) return@launch
fun playAll() /*= MainScope().launch(Dispatchers.Main)*/ {
if (videoViews.isEmpty()) return
//delay(splitMode * Constants.CONF_DELAY_BASE_MILLIS)
for (index in data.indices) {
if (!videoViews[index].isReady) continue
videoViews[index].resetRetryCount()
videoViews[index].play()
delay(300)
//delay(300)
}
}.start()
}//.start()
fun stopAll() = MainScope().launch(Dispatchers.Main) {
if (videoViews.isEmpty()) return@launch
fun stopAll() /*= MainScope().launch(Dispatchers.Main)*/ {
if (videoViews.isEmpty()) return
for (index in data.indices) {
videoViews[index].stopRetryCount()
if (!videoViews[index].isPlaying || videoViews[index].isLoading) continue
videoViews[index].stop()
delay(100)
//delay(300)
}
}.start()
}//.start()
fun destroyAll() = MainScope().launch(Dispatchers.Main) {
if (videoViews.isEmpty()) return@launch
fun destroyAll() /*= MainScope().launch(Dispatchers.Main)*/ {
if (videoViews.isEmpty()) return
for (index in data.indices) {
videoViews[index].destroy()
delay(100)
//delay(100)
}
}.start()
}//.start()
companion object {
private val TAG = SplitViewFragment::class.java.simpleName

View File

@ -36,6 +36,8 @@ class VideoView : ConstraintLayout, GstCallback {
private var data: Device? = null
var isReady: Boolean = false
var isLoading: Boolean = false
set(value) {
view.pbLoading.isVisible = value
@ -132,6 +134,7 @@ class VideoView : ConstraintLayout, GstCallback {
override fun onStatus(gstStatus: GstStatus?) { // onStatus 不是在主執行緒,因此透過 Handler 發訊息到主執行緒去執行
when (gstStatus) {
GstStatus.READY -> isReady = true
GstStatus.PLAYING -> mHandler.sendMessage(Message().apply { what = MSG_PLAY })
GstStatus.PAUSE -> mHandler.sendMessage(Message().apply { what = MSG_PAUSE })
//GstStatus.ERROR_WHEN_OPENING -> mHandler.sendMessage(Message().apply { what = MSG_PAUSE })