Android直播代码中动态功能的实现

动态功能,类似微信朋友圈,用户可以发布文字图片视频音频等内容,分享自己的日常生活。目前直播app是市场的宠儿,在直播app中加入动态功能可以增加趣味性和用户粘性,是互动直播之外的重要内容。下面以云豹直播系统的直播代码为例,为大家展现具体的直播代码实现方式

注意:

  1. 本文仅用于Android端直播代码
  2. 本文仅供参考

如上图所示,动态页面由一个列表呈现 ,根据动态的内容,分为图片、视频和音频等不同类型,不同类型的内容使用不同的布局,图片类动态根据图片的数量使用不同的布局。图片、视频和音频可以进行点击播放,观众可以对本条动态进行评论、点赞或举报等。

这个直播代码的列表使用RecyclerView,根据不同的类型,使用不同的item布局和ViewHolder进行展示。
具体直播代码如下:

public class ActiveAdapter extends RefreshAdapter {

    private View.OnClickListener mItemClickListener;
    private View.OnClickListener mAvatarClickListener;
    private View.OnClickListener mFollowClickListener;
    private View.OnClickListener mLikeClickListener;
    private View.OnClickListener mMoreClickListener;
    private NineGridLayout.ActionListener mNineGridListener;
    private ActiveVoiceLayout.ActionListener mVoiceListener;
    private View.OnClickListener mVideoClickListener;
    private Drawable[] mLikeDrawables;
    private String mFollowString;
    private String mUnFollowString;
    private String mStatusString0;
    private String mStatusString2;
    private int mStatusColor0;
    private int mStatusColor2;
    private ActiveVoiceLayout mNowPlayVoiceLayout;//当前正在播放的语音控件
    private VoiceMediaPlayerUtil mPlayerUtil;
    private DownloadUtil mDownloadUtil;
    private Drawable mFollowDrawable;
    private Drawable mUnFollowDrawable;
    private int mFollowColor;
    private int mUnFollowColor;

    public ActiveAdapter(Context context) {
        super(context);
        init(context);
    }

    public ActiveAdapter(Context context, List list) {
        super(context, list);
        init(context);
    }

    private void init(Context context) {
        mLikeDrawables = new Drawable[6];
        mLikeDrawables[0] = ContextCompat.getDrawable(context, R.mipmap.icon_active_like_0);
        mLikeDrawables[1] = ContextCompat.getDrawable(context, R.mipmap.icon_active_like_1);
        mLikeDrawables[2] = ContextCompat.getDrawable(context, R.mipmap.icon_active_like_2);
        mLikeDrawables[3] = ContextCompat.getDrawable(context, R.mipmap.icon_active_like_3);
        mLikeDrawables[4] = ContextCompat.getDrawable(context, R.mipmap.icon_active_like_4);
        mLikeDrawables[5] = ContextCompat.getDrawable(context, R.mipmap.icon_active_like_5);
        mFollowDrawable = ContextCompat.getDrawable(context, R.drawable.btn_active_follow_1);
        mUnFollowDrawable = ContextCompat.getDrawable(context, R.drawable.btn_active_follow_0);
        mFollowColor = ContextCompat.getColor(context, R.color.gray5);
        mUnFollowColor = ContextCompat.getColor(context, R.color.global);
        mFollowString = WordUtil.getString(R.string.following);
        mUnFollowString = WordUtil.getString(R.string.follow);
        mStatusString0 = WordUtil.getString(R.string.active_status_0);
        mStatusString2 = WordUtil.getString(R.string.active_status_2);
        mStatusColor0 = ContextCompat.getColor(mContext, R.color.gray1);
        mStatusColor2 = ContextCompat.getColor(mContext, R.color.global);
        mItemClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!ClickUtil.canClick()) {
                    return;
                }
                Object tag = v.getTag();
                if (tag != null) {
                    if (!(mContext instanceof ActiveDetailActivity)) {
                        ActiveDetailActivity.forward(mContext, (ActiveBean) tag);
                    }
                }
            }
        };
        mAvatarClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!ClickUtil.canClick()) {
                    return;
                }
                Object tag = v.getTag();
                if (tag == null) {
                    return;
                }
                RouteUtil.forwardUserHome(mContext, ((ActiveBean) tag).getUid());
            }
        };
        mFollowClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!ClickUtil.canClick()) {
                    return;
                }
                Object tag = v.getTag();
                if (tag == null) {
                    return;
                }
                ActiveUserBean u = ((ActiveBean) tag).getUserBean();
                if (u != null) {
                    CommonHttpUtil.setAttention(u.getId(), null);
                }
            }
        };
        mLikeClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!ClickUtil.canClick()) {
                    return;
                }
                Object tag = v.getTag();
                if (tag == null) {
                    return;
                }
                final Vh vh = (Vh) tag;
                ActiveBean activeBean = vh.mBean;
                if (activeBean == null) {
                    return;
                }
                final String activeId = activeBean.getId();
                MainHttpUtil.activeAddLike(activeId, new HttpCallback() {
                    @Override
                    public void onSuccess(int code, String msg, String[] info) {
                        if (code == 0) {
                            if (info.length > 0) {
                                JSONObject obj = JSON.parseObject(info[0]);
                                int isLike = obj.getIntValue("islike");
                                int likeNum = obj.getIntValue("likes");
                                vh.changeLike(isLike, likeNum);
                                EventBus.getDefault().post(new ActiveLikeEvent(ActiveAdapter.this.hashCode(), activeId, isLike, likeNum));
                            }
                        }else {
                            ToastUtil.show(msg);
                        }
                    }
                });

            }
        };
        mMoreClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!ClickUtil.canClick()) {
                    return;
                }
                Object tag = v.getTag();
                if (tag == null) {
                    return;
                }
                final ActiveBean bean = (ActiveBean) tag;
                if (bean.isSelf()) {
                    DialogUitl.showStringArrayDialog(mContext, new Integer[]{R.string.delete}, new DialogUitl.StringArrayDialogCallback() {
                        @Override
                        public void onItemClick(String text, int tag) {
                            MainHttpUtil.activeDelete(bean.getId(), new HttpCallback() {
                                @Override
                                public void onSuccess(int code, String msg, String[] info) {
                                    if (code == 0) {
                                        EventBus.getDefault().post(new ActiveDeleteEvent(bean.getId()));
                                    }
                                    ToastUtil.show(msg);
                                }
                            });
                        }
                    });
                } else {
                    DialogUitl.showStringArrayDialog(mContext, new Integer[]{R.string.report}, new DialogUitl.StringArrayDialogCallback() {
                        @Override
                        public void onItemClick(String text, int tag) {
                            ActiveReportActivity.forward(mContext, bean.getId());
                        }
                    });
                }
            }
        };
        mNineGridListener = new NineGridLayout.ActionListener() {
            @Override
            public void onItemClick(final List dataList, int position) {
                ImagePreviewDialog dialog = new ImagePreviewDialog();
                dialog.setImageInfo(dataList.size(), position, false, new ImagePreviewDialog.ActionListener() {
                    @Override
                    public void loadImage(ImageView imageView, int position) {
                        ImgLoader.display(mContext, (String) (dataList.get(position)), imageView);
                    }

                    @Override
                    public void onDeleteClick(int position) {

                    }
                });
                dialog.show(((AbsActivity) mContext).getSupportFragmentManager(), "ImagePreviewDialog");
            }

            @Override
            public void displayImage(Object path, ImageView imageView) {
                ImgLoader.display(mContext, (String) path, imageView);
            }
        };
        mVoiceListener = new ActiveVoiceLayout.ActionListener() {

            @Override
            public void onPlayStart(ActiveVoiceLayout voiceLayout, File file) {
                if (mNowPlayVoiceLayout != null && mNowPlayVoiceLayout != voiceLayout) {
                    mNowPlayVoiceLayout.stopPlay();
                }
                mNowPlayVoiceLayout = voiceLayout;
                playVoiceFile(file);
            }

            @Override
            public void onPlayStop() {
                mNowPlayVoiceLayout = null;
                stopPlayVoiceFile();
            }

            @Override
            public void onNeedDownload(final ActiveVoiceLayout voiceLayout, String voiceUrl) {
                if (mNowPlayVoiceLayout != null) {
                    mNowPlayVoiceLayout.stopPlay();
                }
                mNowPlayVoiceLayout = null;
                File dir = new File(CommonAppConfig.MUSIC_PATH);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                String fileName = MD5Util.getMD5(voiceUrl);
                File voiceFile = new File(dir, fileName);
                if (voiceFile.exists()) {
                    voiceLayout.setVoiceFile(voiceFile);
                    voiceLayout.starPlay();
                } else {
                    if (mDownloadUtil == null) {
                        mDownloadUtil = new DownloadUtil();
                    }
                    mDownloadUtil.download(Constants.DOWNLOAD_MUSIC, dir, fileName, voiceUrl, new DownloadUtil.Callback() {
                        @Override
                        public void onSuccess(File file) {
                            voiceLayout.setVoiceFile(file);
                            voiceLayout.starPlay();
                        }

                        @Override
                        public void onProgress(int progress) {

                        }

                        @Override
                        public void onError(Throwable e) {
                        }
                    });
                }
            }
        };

        mVideoClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Object tag = v.getTag();
                if (tag == null) {
                    return;
                }
                ActiveBean bean = (ActiveBean) tag;
                ActiveVideoPlayActivity.forward(mContext, bean.getVideoUrl(), bean.getVideoImage());
            }
        };
    }
    /**
     * 评论数发生变化
     */
    public void onLikeChanged(int from, String activeId, int likeNum, int isLike) {
        if (from == this.hashCode() || TextUtils.isEmpty(activeId)) {
            return;
        }
        for (int i = 0, size = mList.size(); i < size; i++) {
            ActiveBean activeBean = mList.get(i);
            if (activeId.equals(activeBean.getId())) {
                activeBean.setLikeNum(likeNum);
                activeBean.setIsLike(isLike);
                notifyItemChanged(i, Constants.PAYLOAD);
                break;
            }
        }
    }


    @Override
    public void refreshData(List list) {
        if (mNowPlayVoiceLayout != null) {
            mNowPlayVoiceLayout.stopPlay();
        }
        super.refreshData(list);
    }

    @Override
    public void clearData() {
        if (mNowPlayVoiceLayout != null) {
            mNowPlayVoiceLayout.stopPlay();
        }
        super.clearData();
    }

    @Override
    public int getItemViewType(int position) {
        return mList.get(position).getActiveType();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int itemViewType) {
        if (itemViewType == Constants.ACTIVE_TYPE_IMAGE) {
            return new ImageVh(mInflater.inflate(R.layout.item_active_vh_image, viewGroup, false));
        } else if (itemViewType == Constants.ACTIVE_TYPE_VOICE) {
            return new VoiceVh(mInflater.inflate(R.layout.item_active_vh_voice, viewGroup, false));
        } else if (itemViewType == Constants.ACTIVE_TYPE_VIDEO) {
            return new VideoVh(mInflater.inflate(R.layout.item_active_vh_video, viewGroup, false));
        }
        return new Vh(mInflater.inflate(R.layout.item_active_vh_text, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder vh, int position, @NonNull List payloads) {
        Object payload = payloads.size() > 0 ? payloads.get(0) : null;
        ((Vh) vh).setData(mList.get(position), position, payload);
    }

    class Vh extends RecyclerView.ViewHolder {

        ImageView mAvatar;
        View mBtnAvatar;
        TextView mName;
        ImageView mSex;
        TextView mBtnFollow;
        TextView mStatus;
        TextView mText;
        View mAddressGroup;
        TextView mAddress;
        TextView mCity;
        View mLine;
        TextView mCommentNum;
        View mBtnLike;
        TextView mLikeNum;
        ActiveLikeImage mLikeIcon;
        View mBtnMore;
        ActiveBean mBean;

        public Vh(@NonNull View itemView) {
            super(itemView);
            mAvatar = itemView.findViewById(R.id.avatar);
            mBtnAvatar = itemView.findViewById(R.id.btn_avatar);
            mName = itemView.findViewById(R.id.name);
            mSex = itemView.findViewById(R.id.sex);
            mBtnFollow = itemView.findViewById(R.id.btn_follow);
            mStatus = itemView.findViewById(R.id.status);
            mText = itemView.findViewById(R.id.text);
            mAddressGroup = itemView.findViewById(R.id.address_group);
            mAddress = itemView.findViewById(R.id.address);
            mCity = itemView.findViewById(R.id.city);
            mLine = itemView.findViewById(R.id.line);
            mCommentNum = itemView.findViewById(R.id.comment_num);
            mBtnLike = itemView.findViewById(R.id.btn_like);
            mLikeNum = itemView.findViewById(R.id.like_num);
            mLikeIcon = itemView.findViewById(R.id.like_icon);
            mBtnMore = itemView.findViewById(R.id.btn_more);
            itemView.setOnClickListener(mItemClickListener);
            mBtnAvatar.setOnClickListener(mAvatarClickListener);
            mBtnFollow.setOnClickListener(mFollowClickListener);
            mBtnLike.setOnClickListener(mLikeClickListener);
            mBtnMore.setOnClickListener(mMoreClickListener);
            mLikeIcon.setDrawables(mLikeDrawables);
        }

        void setData(ActiveBean bean, int position, Object payload) {
            if (payload == null) {
                mBean = bean;
                mBtnAvatar.setTag(bean);
                itemView.setTag(bean);
                mBtnLike.setTag(this);
                mBtnFollow.setTag(bean);
                mBtnMore.setTag(bean);
                UserBean u = bean.getUserBean();
                if (u != null) {
                    ImgLoader.display(mContext, u.getAvatar(), mAvatar);
                    mName.setText(u.getUserNiceName());
                    mSex.setImageResource(CommonIconUtil.getSexIcon(u.getSex()));
                }
                if (TextUtils.isEmpty(bean.getText())) {
                    if (mText.getVisibility() != View.GONE) {
                        mText.setVisibility(View.GONE);
                    }
                } else {
                    if (mText.getVisibility() != View.VISIBLE) {
                        mText.setVisibility(View.VISIBLE);
                    }
                    mText.setText(bean.getText());
                }
                if (TextUtils.isEmpty(bean.getAddress())) {
                    if (mAddressGroup.getVisibility() != View.GONE) {
                        mAddressGroup.setVisibility(View.GONE);
                    }
                } else {
                    if (mAddressGroup.getVisibility() != View.VISIBLE) {
                        mAddressGroup.setVisibility(View.VISIBLE);
                    }
                    mAddress.setText(bean.getAddress());
                }
                mCity.setText(StringUtil.contact(bean.getCity(), " | ", bean.getDateTime()));
                if (bean.isSelf()) {
                    if (mBtnFollow.getVisibility() == View.VISIBLE) {
                        mBtnFollow.setVisibility(View.INVISIBLE);
                    }
                    if (bean.getStatus() == 1) {
                        if (mStatus.getVisibility() == View.VISIBLE) {
                            mStatus.setVisibility(View.INVISIBLE);
                        }
                    } else {
                        if (mStatus.getVisibility() != View.VISIBLE) {
                            mStatus.setVisibility(View.VISIBLE);
                        }
                        if (bean.getStatus() == 0) {
                            mStatus.setText(mStatusString0);
                            mStatus.setTextColor(mStatusColor0);
                        } else {
                            mStatus.setText(mStatusString2);
                            mStatus.setTextColor(mStatusColor2);
                        }
                    }
                } else {
                    if (mBtnFollow.getVisibility() != View.VISIBLE) {
                        mBtnFollow.setVisibility(View.VISIBLE);
                    }
                }
            }
            mCommentNum.setText(bean.getCommentNumString());
            mLikeIcon.setLike(bean.isLike(), false);
            mLikeNum.setText(bean.getLikeNumString());
            if (mBtnFollow.getVisibility() == View.VISIBLE) {
                if (bean.isFollow()) {
                    mBtnFollow.setText(mFollowString);
                    mBtnFollow.setTextColor(mFollowColor);
                    mBtnFollow.setBackground(mFollowDrawable);
                } else {
                    mBtnFollow.setText(mUnFollowString);
                    mBtnFollow.setTextColor(mUnFollowColor);
                    mBtnFollow.setBackground(mUnFollowDrawable);
                }

            }
            if (position == 0) {
                if (mLine.getVisibility() != View.GONE) {
                    mLine.setVisibility(View.GONE);
                }
            } else {
                if (mLine.getVisibility() != View.VISIBLE) {
                    mLine.setVisibility(View.VISIBLE);
                }
            }
        }


        void changeLike(int isLike, int likeNum) {
            if (mBean != null) {
                mBean.setIsLike(isLike);
                mBean.setLikeNum(likeNum);
                if (mLikeNum != null) {
                    mLikeNum.setText(mBean.getLikeNumString());
                }
                if (mLikeIcon != null) {
                    mLikeIcon.setLike(isLike == 1, true);
                }
            }
        }

    }
}

这样,就在直播代码中实现了动态功能。
目前直播行业竞争激烈,只有不断对直播代码进行优化和更新,才能打造更适合市场趋势的产品,以上代码是从云豹直播代码中截取出来的,如果需使用,可能需要进行一定程度的调整,因此仅供参考。

声明:以上内容为CSDN作者:云豹科技官方 本人原创,未经作者本人同意,禁止转载,否则将追究相关法律责任

你可能感兴趣的:(直播源码,直播APP源码,直播平台开发)