1.優化for loop

2.優化VideoView get/set
This commit is contained in:
Raymond Yang 2023-05-22 10:56:24 +08:00
parent 8449706b64
commit ba12fd3856
2 changed files with 27 additions and 32 deletions

View File

@ -162,14 +162,13 @@ class SplitViewFragment : Fragment() {
} }
if (isClickable) { if (isClickable) {
for (position in videoViews.indices) { videoViews.forEach { videoView ->
videoViews[position].setOnClickListener { videoView.setOnClickListener {
if (position >= data.size) return@setOnClickListener if (!videoView.isPlaying) {
/*if (!videoViews[position].isPlaying) {
return@setOnClickListener return@setOnClickListener
}*/ }
stopAll() stopAll()
val item = data[position] val item = videoView.data
val bundle = Bundle().apply { val bundle = Bundle().apply {
//putInt(MonitoringActivity.BUNDLE_DEVICE_ID, item.id) //putInt(MonitoringActivity.BUNDLE_DEVICE_ID, item.id)
//putInt(MonitoringActivity.BUNDLE_CHANNEL_ID, item.channelId) //putInt(MonitoringActivity.BUNDLE_CHANNEL_ID, item.channelId)
@ -189,27 +188,23 @@ class SplitViewFragment : Fragment() {
private fun setAllUrl() { private fun setAllUrl() {
for (index in data.indices) { for (index in data.indices) {
videoViews[index].setData(data[index]) videoViews[index].data = data[index]
videoViews[index].setTextVisible( videoViews[index].setTextVisible(
(splitMode != Constants.SPLIT_MODE_NINE && splitMode != Constants.SPLIT_MODE_SIXTEEN) (splitMode != Constants.SPLIT_MODE_NINE && splitMode != Constants.SPLIT_MODE_SIXTEEN)
) )
} }
} }
fun playAll() = MainScope().launch(Dispatchers.Main) { fun playAll() {
if (videoViews.isEmpty()) return@launch
delay(splitMode * Constants.CONF_DELAY_BASE_MILLIS)
videoViews.forEach { videoView -> videoViews.forEach { videoView ->
if (videoView.isReady) { if (videoView.isReady) {
videoView.resetRetryCount() videoView.resetRetryCount()
videoView.play() videoView.play()
//delay(300)
} }
} }
}.start() }
fun stopAll() { fun stopAll() {
if (videoViews.isEmpty()) return
videoViews.forEach { videoView -> videoViews.forEach { videoView ->
videoView.stopRetryCount() videoView.stopRetryCount()
if (videoView.isPlaying || !videoView.isLoading) { if (videoView.isPlaying || !videoView.isLoading) {
@ -219,7 +214,6 @@ class SplitViewFragment : Fragment() {
} }
fun destroyAll() { fun destroyAll() {
if (videoViews.isEmpty()) return
videoViews.forEach { videoView -> videoViews.forEach { videoView ->
//videoView.destroy() //videoView.destroy()
videoView.destroySurface() videoView.destroySurface()

View File

@ -35,7 +35,25 @@ class VideoView : ConstraintLayout, GstCallback {
private lateinit var view: ItemVideoViewBinding private lateinit var view: ItemVideoViewBinding
private var data: Device? = null var streamType: Int = MAIN_STREAM
var data: Device? = null
set(value) {
field = value
if (field == null) {
view.textDeviceName.isVisible = false
isPlaying = false
isLoading = false
return
}
this.tag = field?.deviceName
view.textDeviceName.text = field?.deviceName
view.textDeviceName.isVisible = true
val rtspUrl = this.data?.getStreamPath(streamType) ?: return // 如果 null 就不指派給 Gstreamer 了
gstLibrary.setTag(this.data!!.deviceName)
gstLibrary.setRtspUrl(rtspUrl)
Log.d("${TAG}_$tag", "Set device to: $field, rtspUrl = $rtspUrl")
}
var isReady: Boolean = false var isReady: Boolean = false
@ -95,23 +113,6 @@ class VideoView : ConstraintLayout, GstCallback {
} }
} }
fun setData(device: Device?, streamType: Int = SUB_STREAM) {
if (device == null) {
view.textDeviceName.isVisible = false
isPlaying = false
isLoading = false
return
}
this.data = device
this.tag = device.deviceName
view.textDeviceName.text = device.deviceName
view.textDeviceName.isVisible = true
val rtspUrl = this.data?.getStreamPath(streamType) ?: return // 如果 null 就不指派給 Gstreamer 了
gstLibrary.setTag(this.data!!.deviceName)
gstLibrary.setRtspUrl(rtspUrl)
Log.d("${TAG}_$tag", "Set device to: $device, rtspUrl = $rtspUrl")
}
fun setTextVisible(isVisible: Boolean) { fun setTextVisible(isVisible: Boolean) {
view.textDeviceName.isVisible = isVisible view.textDeviceName.isVisible = isVisible
} }