1

I use a tutorial video inside VideoView. But video is not fill VideoView while playing and has black part in VideoView because that has Rectangular view. I can not solution for it. I tried to cover VideoView by CardView and make cardRadius. But It's not remove all black part as properly. How can do this? Can not make VideoView background radius?

enter image description here

VideoFragment.kt

private fun initVideo() { simpleVideoView = binding.videoViewTutorial simpleVideoView?.setVideoURI( Uri.parse("android.resource://" + requireActivity().packageName + "/" + R.raw.tutorial) ) simpleVideoView?.requestFocus() simpleVideoView?.start() } 

fragment_video.xml

<?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" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools"> <androidx.fragment.app.FragmentContainerView android:id="@+id/tabContainerUser" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:defaultNavHost="true" /> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottomNavigationView" android:layout_width="0dp" android:layout_height="70dp" android:layout_gravity="bottom" android:layout_margin="30dp" android:background="@drawable/background_white_gray_stroke_60dp_radius" app:itemIconSize="27dp" app:labelVisibilityMode="unlabeled" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:menu="@menu/bottom_nav_menu_user" /> <ImageView android:id="@+id/ivAskAdvise" android:layout_width="50dp" android:layout_height="50dp" android:translationZ="999dp" android:src="@drawable/ic_black_plus_yellow_bg" app:layout_constraintBottom_toBottomOf="@+id/bottomNavigationView" app:layout_constraintEnd_toEndOf="@+id/bottomNavigationView" app:layout_constraintStart_toStartOf="@+id/bottomNavigationView" app:layout_constraintTop_toTopOf="@+id/bottomNavigationView" app:layout_constraintVertical_bias="0.63" tools:ignore="ContentDescription" /> <View android:id="@+id/viewBlackBg" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/black_0_70" android:visibility="gone" android:translationZ="999dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:visibility="visible" /> <!-- <androidx.cardview.widget.CardView android:id="@+id/cardViewVideo" android:layout_width="0dp" android:layout_height="0dp" app:cardCornerRadius="70dp" app:cardBackgroundColor="@color/transparent" android:visibility="gone" android:translationZ="999dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHeight_percent="0.80" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintWidth_percent="0.80" tools:visibility="visible">--> <VideoView android:id="@+id/videoViewTutorial" android:layout_width="0dp" android:layout_height="0dp" android:translationZ="999dp" android:visibility="gone" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHeight_percent="0.80" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintWidth_percent="0.80" tools:visibility="visible"/> <!--</androidx.cardview.widget.CardView>--> <TextView android:id="@+id/tvSkipTutorial" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:fontFamily="@font/mulish_regular" android:text="@string/skip" android:textColor="@color/white" android:textSize="16sp" android:visibility="gone" android:translationZ="999dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/videoViewTutorial" tools:visibility="visible" /> </androidx.constraintlayout.widget.ConstraintLayout> 

3 Answers 3

0

Try this

vSimpleVideoView.setBackgroundColor(Color.TRANSPARENT); 
Sign up to request clarification or add additional context in comments.

1 Comment

I tried before already but I just tried again and it's not working.
0

Please try one more time

<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <VideoView android:id="@+id/videoViewTutorialN" android:layout_width="0dp" android:layout_height="0dp" android:visibility="gone" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHeight_percent="0.80" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintWidth_percent="0.80"/> <View android:id="@+id/videoViewOverlayN" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent" android:visibility="gone"/> </FrameLayout> 

Java file side - when you want to show the VideoViewT then

videoViewTutorialN.setVisibility(View.VISIBLE); videoViewOverlayN.setVisibility(View.VISIBLE); 

otherwise

videoViewTutorialN.setVisibility(View.GONE); videoViewOverlayN.setVisibility(View.GONE); 

1 Comment

How to use VideoView with constraint layout attributes inside FrameLayout? It does not work.
0

Try this VideoSurface custom view:

import android.content.Context import android.graphics.SurfaceTexture import android.media.MediaPlayer import android.net.Uri import android.util.AttributeSet import android.view.Surface import android.view.TextureView import android.view.TextureView.SurfaceTextureListener /** * A [TextureView]-based custom video surface that wraps [MediaPlayer] for * lightweight video playback. This class allows playing looping videos inside * a Compose `AndroidView` or traditional Views without requiring ExoPlayer. * * Usage: * ``` * val videoSurface = VideoSurface(context).apply { * setSource(videoUri) * setOnPreparedListener { mp -> * // do something when ready, e.g. hide loader * } * setOnCompletionListener { * // handle completion if looping is disabled * } * setOnErrorListener { mp, what, extra -> * // handle error * true * } * } * ``` * * Notes: * - Releases its [MediaPlayer] automatically on [onDetachedFromWindow]. * - You must call [setSource] before the surface is available. * - Starts playback automatically once prepared. */ class VideoSurface @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyle: Int = 0 ) : TextureView(context, attrs, defStyle), SurfaceTextureListener { private val mediaPlayer = MediaPlayer() private var source: Uri? = null private var completionListener: MediaPlayer.OnCompletionListener? = null private var preparedListener: MediaPlayer.OnPreparedListener? = null private var errorListener: MediaPlayer.OnErrorListener? = null init { surfaceTextureListener = this } /** * Sets the video source [Uri]. * * Must be called before the surface is available for playback to start. */ fun setSource(source: Uri?) { this.source = source } /** * Registers a listener to be notified when playback completes. */ fun setOnCompletionListener(listener: MediaPlayer.OnCompletionListener?) { completionListener = listener } /** * Registers a listener to be notified when the video is prepared. */ fun setOnPreparedListener(listener: MediaPlayer.OnPreparedListener?) { preparedListener = listener } /** * Registers a listener to be notified when an error occurs during playback. */ fun setOnErrorListener(listener: MediaPlayer.OnErrorListener?) { errorListener = listener } /** * Releases the [MediaPlayer] when the view is detached. */ override fun onDetachedFromWindow() { mediaPlayer.reset() super.onDetachedFromWindow() } override fun onSurfaceTextureAvailable( surfaceTexture: SurfaceTexture, width: Int, height: Int ) { val surface = Surface(surfaceTexture) try { mediaPlayer.apply { setOnCompletionListener(completionListener) setOnErrorListener(errorListener) setSurface(surface) isLooping = true source?.let { setDataSource(context, it) } setOnPreparedListener { mp -> start() preparedListener?.onPrepared(mp) } prepareAsync() } } catch (e: Exception) { e.printStackTrace() mediaPlayer.reset() } } override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture, width: Int, height: Int) = Unit override fun onSurfaceTextureDestroyed(surface: SurfaceTexture): Boolean { surface.release() return true } override fun onSurfaceTextureUpdated(surface: SurfaceTexture) = Unit } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.