I need to take a screenshot in ExoPlayer, the root view of which is PlayerView. enter image description here
I figured it out and found a simple solution to how to get a Bitmap from the view, partly this is the answer to my question
val bitmap = videoView.drawToBitmap()
UPDATED 27.04.2023
DrawToBitmap() in my case produced a black screenshot. I haven't found anything better than specifying surface_type as surfaceview
app:surface_type="surface_view"
Then in the code I convert the player view to a surface view
binding.apply {
val videViewSurface = videoView.videoSurfaceView as SurfaceView
ScreenshotUtils.takeScreenshotWithPixelCopy(videViewSurface) { bitmap ->...}
}
Then I use Pixel Copy to create the final bitmap
fun takeScreenshotWithPixelCopy(videoView: SurfaceView, callback: (Bitmap?) -> Unit) {
val bitmap: Bitmap = Bitmap.createBitmap(
videoView.width,
videoView.height,
Bitmap.Config.ARGB_8888
)
try {
val handlerThread = HandlerThread("PixelCopier")
handlerThread.start()
PixelCopy.request(
videoView, bitmap,
{ copyResult ->
if (copyResult == PixelCopy.SUCCESS) {
callback(bitmap)
}
handlerThread.quitSafely()
},
Handler(handlerThread.looper)
)
} catch (e: IllegalArgumentException) {
callback(null)
e.printStackTrace()
}
}