How To Play Video In Android Programmatically
Last Updated : Jul 1, 2023
In this article we will show you the solution of how to play video in android programmatically, the 3GP and MP4 file types are supported by the VideoView widget, which can play video files.
You may play media files from both your local storage and the Internet through VideoView.
Below is a sample GIF that will help you understand what this post will cover.
Utilize the VideoView and MediaController classes to create an application called SimpleVideoView that plays a video clip.
Keep the video playback position constant while the activity state changes.
Use an event listener to respond when the video has finished playing.
Play media that is either included in the app or is being streamed from the internet.
An established set of media controls is available in the SimpleVideoView app.
You can use the controls to play, pause, rewind, fast-forward, and skip forward or backward to specified points in the movie by dragging the progress slider.
The VideoView class implements much of the fundamental behaviour you need to play a video in your app. However, it does not offer many features or customization options.
Streaming media from the internet or playing media saved on external media, such as an SD card, or stored within the app's resources can all be done with your app.
In this scenario, you embed the video file within the app itself to keep things simple.
Android includes the MediaController view for the video playback user interface.
The MediaController view offers a collection of standard media controls that can operate a media player (the VideoView object).
You create the initial version of the SimpleVideoView app in this challenge, which plays a video.
Step By Step Guide On How To Play Video In Android Programmatically :-
<VideoView android:id="@+id/videoview" android:layout_width="0dp" android:layout_height="0dp" android:layout_margin="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintDimensionRatio="4:3" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/>
- Make a fresh Android project first. Utilize the Empty activity template and give it the name SimpleVideoView. All other settings can be left alone.
- The resource directory for the video files the app will play can be created by clicking the RES directory in the Project view. Next, select File > New > Android resource directory.
- Change the Resource type to "raw" and the Directory name to "raw". Click OK after leaving the other choices alone. Make a directory for all resources in the project.
- Save the Tacoma Narrows movie clip to your computer. Please use the Save Page As, option if the movie begins playing in your browser and you do not want to download it.
- Insert the Tacoma narrows.mp4 file into your project's raw directory.
- Remove the old TextView from activity main.xml and replace it with a VideoView element that has the following attributes:
The size and location of the VideoView are dynamically determined based on the other constraints when layout width and layout height are both set to 0DP.
The layout constraintDimensionRatio parameter specifies that the width-to-height ratio of the VideoView should be 4:3 when the app determines its size.
This restriction keeps the video's aspect ratio constant and prevents the view from stretching too far in either direction (depending on how the device is rotated).
I hope this article on how to play video in android programmatically helps you and the steps and method mentioned above are easy to follow and implement.