Search code examples
androidcontainersandroid-videoviewoverlap

Android Studio - How to avoid VideoView overflowing its (View) container?


I have 3 VideoViews in a horizontal Linearlayout and I want each one to play a different video, each video will only show a part of its Original Video (therefore each video is contained in a LinearLayout that limits what should be shown from 150dp to 150dp). The problem is that the videos overlap. As an example I show the code where I only fill the VideoView Central but it invades the regions of the other VideoViews, I have tried everything but I can't make them not invade each other. Any idea how to fix it. Note: It is imperative that the VideoView is larger than its 150x150dp LinearLayout container.

In this image i show Actual Error and Correct Result wanted

import android.media.MediaPlayer
import android.net.Uri
import android.os.Bundle
import android.util.DisplayMetrics
import android.widget.MediaController
import com.uncdf.unitlife.R
import com.uncdf.unitlife.util.ActBase
import com.uncdf.unitlife.util.TestCustomVideoView
import com.uncdf.unitlife.util.Utiles

class MainActivity : ActBase() {

override fun onCreate(savedInstanceState: Bundle?) {
    try{
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        init()
    }catch (ex: java.lang.Exception){
        Utiles.manageViewException(ex, baseContext, getEventCode("0001"))
    }
}

fun init(){

    initVideo(R.id.viewVideoX2)
    
}

fun initVideo(idViewVideo : Int){
    val video = findViewById<TestCustomVideoView>(idViewVideo)

    video?.setMediaController(MediaController(this));
    video?.setOnPreparedListener(MediaPlayer.OnPreparedListener() {
        // it.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING)
        it.isLooping = true
        // itemInfo.videoMediaPlayer = it
    })

    val metrics = DisplayMetrics()
    windowManager.defaultDisplay.getMetrics(metrics)
    video?.setContainer(736, 412, true)

    // "http://www.demonuts.com/Demonuts/smallvideo.mp4"
    var rutaVideo =  "android.resource://" + packageName + "/" + R.raw.smallvideo;
    video?.setVideoURI(Uri.parse(rutaVideo))
    video?.requestFocus()
    video?.seekTo(5)
    video.setZOrderMediaOverlay(false)
    // viewVideoFull?.start()

}
}



import android.content.Context;
import android.graphics.Canvas;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.util.AttributeSet;
import android.util.Size;
import android.widget.VideoView;

public class TestCustomVideoView extends VideoView {

private Size finalVideoSize = new Size(0,0);
private boolean isOut = false;

public TestCustomVideoView(Context context) {
    super(context);
}

public TestCustomVideoView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public TestCustomVideoView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public void setContainer(int width, int height, boolean outContainer){
    this.finalVideoSize = new Size(width, height);
    setMeasuredDimension(finalVideoSize.getWidth(), finalVideoSize.getHeight());
}

@Override
public void setVideoURI(Uri uri) {

    super.setVideoURI(uri);

    if(finalVideoSize.getWidth() > 0 && finalVideoSize.getHeight() > 0)
        setMeasuredDimension(finalVideoSize.getWidth(), finalVideoSize.getHeight());
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
    if(finalVideoSize.getWidth() > 0 && finalVideoSize.getHeight() > 0)
        setMeasuredDimension(finalVideoSize.getWidth(), finalVideoSize.getHeight());
    else
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}
}


    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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=".temporal.MainActivity">

<LinearLayout
    android:id="@+id/viewContainer"
    android:layout_width="wrap_content"
    android:layout_height="150dp"
    android:orientation="horizontal"

    android:background="@android:color/holo_orange_light"

    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintWidth_percent=".9">

    <LinearLayout
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:gravity="center"

        android:background="@android:color/holo_green_light"
        android:layout_marginLeft="20dp">

        <com.uncdf.unitlife.util.TestCustomVideoView
            android:id="@+id/viewVideoX1"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_gravity="center"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:gravity="center"

        android:background="@android:color/holo_green_light"
        android:layout_marginLeft="20dp">

        <com.uncdf.unitlife.util.TestCustomVideoView
            android:id="@+id/viewVideoX2"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_gravity="center"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:gravity="center"

        android:background="@android:color/holo_green_light"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp">

        <com.uncdf.unitlife.util.TestCustomVideoView
            android:id="@+id/viewVideoX3"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_gravity="center"/>

    </LinearLayout>

</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • Hey i stumbled upon something similar and the issue i found was with VideoView natively extends surfaceView. So i opted out to try playing video with textureView and here is the library i used and it helped me to deal with the error .ScalableVideoView

    Let me know if you face any other issue while implementing this Library