0

In my android application i have to show different videos from our server with a thumbnail.The thumbnail should come with a play button also.When we click on the play button the video should play.I have tried below code but its not displaying exactly what i want and also taking some time.I don't want to use a thumbnail image already attached with the video, instead of that just create a thumbnail at run time.

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

List<Attachments> item = new ArrayList<Attachments>();
Context context;

private static LayoutInflater inflater=null;
private Typeface fontFamily;

public AttachmentGridAdapter(Context activity, List<Attachments> arrayList, Typeface fontFamily) {
    item=arrayList;
    context=activity;
    this.fontFamily=fontFamily;
    inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    final View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_attachment, parent, false);
    final ViewHolder vh = new ViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(final AttachmentGridAdapter.ViewHolder gridHolder, final int position) {
    gridHolder.gridAttachmentItem=item.get(position);

        try {
            Bitmap bitmap = null;

                try {
                    bitmap = retriveVideoFrameFromVideo(gridHolder.gridAttachmentItem.getUrl());
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
                if (bitmap != null) {
                    bitmap = Bitmap.createScaledBitmap(bitmap, 240, 240, false);
                    gridHolder.imgAttachment.setImageBitmap(bitmap);
                }
        }catch (Exception e){
            e.printStackTrace();
        }



}

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

public class ViewHolder extends RecyclerView.ViewHolder {
    Attachments gridAttachmentItem;
    ImageView imgExpAttachment;

    public ViewHolder(View itemView) {
        super(itemView);
        imgExpAttachment=(ImageView) itemView.findViewById(R.id.imgExpAttachment);
    }
}

public static Bitmap retriveVideoFrameFromVideo(String videoPath)
        throws Throwable {
    Bitmap bitmap = null;
    MediaMetadataRetriever mediaMetadataRetriever = null;
    try {
        mediaMetadataRetriever = new MediaMetadataRetriever();
        if (Build.VERSION.SDK_INT >= 14)
            mediaMetadataRetriever.setDataSource(videoPath, new HashMap<String, String>());
        else
            mediaMetadataRetriever.setDataSource(videoPath);

        bitmap = mediaMetadataRetriever.getFrameAtTime(1, MediaMetadataRetriever.OPTION_CLOSEST);
    } catch (Exception e) {
        e.printStackTrace();
        throw new Throwable(
                "Exception in retriveVideoFrameFromVideo(String videoPath)"
                        + e.getMessage());

    } finally {
        if (mediaMetadataRetriever != null) {
            mediaMetadataRetriever.release();
        }
    }
    return bitmap;
}

}
KJEjava48
  • 1,967
  • 7
  • 40
  • 69
  • 1
    https://stackoverflow.com/questions/44943575/how-to-create-thumbnail-of-video-url-form-server/44943830#44943830 – AskNilesh Nov 19 '18 at 05:51
  • @NileshRathod what is the variable "path" in DownloadImage class, u haven't declared that. – KJEjava48 Nov 19 '18 at 05:58
  • @NileshRathod How should i add a play button above the subtitle,to play the video on click? – KJEjava48 Nov 19 '18 at 06:36
  • put the play icon in drawable and also above your thumbnail image in xml.and set click on that play icon to play video.. – Bunny Nov 19 '18 at 07:33

0 Answers0