Omkar Amberkar
08/01/2024, 3:54 PMRobert C
08/01/2024, 4:06 PMOmkar Amberkar
08/01/2024, 4:12 PMRobert C
08/01/2024, 4:19 PMOmkar Amberkar
08/01/2024, 4:20 PMOmkar Amberkar
08/01/2024, 4:22 PM@Composable
private fun BoxScope.PlayerVideo(
modifier: Modifier = Modifier,
orientationValue: OrientationValue,
resizeValue: ResizeValue,
videoAspectRatioValue: Float,
onSurfaceViewAttachedToWindow: (SurfaceView) -> Unit,
onSurfaceViewDetachedFromWindow: (SurfaceView) -> Unit,
) {
var size by remember { mutableStateOf(IntSize.Zero) }
AndroidView(
modifier = modifier
.align(Alignment.Center)
.fillMaxSize()
.onSizeChanged {
Logger.d("TEST", "Size Changed")
size = it
}
.border(2.dp, Color.Red),
factory = { context ->
Logger.d("TEST", "Create View")
AspectRatioLayout(context).apply {
layoutParams = FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)
setAspectRatio(videoAspectRatioValue)
setResizeMode(RESIZE_MODE_FIT)
addView(SurfaceView(context).apply {
layoutParams = FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)
addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
override fun onViewAttachedToWindow(view: View) =
onSurfaceViewAttachedToWindow(view as SurfaceView)
override fun onViewDetachedFromWindow(view: View) =
onSurfaceViewDetachedFromWindow(view as SurfaceView)
})
})
}
},
update = { view ->
Logger.d("TEST", "Update View")
val aspectRatioLayout = view as AspectRatioLayout
aspectRatioLayout.setAspectRatio(videoAspectRatioValue)
// TODO: change orientation to window size class
when (orientationValue) {
OrientationValue.LANDSCAPE -> when (resizeValue) {
ResizeValue.FIT -> aspectRatioLayout.setResizeMode(RESIZE_MODE_FIT)
ResizeValue.ZOOM -> aspectRatioLayout.setResizeMode(RESIZE_MODE_ZOOM)
}
OrientationValue.PORTRAIT -> aspectRatioLayout.setResizeMode(RESIZE_MODE_FIXED_WIDTH)
}
aspectRatioLayout.layoutParams = FrameLayout.LayoutParams(size.width, size.height)
aspectRatioLayout.invalidate()
aspectRatioLayout.requestLayout()
aspectRatioLayout.getChildAt(0).layoutParams = FrameLayout.LayoutParams(size.width, size.height)
aspectRatioLayout.getChildAt(0).invalidate()
aspectRatioLayout.getChildAt(0).requestLayout()
}
)
}
Robert C
08/01/2024, 4:31 PMfun Composable() {
val context = LocalContext.current
val config = LocalConfiguration.current
val isPortrait = config.orientation == Configuration.ORIENTATION_PORTRAIT
Box(modifier = Modifier.size(if(isPortrait) 100.dp else 200.dp)) {
AndroidView(factory = {
val view = SurfaceView(context)
view.setBackgroundColor(ContextCompat.getColor(context, R.color.teal_200))
view.layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
view
})
}
}
Omkar Amberkar
08/01/2024, 4:55 PMOmkar Amberkar
08/01/2024, 7:06 PMval surfaceView = view.findViewById<SurfaceView>(R.id.surface_view)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
surfaceView.getRootSurfaceControl()?.let { rootSurfaceControl ->
val surfaceSyncGroup = SurfaceSyncGroup("exo-sync-b-334901521")
surfaceSyncGroup.add(rootSurfaceControl) { }
surfaceSyncGroup.markSyncReady()
surfaceView.invalidate()
rootSurfaceControl.applyTransactionOnDraw(SurfaceControl.Transaction())
}
}
the above helped me solve the problemOmkar Amberkar
08/01/2024, 7:07 PM