In this Video Player App Series, I am going to implement how we can play video from Recyclerview in VideoView in android studio
So let's start.
1. we have to create new Activity and name it like VideoPlayer and after creating the activity go to the video_player_activity.xml and add this code.
<?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:keepScreenOn="true"
tools:context=".activity.VideoPlayer">
<RelativeLayout
android:id="@+id/zoom_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<VideoView
android:id="@+id/video_view"
android:keepScreenOn="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
2. add setOnClickListener on VideoFolderAdapter.java
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, VideoPlayer.class);
intent.putExtra("p", position);
context.startActivity(intent);
}
});
3. add this code to your VideoPlayer.java
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.Toast;
import android.widget.VideoView;
import com.example.player.R;
import static com.example.player.adapter.VideosAdapter.videoFolder;
public class VideoPlayer extends AppCompatActivity {
int position = -1;
private VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_player);
videoView = findViewById(R.id.video_view);
position = getIntent().getIntExtra("p", -1);
String path = videoFolder.get(position).getPath();
if (path!=null){
videoView.setVideoPath(path);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
videoView.start();
}
});
}else {
Toast.makeText(this, "path didn't exits", Toast.LENGTH_SHORT).show();
}
}
}
Watch Full Video Tutorials
0 Comments