如何从androidstudio中的recyclerview适配器获取视频长度

dly7yett  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(367)

我创建了一个应用程序,它可以显示手机存储器中的视频。我想创建一个功能,当用户点击其中一个视频时,如果点击的视频长度超过5分钟或少于2分钟,就会弹出祝酒词。如何在recyclerview适配器中实现这一点。下面是我的recyclerview适配器。
recyclerview适配器

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);

}
}

视频模型类

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; 
  }

  }
bogh5gae

bogh5gae1#

假设以毫秒(msec)为单位保存持续时间

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

然后在列表项中按下面的方式执行 onClick 回拨

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();
        }

    }
});
sulc1iza

sulc1iza2#

首先,每60000等于1分钟。在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();
}

相关问题