將跳轉加上延遲
This commit is contained in:
parent
b4346f142b
commit
85218c443f
@ -12,6 +12,7 @@ import com.ray650128.gstreamer_demo_app.R
|
||||
import com.ray650128.gstreamer_demo_app.databinding.ActivityMainBinding
|
||||
import com.ray650128.gstreamer_demo_app.model.Device
|
||||
import kotlinx.coroutines.MainScope
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
@ -20,9 +21,12 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
private val viewModel: MainViewModel by viewModels()
|
||||
|
||||
private val splitViewModel: SplitViewModel by viewModels()
|
||||
|
||||
private val mContext: Context by lazy { this }
|
||||
|
||||
private var splitMode = 1
|
||||
private var oldSplitMode = 1
|
||||
|
||||
private lateinit var splitVideoViewAdapter: VideoViewAdapter
|
||||
|
||||
@ -52,8 +56,9 @@ class MainActivity : AppCompatActivity() {
|
||||
registerOnPageChangeCallback(object: ViewPager2.OnPageChangeCallback() {
|
||||
override fun onPageSelected(position: Int) {
|
||||
super.onPageSelected(position)
|
||||
splitVideoViewAdapter.stop(currentPage)
|
||||
/*splitVideoViewAdapter.stop(currentPage)*/
|
||||
currentPage = position
|
||||
splitViewModel.activePage.postValue(position)
|
||||
Log.d("Split", "currentPage: $currentPage")
|
||||
/*for (i in 0 until splitVideoViewAdapter.itemCount) {
|
||||
if (i == currentPage) continue
|
||||
@ -95,6 +100,11 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
private fun reloadVideoViews(list: List<List<Device>>?) = MainScope().launch {
|
||||
binding.viewPager.setCurrentItem(0, false)
|
||||
for (i in 0 until splitVideoViewAdapter.itemCount) {
|
||||
splitVideoViewAdapter.stop(i)
|
||||
}
|
||||
delay(oldSplitMode * 300L)
|
||||
oldSplitMode = splitMode
|
||||
splitVideoViewAdapter.clear()
|
||||
// 如果群組內沒有裝置,則顯示底圖
|
||||
if (list.isNullOrEmpty()) {
|
||||
@ -109,6 +119,8 @@ class MainActivity : AppCompatActivity() {
|
||||
)
|
||||
splitVideoViewAdapter.add(i, splitFragment)
|
||||
}
|
||||
currentPage = 0
|
||||
binding.viewPager.currentItem = 0
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -7,6 +7,7 @@ import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import androidx.gridlayout.widget.GridLayout
|
||||
import com.ray650128.gstreamer_demo_app.databinding.FragmentSplitViewBinding
|
||||
import com.ray650128.gstreamer_demo_app.dp
|
||||
@ -20,6 +21,8 @@ import kotlin.math.sqrt
|
||||
|
||||
class SplitViewFragment : Fragment() {
|
||||
|
||||
val viewModel: SplitViewModel by activityViewModels()
|
||||
|
||||
private var mPageNum: Int = 0
|
||||
private var splitMode = MainViewModel.PAGE_MODE_ONE
|
||||
private var streamType = VideoView.SUB_STREAM
|
||||
@ -52,6 +55,15 @@ class SplitViewFragment : Fragment() {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
initView()
|
||||
|
||||
viewModel.activePage.observe(viewLifecycleOwner) {
|
||||
if (it == null) return@observe
|
||||
if (it == this.mPageNum) {
|
||||
playAll()
|
||||
} else {
|
||||
stopAll()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
@ -62,9 +74,11 @@ class SplitViewFragment : Fragment() {
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
MainScope().launch {
|
||||
delay(1000)
|
||||
playAll()
|
||||
if (viewModel.activePage.value == mPageNum) {
|
||||
MainScope().launch {
|
||||
delay(splitMode * 300L)
|
||||
playAll()
|
||||
}
|
||||
}
|
||||
Log.d("${TAG}_$mPageNum", "onResume()")
|
||||
}
|
||||
@ -140,8 +154,9 @@ class SplitViewFragment : Fragment() {
|
||||
}
|
||||
return@setOnClickListener
|
||||
}
|
||||
stopAll()
|
||||
MainScope().launch {
|
||||
stopAll()
|
||||
delay(splitMode * 300L)
|
||||
val item = data[position]
|
||||
val bundle = Bundle().apply {
|
||||
//putInt(MonitoringActivity.BUNDLE_DEVICE_ID, item.id)
|
||||
@ -175,9 +190,10 @@ class SplitViewFragment : Fragment() {
|
||||
videoViews[index].play()
|
||||
delay(300)
|
||||
}
|
||||
}
|
||||
}.start()
|
||||
|
||||
fun stopAll() = MainScope().launch(Dispatchers.Main) {
|
||||
if (videoViews.isEmpty()) return@launch
|
||||
for (index in data.indices) {
|
||||
videoViews[index].stopRetryCount()
|
||||
videoViews[index].stop()
|
||||
|
||||
@ -0,0 +1,10 @@
|
||||
package com.ray650128.gstreamer_demo_app.ui.mainScreen
|
||||
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
|
||||
class SplitViewModel : ViewModel() {
|
||||
val activePage: MutableLiveData<Int> by lazy {
|
||||
MutableLiveData()
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user