In this tutorial, we will learn about Video Player in Android. We will create an android application and play video from the raw directory.
Note: The android.widget.VideoView class provides methods to play or pause and control the video player.
- The VideoView class has a wide range of methods that may be called in order to manage the playback of video.
Methods | About |
---|---|
setMediaController(mediaController) | It sets the media controller to the video view. |
setVideoURI(uri) | It sets the URI of the video file. |
start() | It starts the video view. |
stopPlayback() | It stops the playback. |
setOnPreparedListener() | It gets invoked once the video starts. |
seekTo(int milliSec) | It seeks to specified time in mili-second. |
suspend() | It suspends the playback. |
resume() | It resumes the playback. |
setVideoPath() | It used for local paths. |
pause() | It pause the playback. |
Preview: Video Player
<–Video Player Example
Step 1 –> Add the code given below to the File: MainActivity.kt
File: MainActivity.kt
package com.example.videoplayer import android.net.Uri import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.os.Environment import android.view.Window import android.view.WindowManager import android.widget.MediaController import android.widget.VideoView class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val videoView=findViewById<VideoView>(R.id.videoView) //media controller val mediaController=MediaController(this) mediaController.setAnchorView(videoView) //media file val uri:Uri= Uri.parse("android.resource://"+packageName+"/"+R.raw.demo) //setting the mediaController , video uri videoView.setMediaController(mediaController) videoView.setVideoURI(uri) videoView.requestFocus() videoView.start() } }
Step 2 –> Add video to the raw directory
(Note: res/New/Android Resource Directory-> Resource type : raw-> ok)
Step 3 –> Add the code given below to the File: activity_main.xml.
File: activity_main.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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <FrameLayout android:id="@+id/frameLayout" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginTop="166dp" android:layout_marginBottom="91dp" app:layout_constraintBottom_toTopOf="@+id/textView" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <VideoView android:id="@+id/videoView" android:layout_width="match_parent" android:layout_height="400dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </FrameLayout> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="55dp" android:text="Developers Dome" android:textSize="30sp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/frameLayout" /> </androidx.constraintlayout.widget.ConstraintLayout>
- We hope that this guide will assist you in quickly creating a simple video player Android application. If you have any problems, please post them in the comments section and we will gladly assist you.
How to Create Calculator App with Example in Android