I am using media3 in Jetpack Compose. I am struggling to implement pip. When the pip is shown the video is not resized properly. I am attaching the screenshots below. .
Here is my code.
Box(
modifier = modifier
) {
PipActionReceiver(exoPlayer, isInPictureInPictureMode)
AndroidView(
factory = {
playerView.also {
it.requestFocus()
}
},
update = {
it.player = exoPlayer.also { player ->
player.addListener(exoPlayerListener)
}
it.setShowBuffering(PlayerView.SHOW_BUFFERING_WHEN_PLAYING)
it.setControllerAnimationEnabled(false)
it.setShowFastForwardButton(true)
it.setShowRewindButton(true)
it.setShowNextButton(false)
it.setShowPreviousButton(false)
it.showController()
it.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FILL)
it.setControllerVisibilityListener(PlayerView.ControllerVisibilityListener { isVisible ->
isControllerVisible = isVisible == View.VISIBLE
Log.d(LogTag,"ControllerVisibilityListener $isControllerVisible")
it.subtitleView?.adjustSubtitlePosition(context, isControllerVisible, it.height)
})
it.artworkDisplayMode = ARTWORK_DISPLAY_MODE_FILL
it.setShowSubtitleButton(true)
it.setErrorMessageProvider(PlayerErrorMessageProvider(context))
},
modifier = Modifier
.fillMaxSize()
.zIndex(1f)
.focusable(
enabled = true,
interactionSource = interactionSource
)
.focusRequester(focusRequester)
.onGloballyPositioned {
playerRect = it
.boundsInWindow()
.toAndroidRect()
}
.then(
if (DeviceType.isTv(context))
Modifier.onKeyEvent { keyEvent ->
Log.d(LogTag, "onKeyEvent in main player $keyEvent")
// Detecting back button press
if (keyEvent.nativeKeyEvent.keyCode == KeyEvent.KEYCODE_BACK && keyEvent.nativeKeyEvent.action == KeyEvent.ACTION_DOWN) {
when {
currentDialogType != TrackSettingsDialogType.None -> {
currentDialogType = TrackSettingsDialogType.None
}
else -> {
releasePlayer()
onGotoBackScreen()
}
}
true // Consuming the back press event
} else if (keyEvent.nativeKeyEvent.keyCode == KeyEvent.KEYCODE_DPAD_CENTER && keyEvent.nativeKeyEvent.action == KeyEvent.ACTION_DOWN) {
if (isControllerVisible && playerView.findTimeBar().isFocused) {
playerView.togglePlayPause()
} else if (!isControllerVisible) {
playerView.apply {
showController()
findTimeBar().requestFocus()
playerView.togglePlayPause()
}
}
true
} else {
// Handle other key events, including showing the controller when necessary
if (isControllerVisible) {
playerView.dispatchKeyEvent(keyEvent.nativeKeyEvent)
} else {
playerView.showController()
playerView.findTimeBar().requestFocus()
true//Don't send key press event
}
}
}
else
Modifier
),
)
And for updating pip params
fun updatePipParams(
context: Context,
videoInfo: VideoInfo,
videoViewBounds: Rect,
isPlaying: Boolean,
): PictureInPictureParams? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
PictureInPictureParams.Builder()
.setAspectRatio(Rational(16, 9))
.also {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
it.setExpandedAspectRatio(Rational(4, 3))
it.setTitle(videoInfo.title)
it.setSubtitle(videoInfo.artistName)
it.setSeamlessResizeEnabled(true)
it.setActions(
listOfRemoteActions(isPlaying, context)
)
}
context.findActivity()?.setPictureInPictureParams(it.build())
}
.build()
} else null
}
And the usage
setOnClickListener {
updatePipParams(context, videoInfo, playerRect, exoPlayer.isPlaying)?.let { params ->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (activity.enterPictureInPictureMode(params)) {
Log.d(LogTag, "Entered PiP mode")
findControllerLayout().isVisible(false)
onPresentationChanged(PlayerPresentationModes.PIP)
} else {
Log.e(LogTag, "Failed to enter PiP mode")
}
}
}
}
I have also tried switching the Zoom,fill and fit. Nothing helped. How can I get it fixed to render the video in PIP in both shrinked and extended window with properly scaled and resized video .