Prasanna Punekar
Prasanna Punekar

Reputation: 97

How to solve aspect ratio issue while implementing Picture-in-picture feature?

I am trying to include a PiP(Picture-in-picture) feature in an app. I am encountering the following error :

 Caused by: java.lang.IllegalArgumentException: enterPictureInPictureMode: Aspect ratio is too extreme (must be between 0.418410 and 2.390000).

I would like to know how do to solve this issue. I have tried different techniques by making changes in xml as well as my java file. None have helped my issue.

I am including my java as well as xml code for more clarity :

Java :

 Rational aspectRatio = new Rational(videoView.getWidth(), videoView.getHeight());
 pictureInPictureParamsBuilder.setAspectRatio(aspectRatio).build();
 enterPictureInPictureMode(pictureInPictureParamsBuilder.build());

XML :

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.courses.oustchat.VideoPipActivity">
    <VideoView
        android:id="@+id/pipvideoview"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_margin="4dp"
        android:adjustViewBounds="true"/>
</RelativeLayout>

Pls. do provide a solution for this.

Thanks in advance.

Upvotes: 2

Views: 3439

Answers (1)

Hussnain Haidar
Hussnain Haidar

Reputation: 2258

This error happen because videoView.getWidth and .getheight method gives value more than pip mode support as exception saying the aspect ratio you set is more than pip mode support. Let's leave it to android by not setting ratio by yourself

pictureInPictureParamsBuilder.build();
enterPictureInPictureMode(pictureInPictureParamsBuilder.build());

or if you want to set custom than do like below but your ratio must be between 0.418410 and 2.390000:

Rational aspectRatio = new Rational(192, 108);
pictureInPictureParamsBuilder.setAspectRatio(aspectRatio).build();
enterPictureInPictureMode(pictureInPictureParamsBuilder.build());

Upvotes: 5

Related Questions