將VideoView的retryCount封裝

This commit is contained in:
Raymond Yang 2023-02-01 10:38:22 +08:00
parent 90344d14b0
commit 792ebc70c2
2 changed files with 15 additions and 13 deletions

View File

@ -132,7 +132,7 @@ class SplitViewFragment : Fragment() {
if (position >= data.size) return@setOnClickListener
if (!videoViews[position].isPlaying) {
if (!videoViews[position].isLoading) {
videoViews[position].retryCount = 0
videoViews[position].resetRetryCount()
videoViews[position].play()
}
return@setOnClickListener
@ -166,7 +166,7 @@ class SplitViewFragment : Fragment() {
fun playAll() = MainScope().launch(Dispatchers.Main) {
if (videoViews.isEmpty()) return@launch
for (index in data.indices) {
videoViews[index].retryCount = 0
videoViews[index].resetRetryCount()
videoViews[index].play()
delay(300)
}
@ -174,6 +174,7 @@ class SplitViewFragment : Fragment() {
fun stopAll() = MainScope().launch(Dispatchers.Main) {
for (index in data.indices) {
videoViews[index].stopRetryCount()
videoViews[index].stop()
delay(300)
}

View File

@ -47,7 +47,7 @@ class VideoView : ConstraintLayout, SurfaceHolder.Callback, GstCallback {
field = value
}
var retryCount = 0
private var retryCount = 0
private var retryRunnable = object : Runnable {
override fun run() {
play()
@ -99,26 +99,27 @@ class VideoView : ConstraintLayout, SurfaceHolder.Callback, GstCallback {
}
fun play() {
/*if (retryCount == RETRY_OFF) {
return
}*/
videoView.postInvalidate()
gstLibrary.play()
}
fun stop() {
isPlaying = false
retryCount = RETRY_OFF
if (this::gstLibrary.isInitialized) {
gstLibrary.stop()
}
}
fun resetRetryCount() {
retryCount = 0
}
fun stopRetryCount() {
retryCount = RETRY_OFF
}
override fun surfaceCreated(holder: SurfaceHolder) {
Log.d("${TAG}_$tag", "Surface created: " + holder.surface)
if (this::gstLibrary.isInitialized) {
gstLibrary.setSurfaceHolder(holder)
}
}
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
@ -130,7 +131,7 @@ class VideoView : ConstraintLayout, SurfaceHolder.Callback, GstCallback {
override fun surfaceDestroyed(p0: SurfaceHolder) {
Log.d("${TAG}_$tag", "Surface destroyed")
// TODO: 如果呼叫 releaseSurface(),會閃退
// TODO: 呼叫 releaseSurface() 會閃退,目前仍在釐清時機點
if (this::gstLibrary.isInitialized) {
gstLibrary.releaseSurface()
}
@ -162,7 +163,7 @@ class VideoView : ConstraintLayout, SurfaceHolder.Callback, GstCallback {
MSG_PLAY -> {
isLoading = false
isPlaying = true
retryCount = 0
resetRetryCount()
}
MSG_BUFFERING -> {
isLoading = true
@ -173,7 +174,7 @@ class VideoView : ConstraintLayout, SurfaceHolder.Callback, GstCallback {
mHandler.post(retryRunnable)
retryCount++
} else {
retryCount = RETRY_OFF
stopRetryCount()
//Log.e("${TAG}_$tag", "Retry count = 5, stopped retry...")
}
}