How to build a Video Player in Android

This article explains the step-by-step method of creating a Video Player with Android Studio. There is a unique class named "MediaPlayer" in Android that allows you to view videos.

In this article, you will learn how to create your Video Player Example of Flashlight in Android. So let’s take example of FlashVideo Playerlight activity codes  in Android of Mobile application

Method of video player

  • setMediaController(MediaController controller)
  • setVideoURI(Uri uri)
  • start()
  • stopPlayback()
  • pause()
  • suspend()
  • resume()
  • seekTo(int millis)

 Step-by-Step Implementation
​ Step 1: Create a New Project in Android ​studio (File >new  project). 


Step 2: Create an activity_main.xml file in your layout folder . path (res>layout)

Implement the same in​voke the following code inside activity_main.xml file.

 <?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"
android:orientation="vertical"
tools:context=".MainActivity"
tools:ignore="HardcodedText">

<VideoView
android:id="@+id/videoView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
 Step 3: Create an MainActivity.kt file in your package folder .

Implement the same invoke the fol​low​ing code inside MainActivity.kt file.

import android.media.MediaPlayer
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.MediaController
import android.widget.VideoView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity(), MediaPlayer.OnCompletionListener {
@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val videoView = findViewById<View>(R.id.videoView1) as VideoView
videoView.setVideoURI(Uri.parse("android.resource://" + packageName + "/" + R.raw.demo))
videoView.setMediaController( MediaController(this));
videoView.setOnCompletionListener(this@MainActivity);
videoView.start()
}

override fun onCompletion(mp: MediaPlayer?) {
//onCompletion when video end
}
}

Step 4 : Output of above example.

                                                                                                               

Happy coding!

365Bloggy July 19, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive