Display Gifts | Play normal gift | Play full-screen gift |
| | |
Gift system`s Structure diagram | Gift system`s Sequence diagram |
| |
// File location:tuilivekit/src/main/java/com/trtc/uikit/livekit/common/uicomponent/gift/
giftcloudserver
// Self Definition gift backend service directory├── GiftCloudServer.java // Default implementation class, interacts with the gift backend, responsible for balance verification, settlement, statistics, etc. Clients are advised to implement their custom version└── IGiftCloudServer.java // Gift backend service interface
// File location:tuilivekit/src/main/java/com/trtc/uikit/livekit/common/uicomponent/gift/
view├── TUIGiftListView.java // Gift panel view├── TUIGiftPlayView.java // Gift playback view└── adapter // Gift panel adapter directory├── GiftPanelAdapter.java // Gift panel view for each page└── GiftViewPagerAdapter.java // Adapter supporting page turning
// File Location:tuilivekit/src/main/java/com/trtc/uikit/livekit/liveroom/view/audience/component/AudienceFunctionView.java
mGiftCloudServer.queryGiftInfoList((error, result) -> post(() -> { if (error == Error.NO_ERROR) { mGiftListPanelView.setGiftList(result); } else { ToastUtil.toastLongMessage("query gift list error, code = " + error); } }));
mGiftCloudServer.queryGiftInfoList
on their own, get a custom gift list List<TUIGift>
, and set the gift list through GiftListView.setGiftList
.animationUrl
of the gift is required to be a SVGA animation.// File Location:tuilivekit/src/main/java/com/trtc/uikit/livekit/liveroom/view/audience/component/AudienceFunctionView.java
mGiftCloudServer.queryBalance((error, result) -> post(() -> { if (error == Error.NO_ERROR) { mGiftListPanelView.setBalance(result); } else { ToastUtil.toastLongMessage("query balance error, code = " + error); } }));
mGiftCloudServer.queryBalance
on their own, obtain the gift balance, and update the gift balance through GiftListView.setBalance
.// File Location:tuilivekit/src/main/java/com/trtc/uikit/livekit/liveroom/view/audience/component/AudienceFunctionView.java
@Override public void onSendGift(TUIGiftListView view, TUIGift gift, int giftCount) { TUIGiftUser receiver = new TUIGiftUser(); receiver.userId = mLiveRoomInfo.anchorInfo.userId; receiver.userName = mLiveRoomInfo.anchorInfo.name.get(); receiver.avatarUrl = mLiveRoomInfo.anchorInfo.avatarUrl.get(); receiver.level = "0"; mGiftCloudServer.sendGift(TUILogin.getUserId(), receiver.userId, gift, giftCount, (error, result) -> post(() -> { if (error == Error.NO_ERROR) { view.sendGift(gift, giftCount, receiver); view.setBalance(result); } else { if (error == Error.BALANCE_INSUFFICIENT) { String info = getResources().getString(R.string.livekit_gift_balance_insufficient); ToastUtil.toastLongMessage(info); } else { ToastUtil.toastLongMessage("send gift error, code = " + error); } } })); }
mGiftCloudServer.sendGift
on their own. The main logic is to first connect to the customer's own business server to verify the balance, and after the verification is passed, the server will charge and count the consumption records, and finally call back the result to the client. After receiving the successful callback, the client sends the gift message through the sendGift
of the GiftListView
, and then updates the gift balance through setBalance
.// File Location:// tuilivekit/src/main/java/com/trtc/uikit/livekit/liveroom/view/audience/component/AudienceLivingView.java// tuilivekit/src/main/java/com/trtc/uikit/livekit/liveroom/view/anchor/component/livestreaming/AnchorLivingView.java
@Override public void onPlayGiftAnimation(TUIGiftPlayView view, TUIGift gift) { mGiftCacheService.request(gift.animationUrl, (error, result) -> { if (error == 0) { view.playGiftAnimation(result); } }); }
mGiftCacheService.request
on their own, successfully load the animation to get the result
(of InputStream
type), and then play the gift animation through playGiftAnimation
of TUIGiftPlayView
.TUIGiftListView
: A gift panel that presents the gift list, sends gifts, and recharges.TUIGiftPlayView
: A panel that plays gifts and automatically listens to gift messages.TUIGiftListView
provides the setGiftList
interface, which can be used to set gift materials.// File Location:tuilivekit/src/main/java/com/trtc/uikit/livekit/common/uicomponent/gift/
TUIGiftListView.javaTUIGiftListView giftListView = new TUIGiftListView(mContext, roomId); //generator giftListView objectList<TUIGift> giftList = new ArrayList<>() //you can change gift materials heregiftListView.setGiftList(giftList) //set gift materials of giftListPanleView
TUIGift
are as follows:giftId: String
: Gift IDgiftName: String
: Gift NameimageUrl: String
: Image displayed on the gift panelanimationUrl: String
: SVGA animation URLprice: Int
: Gift PriceextInfo: <String, Object>
: Custom extension informationanimationUrl
is empty, the gift playing effect will be an ordinary play, and the content played will be the image linked by the imageUrl. If the animationUrl
is not empty, the playing effect will be a full-screen play, and the content played will be the corresponding svga animation.onSendGift
callback in the OnGiftListener
of TUIGiftListView
, get the number of gifts and gift information, after preprocessing, you can call the sendGift
function of TUIGiftListView
for the actual sending of gifts.public void onSendGift(TUIGiftListView giftListView, TUIGift gift, int giftCount) {//...This operation is preprocessing, such as verifying the balance of the current userTUIGiftUser receiver = new TUIGiftUser();//...Set the gift receiver information heregiftListView.sendGift(gift, giftCount, receiver);}
TUIGiftPlayView
will receive and play gift messages by itself.// File Location:tuilivekit/src/main/java/com/trtc/uikit/livekit/common/uicomponent/gift/
TUIGiftPlayView.javaTUIGiftPlayView giftPlayView = new TUIGiftPlayView(mContext, roomId);
TUIGiftPlayView
requires full-screen integration.onReceiveGift
callback in the TUIGiftPlayViewListener
of TUIGiftPlayView
.public interface TUIGiftPlayViewListener { void onReceiveGift(TUIGift gift, int giftCount, TUIGiftUser sender, TUIGiftUser receiver);//... }
playGiftAnimation
method of TUIGiftPlayView
when you receive onPlayGiftAnimation
callback from the TUIGiftPlayViewListener
of TUIGiftPlayView
.public interface TUIGiftPlayViewListener {void onPlayGiftAnimation(TUIGiftPlayView view, TUIGift gift);//... }
TUIGiftListView
provides the setBalance
interface, which can be used to set the balance value displayed on the gift panel.giftListView.setBalance(xxx);
onRecharge
callback in the OnGiftListener
of TUIGiftListView
can be used to receive the click event of the recharge button thrown by the gift display panel. Here, you can connect to your own recharge system.public void onRecharge(TUIGiftListView giftListView) {//...to recharge//setup the latest balance giftListView.setBalance(balance); }
onSendGift
callback in the OnGiftListener
of TUIGiftListView
, connect to the customer's own business server, complete the balance verification, gift billing, and consumption statistics, and then call the sendGift
of TUIGiftListView
to send the gift message.public void onSendGift(TUIGiftListView giftListView, TUIGift gift, int giftCount) {//...Connect to the customer's own business server here to complete balance verification, gift billing, consumption statistics, etcTUIGiftUser receiver = new TUIGiftUser();//...Set the gift receiver information heregiftListView.sendGift(gift, giftCount, receiver);}
Was this page helpful?