This video shows the steps to create a video player App in Android. It uses VideoView widget to display the video. Also, it uses Play, Pause, Stop and Restart operations button to do the basic operations of video player.
We will be glad to hear from you regarding any query, suggestions or appreciations at: programmerworld1990@gmail.com
Source code:
package com.example.myvideoplayerapp;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;import android.Manifest;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.view.View;
import android.widget.SeekBar;
import android.widget.VideoView;public class MainActivity extends AppCompatActivity {
private VideoView videoView;
private SeekBar seekBar;
private String MEDIA_PATH = Environment.getExternalStorageDirectory().getPath() + “/Download/Sample_Videos2.mp4”;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);
videoView = findViewById(R.id.videoView);
seekBar = findViewById(R.id.seekBar2);videoView.setVideoPath(MEDIA_PATH);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
seekBar.setMax(videoView.getDuration());
}
});}
public void PlayButton(View view){
if(videoView.isPlaying())
{
videoView.resume();
}
else{
videoView.start();
}new CountDownTimer(videoView.getDuration(),1){
@Override
public void onTick(long l) {
seekBar.setProgress(videoView.getCurrentPosition(),true);
}@Override
public void onFinish() {}
}.start();
}public void PauseButton(View view){
videoView.pause();}
public void RestartButton(View view){
videoView.stopPlayback();
videoView.setVideoPath(MEDIA_PATH);
videoView.start();;
}
public void StopButton(View view){
videoView.stopPlayback();
videoView.setVideoPath(MEDIA_PATH);
}}
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.myvideoplayerapp”><uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”/>
<uses-permission android:name=”android.permission.READ_EXTERNAL_STORAGE”/><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>
<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout 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”><VideoView
android:id=”@+id/videoView”
android:layout_width=”350dp”
android:layout_height=”250dp”
android:layout_marginStart=”25dp”
android:layout_marginTop=”25dp” /><Button
android:id=”@+id/button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”50dp”
android:layout_marginTop=”400dp”
android:onClick=”PlayButton”
android:text=”@string/play” /><Button
android:id=”@+id/button3″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”50dp”
android:layout_marginTop=”480dp”
android:onClick=”RestartButton”
android:text=”@string/restart” /><Button
android:id=”@+id/button4″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”250dp”
android:layout_marginTop=”480dp”
android:onClick=”StopButton”
android:text=”@string/stop” /><Button
android:id=”@+id/button2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”250dp”
android:layout_marginTop=”400dp”
android:onClick=”PauseButton”
android:text=”@string/pause” /><SeekBar
android:id=”@+id/seekBar2″
android:layout_width=”350dp”
android:layout_height=”wrap_content”
android:layout_marginStart=”25dp”
android:layout_marginTop=”320dp” /></RelativeLayout>