mirror of
https://github.com/divkit/divkit.git
synced 2026-06-06 20:07:59 +00:00
supported preview in DivVideo
This commit is contained in:
@@ -48,13 +48,13 @@ class ExoDivPlayer(
|
||||
}
|
||||
|
||||
override fun onPlaybackStateChanged(state: Int) {
|
||||
if (state != Player.STATE_BUFFERING && state != Player.STATE_ENDED) return
|
||||
if (state == Player.STATE_ENDED) needToRenderFrameExplicitly = true
|
||||
|
||||
observers.forEach {
|
||||
when (state) {
|
||||
Player.STATE_BUFFERING -> it.onBuffering()
|
||||
Player.STATE_ENDED -> it.onEnd()
|
||||
Player.STATE_READY -> it.onReady()
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,11 @@ interface DivPlayer {
|
||||
*/
|
||||
fun onPlay() = Unit
|
||||
|
||||
/**
|
||||
* Called when player video is ready to show.
|
||||
*/
|
||||
fun onReady() = Unit
|
||||
|
||||
/**
|
||||
* Called when playback has been paused by an action.
|
||||
*/
|
||||
|
||||
@@ -1,5 +1,13 @@
|
||||
package com.yandex.div.core.view2.divs
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.graphics.Color
|
||||
import android.util.Base64
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.FrameLayout
|
||||
import android.widget.ImageView
|
||||
import com.yandex.div.core.DivActionHandler
|
||||
import com.yandex.div.core.dagger.DivScope
|
||||
import com.yandex.div.core.expression.variables.TwoWayIntegerVariableBinder
|
||||
@@ -45,7 +53,20 @@ internal class DivVideoBinder @Inject constructor(
|
||||
)
|
||||
|
||||
val playerView = divView.div2Component.divVideoFactory.makePlayerView(view.context)
|
||||
val preview = div.createPreview(resolver)
|
||||
val previewImageView: ImageView = ImageView(view.context).apply {
|
||||
layoutParams = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
|
||||
scaleType = ImageView.ScaleType.FIT_CENTER
|
||||
if (preview != null) {
|
||||
visibility = View.VISIBLE
|
||||
setImageBitmap(preview)
|
||||
} else {
|
||||
visibility = View.INVISIBLE
|
||||
}
|
||||
setBackgroundColor(Color.TRANSPARENT)
|
||||
}
|
||||
view.addView(playerView)
|
||||
view.addView(previewImageView)
|
||||
|
||||
playerView.attach(player)
|
||||
|
||||
@@ -70,6 +91,10 @@ internal class DivVideoBinder @Inject constructor(
|
||||
divView.let { divActionHandler.handleAction(divAction, it) }
|
||||
}
|
||||
}
|
||||
|
||||
override fun onReady() {
|
||||
previewImageView.visibility = View.INVISIBLE
|
||||
}
|
||||
}
|
||||
player.addObserver(playerListener)
|
||||
|
||||
@@ -120,3 +145,9 @@ fun DivVideo.createSource(resolver: ExpressionResolver): List<DivVideoSource> {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun DivVideo.createPreview(resolver: ExpressionResolver): Bitmap? {
|
||||
val base64String = preview?.evaluate(resolver) ?: return null
|
||||
val imageBytes = Base64.decode(base64String, Base64.DEFAULT)
|
||||
return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
|
||||
}
|
||||
|
||||
+2
-2
@@ -97,8 +97,8 @@ internal class DivVideoView @JvmOverloads constructor(
|
||||
}
|
||||
|
||||
fun getPlayerView(): DivPlayerView? {
|
||||
if (this.childCount > 1) {
|
||||
KAssert.fail { "More than one player view inside DivVideo" }
|
||||
if (this.childCount > 2) {
|
||||
KAssert.fail { "Too many children in DivVideo" }
|
||||
}
|
||||
this.getChildAt(0)?.let {
|
||||
if (it !is DivPlayerView) {
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user