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