Home » Android » How to play a transparent Video in Android?-Exceptionshub

How to play a transparent Video in Android?-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I have a video with a transparent background that I want to use in my Android project.
I found this library but it’s too old and the performance is not so good.
How can I achieve my goal?

How to&Answers:

You can use videoview like this;

 <VideoView
            android:id="@+id/video_background"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@+id/bottom_navigation_bar"
            app:layout_constraintEnd_toEndOf="parent"
            android:background="@color/transparent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

in your activity;

videoBackgroundView = findViewById(R.id.video_background)
val path = "android.resource://com.example.xxxxx" + "/" + R.raw.video
videoBackgroundView?.setVideoPath(path)
videoBackgroundView?.setOnPreparedListener { mp ->
    mp.isLooping = true
    videoBackgroundView?.start()
}