1.加入TextureView支援

2.將取得畫面寬高的計算方式改成不使用baseView.post
This commit is contained in:
Raymond Yang
2023-05-22 09:43:53 +08:00
parent 02a26445be
commit 8449706b64
5 changed files with 179 additions and 65 deletions
@@ -0,0 +1,94 @@
package com.ray650128.gstreamer_demo_app
import android.content.Context
import android.os.Build
import android.util.DisplayMetrics
import android.view.WindowManager
/**
* 螢幕參數工具類別
* @author Raymond Yang
*/
class DisplayUtils(private var context: Context) {
private val TAG = DisplayUtils::class.java.simpleName
/**
* 取得螢幕寬度
* @return 螢幕寬度值
*/
fun getScreenWidth(): Int {
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val windowMetrics = windowManager.currentWindowMetrics
windowMetrics.bounds.width()
} else {
val metric = DisplayMetrics()
@Suppress("DEPRECATION")
windowManager.defaultDisplay.getMetrics(metric)
metric.widthPixels
}
}
/**
* 取得螢幕高度
* @return 螢幕高度值
*/
fun getScreenHeight(): Int {
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val windowMetrics = windowManager.currentWindowMetrics
windowMetrics.bounds.height()
} else {
val metric = DisplayMetrics()
@Suppress("DEPRECATION")
windowManager.defaultDisplay.getMetrics(metric)
metric.heightPixels
}
}
/**
* 取得狀態列高度
* @return 狀態列高度值
*/
fun getStatusBarHeight(): Int {
var result = 0
val resourceId: Int = context.resources.getIdentifier(
"status_bar_height",
"dimen",
"android"
)
if (resourceId > 0) {
result = context.resources.getDimensionPixelSize(resourceId)
}
return result
}
/**
* 取得導航列高度
* @return 導航列高度值
*/
fun getNavigationBarHeight(): Int {
val resources = context.resources
val resourceId: Int = resources.getIdentifier("navigation_bar_height", "dimen", "android")
return if (resourceId > 0) {
resources.getDimensionPixelSize(resourceId)
} else 0
}
/**
* 取得視窗範圍高度
* @return 視窗範圍高度值
*/
fun getWindowHeight(): Int {
val screen = getScreenHeight()
val statusBar = getStatusBarHeight()
val navBar = getNavigationBarHeight()
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
screen - statusBar - navBar
} else {
screen
}
}
}
@@ -6,10 +6,12 @@ import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.gridlayout.widget.GridLayout
import com.ray650128.gstreamer_demo_app.Constants
import com.ray650128.gstreamer_demo_app.DisplayUtils
import com.ray650128.gstreamer_demo_app.R
import com.ray650128.gstreamer_demo_app.databinding.FragmentSplitViewBinding
import com.ray650128.gstreamer_demo_app.dp
import com.ray650128.gstreamer_demo_app.model.Device
@@ -22,8 +24,6 @@ import kotlin.math.sqrt
class SplitViewFragment : Fragment() {
val viewModel: SplitViewModel by activityViewModels()
private var mPageNum: Int = 0
private var splitMode = Constants.SPLIT_MODE_SINGLE
private var streamType = VideoView.SUB_STREAM
@@ -56,18 +56,6 @@ class SplitViewFragment : Fragment() {
super.onViewCreated(view, savedInstanceState)
initView()
/*viewModel.activePage.observe(viewLifecycleOwner) {
if (it == null) return@observe
if (it == this.mPageNum) {
MainScope().launch {
//delay(1000)
//playAll()
}
} else {
//stopAll()
}
}*/
}
override fun onPause() {
@@ -90,6 +78,7 @@ class SplitViewFragment : Fragment() {
}
private fun initView() {
val displayUtil = DisplayUtils(requireContext())
// 生成 VideoView 分割畫面
binding.apply {
val maxRow = sqrt(splitMode.toFloat()).toInt()
@@ -99,6 +88,19 @@ class SplitViewFragment : Fragment() {
baseView.rowCount = maxRow
baseView.columnCount = maxCol
val cellWidth: Int
val cellHeight: Int
when (splitMode) {
Constants.SPLIT_MODE_SINGLE -> {
cellWidth = (displayUtil.getScreenWidth() / maxRow)
cellHeight = (cellWidth * 0.5625).toInt()
}
else -> {
cellWidth = (displayUtil.getScreenWidth() / maxRow) - maxRow.dp
cellHeight = (cellWidth * 0.5625).toInt() - maxCol.dp
}
}
for (col in 0 until maxCol) {
for (row in 0 until maxRow) {
val videoView = VideoView(requireContext())
@@ -108,6 +110,9 @@ class SplitViewFragment : Fragment() {
marginEnd = 0.dp
marginStart = 0.dp
width = cellWidth
height = cellHeight
// 調整間距
when (splitMode) {
Constants.SPLIT_MODE_FOUR -> {
@@ -154,35 +159,15 @@ class SplitViewFragment : Fragment() {
videoViews.add(videoView)
}
}
baseView.post {
// 根據分割數量決定子項目的寬度
val cellWidth: Int
val cellHeight: Int
when (splitMode) {
Constants.SPLIT_MODE_SINGLE -> {
cellWidth = (baseView.width / maxRow)
cellHeight = (baseView.height / maxCol)
}
else -> {
cellWidth = (baseView.width / maxRow) - maxRow.dp
cellHeight = (baseView.height / maxCol) - maxCol.dp
}
}
videoViews.forEach { videoView ->
videoView.layoutParams?.width = cellWidth
videoView.layoutParams?.height = cellHeight
}
}
}
if (isClickable) {
for (position in videoViews.indices) {
videoViews[position].setOnClickListener {
if (position >= data.size) return@setOnClickListener
if (!videoViews[position].isPlaying) {
/*if (!videoViews[position].isPlaying) {
return@setOnClickListener
}
}*/
stopAll()
val item = data[position]
val bundle = Bundle().apply {
@@ -214,31 +199,32 @@ class SplitViewFragment : Fragment() {
fun playAll() = MainScope().launch(Dispatchers.Main) {
if (videoViews.isEmpty()) return@launch
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)
videoViews.forEach { videoView ->
if (videoView.isReady) {
videoView.resetRetryCount()
videoView.play()
//delay(300)
}
}
}.start()
fun stopAll() = MainScope().launch(Dispatchers.Main) {
if (videoViews.isEmpty()) return@launch
for (index in data.indices) {
videoViews[index].stopRetryCount()
if (!videoViews[index].isPlaying || videoViews[index].isLoading) continue
videoViews[index].pause()
//delay(300)
fun stopAll() {
if (videoViews.isEmpty()) return
videoViews.forEach { videoView ->
videoView.stopRetryCount()
if (videoView.isPlaying || !videoView.isLoading) {
videoView.pause()
}
}
}.start()
}
fun destroyAll() = MainScope().launch(Dispatchers.Main) {
if (videoViews.isEmpty()) return@launch
for (index in data.indices) {
//videoViews[index].destroy()
videoViews[index].destroySurface()
fun destroyAll() {
if (videoViews.isEmpty()) return
videoViews.forEach { videoView ->
//videoView.destroy()
videoView.destroySurface()
}
}.start()
}
companion object {
private val TAG = SplitViewFragment::class.java.simpleName
@@ -9,6 +9,7 @@ import android.util.Log
import android.view.LayoutInflater
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.view.TextureView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import com.hisharp.gstreamer_player.GstCallback
@@ -62,7 +63,7 @@ class VideoView : ConstraintLayout, GstCallback {
}
}
private val videoView: SurfaceView by lazy { view.videoView }
private val videoView: TextureView by lazy { view.videoView }
private lateinit var gstLibrary: GstLibrary
@@ -75,11 +76,13 @@ class VideoView : ConstraintLayout, GstCallback {
view = ItemVideoViewBinding.inflate(layoutInflater, this, true)
view.baseView.clipToOutline = true
//videoView.holder.addCallback(this)
gstLibrary = GstLibrary(context)
gstLibrary.setSurfaceHolder(videoView.holder)
gstLibrary.setTextureView(videoView)
gstLibrary.setOnStatusChangeListener(this)
//videoView.holder.addCallback(this)
//gstLibrary.setSurfaceHolder(videoView.holder)
// View 預設狀態
view.textDeviceName.isVisible = false
isPlaying = false
+1 -1
View File
@@ -7,7 +7,7 @@
android:background="@drawable/bg_video_view"
android:outlineProvider="background">
<SurfaceView
<TextureView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"