How to get a video length from a recyclerview adapter in android studio

Viewed 346

I have created an app where it displays videos from a phone's storage. I want to create a feature where, when a user clicks on one of the videos, a toast pops up if the clicked video's length is longer than 5 minutes or less than 2 minutes. How do I achieve that in a recyclerview adapter. Below is my recyclerview adapter.

The Recyclerview Adapter

public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.ViewHolder> {

private Context context;
private ArrayList<VideoModel> videoModels;

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

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

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
final VideoModel vidModel = videoModels.get(position);

Glide.with(context).load(vidModel.getData()).into(holder.video_thumb);
holder.duration.setText(vidModel.getDuration());

holder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (vidModel.getData() != null){
            Intent intent = new Intent(context, SelectedVideoActivity.class);
            intent.putExtra("videoURL", vidModel.getId());
            context.startActivity(intent);
        }else {
            Toast.makeText(context, "Invalid video", Toast.LENGTH_SHORT).show();
        }

    }
});

}

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

static class ViewHolder extends RecyclerView.ViewHolder {
ImageView video_thumb;
TextView duration;

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

    video_thumb = itemView.findViewById(R.id.video_thumb);
    duration = itemView.findViewById(R.id.duration);

}
}

Video Model class

public class VideoModel{

     long id;
     Uri data; 
     String duration;

    public VideoModel(long id,  Uri data, String duration){
    this.id = id; 
    this.data = data; 
    this.duration = duration;
    }

    public long getId(){
    return id;
   }

    public void setId(long id){
    this.id= id; 
    }
   public Uri getData(){
   return data;
    }

   public void setData(Uri data){
   this.data= data; 
   }

    public String getDuration(){
    return duration:
   }

    public void setDuration(String duration){
    this.duration=duration; 
  }

  }
2 Answers

To start it off, every 60000 is equal to 1 minute. Inside your onClick:

if (vidModel.getDuration() < 120000) { // 120 000 is 2 minutes
    Toast.makeText(activity, "less than 2 minutes", Toast.LENGTH_SHORT).show();
} else if (vidModel.getDuration() > 300000) { // 300 000 is 5 minutes
    Toast.makeText(activity, "more than 5 minutes", Toast.LENGTH_SHORT).show();
} else {
    Toast.makeText(activity, "This video is between 2 and 5 minutes.", Toast.LENGTH_SHORT).show();
}

Assuming you save the duration in Milliseconds (msec)

1 sec = 1000 msec
1 min = 60 sec
So, 1 min = 60 * 1000 = 60000 msec

Then do it like below inside the list item onClick callback

holder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (vidModel.getData() != null){
            Intent intent = new Intent(context, SelectedVideoActivity.class);
            intent.putExtra("videoURL", vidModel.getId());
            context.startActivity(intent);
            
            // Checking video duration
            
            final long ONE_MIN = 60000;
            final long FIVE_MIN = 5 * ONE_MIN;
            final long TWO_MIN = 2 * ONE_MIN;
            
            try {
                long videoDuration = Long.parseLong(vidModel.getDuration());
                
                if (videoDuration > FIVE_MIN)
                    Toast.makeText(this, "The video is longer than 5 min", Toast.LENGTH_SHORT).show();
                    
                else if (videoDuration < TWO_MIN)
                    Toast.makeText(this, "The video is less than 2 min", Toast.LENGTH_SHORT).show();

            } catch (NumberFormatException e) {
                e.printStackTrace();
            }

            
        }else {
            Toast.makeText(context, "Invalid video", Toast.LENGTH_SHORT).show();
        }

    }
});
Related