Header Ads

Header ADS

VideoView Android Studio

 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=".CourseVideoActivity">

<VideoView
android:id="@+id/videoView"
android:layout_width="wrap_content"
android:layout_height="300dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:indeterminateTint="@color/colorPrimary"
android:indeterminateTintMode="src_atop"
app:layout_constraintBottom_toBottomOf="@id/videoView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<LinearLayout
android:layout_margin="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@id/videoView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">

<TextView
android:id="@+id/currentTimeTV"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0.00"
android:textColor="@android:color/white" />

<ProgressBar
android:layout_marginRight="15dp"
android:id="@+id/timeprogressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="6" />

<TextView
android:id="@+id/durationTv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0.00"
android:textColor="@android:color/white" />


</LinearLayout>

<ImageView
android:id="@+id/playBtn"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/ic_baseline_play_circle_outline_24"
app:layout_constraintBottom_toBottomOf="@id/videoView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

JAVA: 
package com.example.onlinecourese;

import androidx.appcompat.app.AppCompatActivity;
import androidx.loader.content.AsyncTaskLoader;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.VideoView;

public class CourseVideoActivity extends AppCompatActivity {


VideoView videoView;
TextView durationTv,currentTimeTV;
ProgressBar progress,timeprogressBar;
Uri videoUri;
ImageView playBtn;
boolean isPlaying;
private int duration =0;
private int current =0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course_video);
videoView = findViewById(R.id.videoView);
progress = findViewById(R.id.progress);
playBtn = findViewById(R.id.playBtn);
durationTv = findViewById(R.id.durationTv);
timeprogressBar = findViewById(R.id.timeprogressBar);
currentTimeTV = findViewById(R.id.currentTimeTV);
timeprogressBar.setProgress(100);
//video have completed buffering
videoUri = Uri.parse("https://www.radiantmediaplayer.com/media/big-buck-bunny-360p.mp4");
videoView.setVideoURI(videoUri);
videoView.requestFocus();
videoView.start();
isPlaying = true;

playBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isPlaying)
{
videoView.pause();
playBtn.setImageResource(R.drawable.ic_baseline_play_circle_outline_24);
isPlaying = false;
}
else
{
videoView.start();
playBtn.setImageResource(R.drawable.ic_baseline_pause_circle_outline_24);
isPlaying = true;
}
}
});

videoView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if (playBtn.getVisibility() == View.GONE) {
playBtn.setVisibility(View.VISIBLE);
} else {
playBtn.setVisibility(View.GONE);
}

}
});
new VideoProgress().execute();

videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
duration = mp.getDuration()/1000;
String duraitonTime = String.format("%02d:%02d",duration/60,duration%60);
durationTv.setText(duraitonTime.trim());

mp.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
progress.setVisibility(View.GONE);
playBtn.setImageResource(R.drawable.ic_baseline_pause_circle_outline_24);
playBtn.setVisibility(View.GONE);
}
});
}
});

}

@Override
protected void onStop() {
super.onStop();
isPlaying = false;
}

public class VideoProgress extends AsyncTask<Void,Integer,Void> {

@Override
protected Void doInBackground(Void... voids) {


do {
current =videoView.getCurrentPosition()/1000;
try {
int currentPossition =current * 100/duration;
publishProgress(currentPossition);
}catch (Exception e)
{

}



} while (timeprogressBar.getProgress()<=100);

return null;
}

@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
timeprogressBar.setProgress(values[0]);
String duraitonTime = String.format("%02d:%02d",values[0]/60,values[0]%60);
currentTimeTV.setText(duraitonTime);
}
}
}









No comments

Theme images by Matt Vince. Powered by Blogger.