Javatpoint Logo
Javatpoint Logo

Kotlin Android Video Player

The android.widget.VideoView class is used to play video player in an application. The android.widget.MediaController class provides the playback controls of video player such as play, pause, backward, forward, etc. to control the video player.

Using VideoView and MediaController class we can create a video player.

Method of VideoView class

The android.widget.VideoView class provides several methods to play and control the video player. The commonly used methods of VideoView class are as follows:

Method Description
public void setMediaController(MediaController controller) It sets the media controller to the video view.
public void setVideoURI (Uri uri) It sets the URI of the video file.
public void start() It starts the video view.
public void stopPlayback() It stops the playback.
public void pause() It pauses the playback.
public void suspend() It suspends the playback.
public void resume() It resumes the playback.
public void seekTo(int millis) It seeks to specified time in milliseconds.

Kotlin Android Video Player Example

In this example, we play the video inside the VideoView.

activity_main.xml

Add the following design code in an activity_main.xml file. In this file, we place the VideoView inside the FrameLayout.

MainActivity.kt

Add the following code in MainActivity.kt class. In this class, we are reading the video file video.mp4 from the sdcard/Movies directory. The path of a video file is placed by creating the instance of Uri class and passed it to VideoView.setVideoURI(Uri). To play the video file call the start() method of VideoView.

AndroidManifest.xml

Add the following uses-permission in AndroidManifest.xml file.

Output:


Kotlin Android Video Player




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA