Recently Added

6/recent/ticker-posts

Video Player App in Android Studio Tutorial | Videos From Folders | Part 4 | 2021

In this video player app part 4 I am going to show you how you can load videos from specific videos folders 
so let's start


1.we need to create an adapter for the video list and name it like  VideosAdapter.java and add this code 

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.player.R;
import com.example.player.VideoModel;
import com.example.player.activity.VideoPlayer;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.snackbar.BaseTransientBottomBar;
import com.google.android.material.snackbar.Snackbar;

import java.io.File;
import java.util.ArrayList;

public class VideosAdapter extends RecyclerView.Adapter<VideosAdapter.MyHolder> {


public static ArrayList<VideoModel> videoFolder = new ArrayList<>();
private final Context context;

public VideosAdapter(ArrayList<VideoModel> videoFolder, Context context) {
this.videoFolder = videoFolder;
this.context = context;
}

@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.files_view, parent, false);
return new MyHolder(view);
}

@Override
public void onBindViewHolder(@NonNull MyHolder holder, int position) {


Glide.with(context).load(videoFolder.get(position).getPath()).into(holder.thumbnail);
holder.title.setText(videoFolder.get(position).getTitle());
holder.duration.setText(videoFolder.get(position).getDuration());
holder.size.setText(videoFolder.get(position).getSize());
holder.resolution.setText(videoFolder.get(position).getResolution());

}

@Override
public int getItemCount() {
return videoFolder.size();
}

public class MyHolder extends RecyclerView.ViewHolder {
ImageView thumbnail, menu;
TextView title, size, duration, resolution;

public MyHolder(@NonNull View itemView) {
super(itemView);

thumbnail = itemView.findViewById(R.id.thumbnail);
title = itemView.findViewById(R.id.video_title);
size = itemView.findViewById(R.id.video_size);
duration = itemView.findViewById(R.id.video_duration);
resolution = itemView.findViewById(R.id.video_quality);
menu = itemView.findViewById(R.id.video_menu);
}
}
}

2.add this code to your VideoFolder.java
package com.example.player.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.player.R;
import com.example.player.VideoModel;
import com.example.player.adapter.VideosAdapter;
import java.util.ArrayList;
import java.util.Locale;

public class VideoFolder extends AppCompatActivity
implements SearchView.OnQueryTextListener {

private RecyclerView recyclerView;
private String name;
private ArrayList<VideoModel> videoModelArrayList = new ArrayList<>();
private VideosAdapter videosAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_folder);
recyclerView = findViewById(R.id.video_recyclerview);
name = getIntent().getStringExtra("folderName");
loadVideos();
}


private void loadVideos() {
videoModelArrayList = getallVideoFromFolder(this, name);
if (name != null && videoModelArrayList.size() > 0) {
videosAdapter = new VideosAdapter(videoModelArrayList, this);
//if your recyclerview lagging then just add this line
recyclerView.setHasFixedSize(true);
recyclerView.setItemViewCacheSize(20);
recyclerView.setDrawingCacheEnabled(true);
recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
recyclerView.setNestedScrollingEnabled(false);

recyclerView.setAdapter(videosAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this,
RecyclerView.VERTICAL, false));
} else {
Toast.makeText(this, "can't find any videos", Toast.LENGTH_SHORT).show();
}

}

private ArrayList<VideoModel> getallVideoFromFolder(Context context, String name) {
ArrayList<VideoModel> list = new ArrayList<>();
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] projection = {
MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA,
MediaStore.Video.Media.TITLE,
MediaStore.Video.Media.SIZE,
MediaStore.Video.Media.HEIGHT,
MediaStore.Video.Media.DURATION,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.BUCKET_DISPLAY_NAME,
MediaStore.Video.Media.RESOLUTION
};
String selection = MediaStore.Video.Media.DATA + " like?";
String[] selectionArgs = new String[]{"%" + name + "%"};

Cursor cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, order);

if (cursor != null) {
while (cursor.moveToNext()) {

String id = cursor.getString(0);
String path = cursor.getString(1);
String title = cursor.getString(2);
int size = cursor.getInt(3);
String resolution = cursor.getString(4);
int duration = cursor.getInt(5);
String disName = cursor.getString(6);
String bucket_display_name = cursor.getString(7);
String width_height = cursor.getString(8);

//this method convert 1204 in 1MB
String human_can_read = null;
if (size < 1024) {
human_can_read = String.format(context.getString(R.string.size_in_b), (double) size);
} else if (size < Math.pow(1024, 2)) {
human_can_read = String.format(context.getString(R.string.size_in_kb), (double) (size / 1024));
} else if (size < Math.pow(1024, 3)) {
human_can_read = String.format(context.getString(R.string.size_in_mb), size / Math.pow(1024, 2));
} else {
human_can_read = String.format(context.getString(R.string.size_in_gb), size / Math.pow(1024, 3));
}

//this method convert any random video duration like 1331533132 into 1:21:12
String duration_formatted;
int sec = (duration / 1000) % 60;
int min = (duration / (1000 * 60)) % 60;
int hrs = duration / (1000 * 60 * 60);

if (hrs == 0) {
duration_formatted = String.valueOf(min)
.concat(":".concat(String.format(Locale.UK, "%02d", sec)));
} else {
duration_formatted = String.valueOf(hrs)
.concat(":".concat(String.format(Locale.UK, "%02d", min)
.concat(":".concat(String.format(Locale.UK, "%02d", sec)))));
}


VideoModel files = new VideoModel(id, path, title,
human_can_read, resolution, duration_formatted,
disName, width_height);
if (name.endsWith(bucket_display_name))
list.add(files);

}
cursor.close();
}

return list;
}




}

3. add this to your string.xml

<string name="size_in_b">%1$.2f B</string>
<string name="size_in_kb">%1$.2f KB</string>
<string name="size_in_mb">%1$.2f MB</string>
<string name="size_in_gb">%1$.2f GB</string>

4. add this code in your Manifest.xml  
<activity
android:name=".activity.VideoPlayer"
android:configChanges="orientation|screenSize|screenLayout|smallestScreenSize"
android:resizeableActivity="true"
android:supportsPictureInPicture="true"
tools:targetApi="n" />
Watch Full Video Tutorials

Post a Comment

0 Comments