Dynamic VideoView in Kotlin - GeeksforGeeks

16 Dec, 2021
In Android, VideoView is used to load video files. We can rely on any of the external resources, URLs, or the local data in order to fetch the video content. In this article, we will be discussing how to create a VideoView in Kotlin dynamically.
Note: If we use the video view in the background or just go back from a current video session, the old video position is not saved, that is the old state where we last left the video is not saved. In order to achieve it, we need to make use of some external databases to store the states.
Following methods are provided by the video view class in order to facilitate the whole procedure:
To create a new project in Android Studio follow these steps:
Click on File, then New and then New Project and give name whatever you like.Choose “Empty Activity” for the project template.Then, select Kotlin language Support and click next button.Select minimum SDK, whatever you need
Click on File, then New and then New Project and give name whatever you like.
Choose “Empty Activity” for the project template.
Then, select Kotlin language Support and click next button.
Select minimum SDK, whatever you need
This is how your project directory should look like:
XML
<?xml version="1.0" encoding="utf-8"?><LinearLayout    android:id="@+id/layout"    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center"    android:orientation="vertical"></LinearLayout>
Now, we need to add the video. To do that, we have two options:
We can have a video file stored locally on our system:Create a folder named “Raw” in the res folder. Add the video file in it and use the following code snippet.// val path = "android.resource://" + packageName + "/" + R.raw.your_videoFile_name
// videoView.setVideoURI(Uri.parse(path)) 
We can use the video file from any web resource:// Uri uri = Uri.parse("your_custom_URL");
// videoView.setVideoURI(uri) 

We can have a video file stored locally on our system:Create a folder named “Raw” in the res folder. Add the video file in it and use the following code snippet.// val path = "android.resource://" + packageName + "/" + R.raw.your_videoFile_name
// videoView.setVideoURI(Uri.parse(path)) 

// val path = "android.resource://" + packageName + "/" + R.raw.your_videoFile_name
// videoView.setVideoURI(Uri.parse(path)) 

We can use the video file from any web resource:// Uri uri = Uri.parse("your_custom_URL");
// videoView.setVideoURI(uri) 

// Uri uri = Uri.parse("your_custom_URL");
// videoView.setVideoURI(uri) 

Insert following code in your MainActivity.kt.
Java
package gfg.apps.videoview  import android.net.Uriimport androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.view.ViewGroupimport android.widget.*  class MainActivity : AppCompatActivity() {      override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)          // creating a VideoView        val videoView = VideoView(this)          // setting height and width of the VideoView in our linear layout        val layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)        layoutParams.setMargins(10, 10, 10, 10)        videoView.layoutParams = layoutParams          // accessing the media controller        val mediaController = MediaController(this)        mediaController.setAnchorView(videoView)        videoView.setMediaController(mediaController)          // setting the video access path        val path = "android.resource://" + packageName + "/" + R.raw.gfg        videoView.setVideoURI(Uri.parse(path))          val linearLayout = findViewById<LinearLayout>(R.id.layout)        // Add VideoView to LinearLayout        linearLayout?.addView(videoView)    }}
XML
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="gfg.apps.videoview">      <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                  <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>  </manifest>
sooda367
android
Android-View
Kotlin Android
Picked
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Create and Add Data to SQLite Database in Android?
Broadcast Receiver in Android With Example
Android RecyclerView in Kotlin
CardView in Android With Example
Content Providers in Android with Example
Broadcast Receiver in Android With Example
Android UI Layouts
Android RecyclerView in Kotlin
Content Providers in Android with Example
Retrofit with Kotlin Coroutine in Android