如何使用滑行从Firebase存储中获取视频Thumbail?

时间:2018-10-01 11:49:18

标签: android firebase-storage android-adapter android-glide

这是我下面的代码。.但是在 setVideoThumbail 上,视频缩略图无法获取,它仅显示黑色,因此如何从Firebase存储中获取视频缩略图。

//Here is the method for fetching video thumbnail and preview it in imageView.
    void setVideoThumbnail(String url){
        if (mediaPost == null)return;
        Glide.with(mediaPost.getContext()).load(url)
                .override(100, 100)
                .fitCenter()
                .into(mediaPost);
        mediaPost.setOnClickListener(this);
    }

下面是VideoAdapter类文件

public class VideoAdapter extends FirebaseRecyclerAdapter<FeedModel, VideoAdapter.ViewHolder> {

    private static final int RIGHT_POST_VIDEO = 1;
    private static final int LEFT_POST_VIDEO = 2;

    private final ClickListener mClickListener;

    private final String nameUser;

    public VideoAdapter (DatabaseReference ref, String nameUser, ClickListener mClickListener) {
        super(FeedModel.class, R.layout.item_feed_left_post, VideoAdapter.ViewHolder.class, ref);
        this.nameUser = nameUser;
        this.mClickListener = mClickListener;
    }

    @NonNull
    @Override
    public VideoAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
      if (viewType == RIGHT_POST_VIDEO){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_feed_right_video,parent,false);
            return new VideoAdapter.ViewHolder(view);
        }else{
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_feed_left_video,parent,false);
            return new VideoAdapter.ViewHolder(view);
        }
    }

    @Override
    public int getItemViewType(int position) {
        FeedModel model = getItem(position);
        if (model.getFile() != null) {
            if (model.getFile().getType().equals("video") && model.getUserModel().getName().equals(nameUser)) {
                return RIGHT_POST_VIDEO;
            } else {
                return LEFT_POST_VIDEO;
            }
        }
        return position;
    }

    @Override
    protected void populateViewHolder(VideoAdapter.ViewHolder viewHolder, FeedModel model, int position) {
        viewHolder.setUsersProfilePhoto(model.getUserModel().getPhoto_profile());
        viewHolder.setTimestamp(model.getTimeStamp());
        if (model.getFile() != null){
            viewHolder.setVideoThumbnail(model.getFile().getUrl_file());
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        final TextView timestamp;
        final ImageView usersProfilePhoto;
        final ImageView mediaPost;

        ViewHolder(View itemView) {
            super(itemView);
            timestamp = itemView.findViewById(R.id.timestamp);
            mediaPost = itemView.findViewById(R.id.media_post);
            usersProfilePhoto = itemView.findViewById(R.id.userPhoto);
        }

        @Override
        public void onClick(View view) {
            int position = getAdapterPosition();
            FeedModel model = getItem(position);
            if (model.getUserModel() != null) {
                mClickListener.clickMediaPost(view,position,model.getUserModel().
                        getName(),model.getUserModel().getPhoto_profile()
                        ,model.getFile().getUrl_file());
            }
        }

        void setUsersProfilePhoto(String urlPhotoUser){
            if (usersProfilePhoto == null)return;
            Glide.with(usersProfilePhoto.getContext()).load(urlPhotoUser).centerCrop().
                    transform(new CircleTransform(usersProfilePhoto.getContext())).
                    override(40,40).into(usersProfilePhoto);
        }

        void setTimestamp(String timestamp){
            if (this.timestamp == null)return;
            this.timestamp.setText(convertTimestamp(timestamp));
        }

        //Here is the method for fetching video thumbnail and preview it in imageView.
        void setVideoThumbnail(String url){
            if (mediaPost == null)return;
            Glide.with(mediaPost.getContext()).load(url)
                    .override(100, 100)
                    .fitCenter()
                    .into(mediaPost);
            mediaPost.setOnClickListener(this);
        }
    }

    private CharSequence convertTimestamp(String mileSeconds){
        return DateUtils.getRelativeTimeSpanString(Long.parseLong(mileSeconds),System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);
    }

}

这是输出:

输出:

enter image description here

0 个答案:

没有答案