SDKAppID
。UserSig
,复制UserID
、签名(Key)
、UserSig
这三个,后续登录时会用到。/// step 1:flutter pub add tencent_cloud_av_chat_room/// step 2:flutter pub get
import 'package:tencent_av_chat_room_kit/tencent_cloud_chat_sdk_type.dart';class _MyHomePageState extends State<MyHomePage> {final int _sdkAppID = 0; // 前置条件中创建的IM应用SDKAppIDfinal String _loginUserID = ""; // 前置条件中的UserIDfinal String _userSig = ""; // 前置条件中的UserSig@overridevoid initState() {super.initState();_initAndLoginIm();}_initAndLoginIm() async {await TencentImSDKPlugin.v2TIMManager.initSDK(sdkAppID: _sdkAppID,loglevel: LogLevelEnum.V2TIM_LOG_ALL,listener: V2TimSDKListener());TencentImSDKPlugin.v2TIMManager.login(userID: _loginUserID, userSig: _userSig);}}
SDKAppID
、UserSig
、UserID
通过配置的方式传入组件内部进行 IM 的初始化和登录。import 'package:tencent_cloud_av_chat_room/tencent_cloud_av_chat_room.dart';class _TencentAVChatRoomKitState extends State<TencentCloudAVChatRoom> {final int _sdkAppID = 0; // 前置条件中创建的IM应用SDKAppIDfinal String _loginUserID = ""; // 前置条件中的UserIDfinal String _userSig = ""; // 前置条件中的UserSig@overrideWidget build(BuildContext context) {return TencentCloudAVChatRoom(config: TencentCloudAvChatRoomConfig(loginUserID: _loginUserID, sdkAppID: _sdkAppID, userSig: _userSig));}}
import 'package:tencent_cloud_av_chat_room/tencent_cloud_av_chat_room.dart';class _TencentAVChatRoomKitState extends State<TencentCloudAVChatRoom> {final int _sdkAppID = 0; // 前置条件中创建的IM应用SDKAppIDfinal String _loginUserID = ""; // 前置条件中的UserIDfinal String _userSig = ""; // 前置条件中的UserSig@overrideWidget build(BuildContext context) {return TencentCloudAVChatRoom(data: TencentCloudAvChatRoomData(anchorInfo: AnchorInfo()),config: TencentCloudAvChatRoomConfig(loginUserID: _loginUserID, sdkAppID: _sdkAppID, userSig: _userSig, avChatRoomID: ''));}}
// live.dartclass Live extends StatefulWidget {const Live({Key? key}) : super(key: key);@overrideState<Live> createState() => _LiveState();}class _LiveState extends State<Live> {...@overridevoid initState() {super.initState();setupLicense();}/// 腾讯云License管理页面(https://console.tencentcloud.com/live/license)setupLicense() {// 当前应用的License LicenseUrlfinal licenseUrl = "";// 当前应用的License Keyfinal licenseKey = "";V2TXLivePremier.setLicence(licenseUrl, licenseKey);}...}
// live_player.dart...class _LivePlayState extends State<LivePlayer> {...@overridevoid initState() {super.initState();initPlayer();}initPlayer() async {_livePlayer = await V2TXLivePlayer.create();}@overridevoid dispose() {super.dispose();_livePlayer.destroy();}@overrideWidget build(BuildContext context) {return Container(color: Colors.black.withOpacity(0.7),child: V2TXLiveVideoWidget(onViewCreated: (viewId) async {_localViewId = viewId;startPlay();},),);}void startPlay() async {if (_isPlaying) {return;}if (_localViewId != null) {debugPrint("_localViewId $_localViewId");var code = await _livePlayer.setRenderViewID(_localViewId!);if (code != V2TXLIVE_OK) {debugPrint("StartPlay error: please check remoteView load");}}var url = widget.playUrl;debugPrint("play url: $url");var playStatus = await _livePlayer.startLivePlay(url);debugPrint("play status: $playStatus");if (playStatus != V2TXLIVE_OK) {setState(() {_onError = true;});debugPrint("play error: $playStatus url: $url");return;}await _livePlayer.setPlayoutVolume(100);setState(() {_isPlaying = true;});}}
import 'package:tencent_av_chat_room_kit/tencent_cloud_chat_sdk_type.dart';class _MyHomePageState extends State<MyHomePage> {final int _sdkAppID = 0; // 前置条件中创建的IM应用SDKAppIDfinal String _loginUserID = ""; // 前置条件中的UserIDfinal String _userSig = ""; // 前置条件中的UserSig@overridevoid initState() {super.initState();_initAndLoginIm();}_initAndLoginIm() async {await TencentImSDKPlugin.v2TIMManager.initSDK(sdkAppID: _sdkAppID,loglevel: LogLevelEnum.V2TIM_LOG_ALL,listener: V2TimSDKListener());TencentImSDKPlugin.v2TIMManager.login(userID: _loginUserID, userSig: _userSig);}}
// live_room.dartclass LiveRoom extends StatelessWidget {final String loginUserID;final String playUrl;final String avChatRoomID = ''; // 一个直播间本质上是所有用户在一个av chat room 类型的群组里面做消息的收发LiveRoom({Key? key, required this.loginUserID, required this.playUrl}): super(key: key);@overrideWidget build(BuildContext context) {return Stack(children: [LivePlayer(playUrl: playUrl), // 直播拉流和画面播放TencentCloudAVChatRoom(config: TencentCloudAvChatRoomConfig(avChatRoomID: avChatRoomID,loginUserID: loginUserID, //登录的用户ID),data: TencentCloudAvChatRoomData(isSubscribe: false,notification: "直播间公告",anchorInfo: AnchorInfo(subscribeNum: 200,fansNum: 5768,nickName: "风雨人生",avatarUrl:""),))])}}
Stack Widget
将LivePlayer(直播)
和LiveRoom(互动)
通过层叠的方式结合在一起。...@overrideWidget build(BuildContext context) {return Stack(children: [LivePlayer(playUrl: playUrl), // 直播拉流和画面播放TencentCloudAVChatRoom(...TencentCloudAvChatRoomCustomWidgets(roomHeaderAction: Container(), // 屏幕右上角显示区域自定义组件, 默认显示的是直播间在线人数roomHeaderLeading: Container(), // 屏幕左上角显示区域自定义组件,默认显示的是主播信息roomHeaderTag: Container(), // roomHeaderAction 和 roomHeaderLeading 下方,一般用于显示排行榜,热度等。onlineMemberListPanelBuilder: (context, id) { // 直播间在线人数点击后展开的面板自定义return Container();},anchorInfoPanelBuilder: (context, id) { // 主播头像点击后展开的面板自定义return Container();},giftsPanelBuilder: (context) { // 屏幕右下方礼物按钮点击后展示的面板自定义return Container();},messageItemBuilder: (context, message, child) { // 弹幕消息自定义return Container();},messageItemPrefixBuilder: (context, message) { // 弹幕消息前缀自定义,一般用于自定义粉丝牌子等return Container();},giftMessageBuilder: (context, message) { // 礼物消息自定义, 从屏幕左侧滑入的礼物消息return Container();},textFieldActionBuilder: ( // 屏幕右下方区域自定义context,) {return [Container()];},textFieldDecoratorBuilder: (context) { // 屏幕左下方输入框自定义return Container();}))])}
// 礼物带特效(会在屏幕左侧滑入送礼详情并会在屏幕上展示礼物特效(Lottie/SVAGA))final customInfoRocket = {"version": 1.0, // 协议版本号"businessID": "flutter_live_kit", // 业务标识字段"data": {"cmd":"send_gift_message", // 必须为send_gift_message"cmdInfo": {"type": 3, // 礼物类型"giftUrl": "", // 礼物图片地址"giftCount": 1, // 礼物数量"giftSEUrl": "assets/live/rocket.json", // 礼物特效地址,如果是http 开头会加载网络地址"giftName": "超级火箭", // 礼物名称},}};// 礼物不带特效(会在屏幕左测滑入)final customInfoPlane = {"version": 1.0, // 协议版本号"businessID": "flutter_live_kit", // 业务标识字段"data": {"cmd":"send_gift_message", // 必须为send_gift_message"cmdInfo": {"type": 2, // 礼物类型"giftUrl": "", // 礼物图片地址"giftCount": 1, // 礼物数量"giftName": "飞机", // 礼物名称},}};// 普通礼物(礼物会展示到弹幕中,不会从屏幕左测滑入和展示特效)final normalGift = {"version": 1.0, // 协议版本号"businessID": "flutter_live_kit", // 业务标识字段"data": {"cmd":"send_gift_message", // 必须为send_gift_message"cmdInfo": {"type": 1, //普通礼物"giftUrl": "", // 礼物图片地址"giftCount": 1, // 礼物数量"giftName": "花", // 礼物名称"giftUnits": "朵", // 礼物单位},}};
TencentCloudAvChatRoomData(anchorInfo: AnchorInfo(), // 主播信息isSubscribe: false, // 是否订阅notification: "直播间公告" // 直播间公告)
TencentCloudAvChatRoomConfig(avChatRoomID: '', // AV Chat Group. AV Chat Room类型的群ID.[https://www.tencentcloud.com/document/product/269/75697?from_cn_redirect=1]loginUserID: '', // 登录用户IDsdkAppID: 0, // IM 应用IDuserSig: '', // 用户ID和secrectKey生成的sigbarrageMaxCount: 200, // 弹幕最大条数。默认200条,当超出条数后,早些的弹幕会被清除。giftHttpBase: '', // 礼物消息http base。displayConfig: DisplayConfig() // 可控制界面部分组件的展示与隐藏)
final controller = TencentCloudAvChatRoomController();final _needUpdateData = TencentCloudAvChatRoomData(anchorInfo: AnchorInfo(), // 主播信息isSubscribe: false, // 是否订阅notification: "直播间公告" // 直播间公告);final _textString = "我是一条文本消息";final customInfoRocket = {"version": 1.0, // 协议版本号"businessID": "flutter_live_kit", // 业务标识字段"data": {"cmd":"send_gift_message", // 指令"cmdInfo": { // 指令携带的信息"type": 3, // 礼物类型"giftUrl": "1e8913f8c6d804972887fc179fa1fbd7.png", // 礼物图片地址"giftCount": 1, // 礼物数量"giftSEUrl": "assets/live/rocket.json", // 礼物特效地址"giftName": "超级火箭", // 礼物名称},}};final customInfoPlane = {"version": 1.0,"businessID": "flutter_live_kit","data": {"cmd":"send_gift_message","cmdInfo": {"type": 2,"giftUrl": "5e175b792cd652016aa87327b278402b.png","giftCount": 1,"giftName": "飞机",},}};final customInfoFlower = {"version": 1.0,"businessID": "flutter_live_kit","data": {"cmd":"send_gift_message","cmdInfo": {"type": 1,"giftUrl": "8f25a2cdeae92538b1e0e8a04f86841a.png","giftCount": 1,"giftName": "花","giftUnits": "朵",},}};// 更新传入组件的data信息。controller.updateData(_needUpdateData);// 发送文本消息controller.sendTextMessage(_textString);// 发送礼物消息, 礼物消息需要按照如上特定的格式。礼物分为三种类型: [1]: 普通礼物 [2]: 礼物不带特效 [3]: 礼物带特效controller.sendGiftMessage(jsonEncode(customInfoFlower));// 发送任何类型的消息, [message] 需要自己创建controller.sendMessage(message);// 播放特效动画(Lottie, SVGA).controller.playAnimation("assets/live/rocket.json"):
TencentCloudAvChatRoomCallback(onMemberEnter: (memberInfo) {}, // 有人进入直播间onRecvNewMessage: (message) {} // 收到弹幕消息)
TencentCloudAvChatRoomCustomWidgets(roomHeaderAction: Container(), // 屏幕右上角显示区域自定义组件, 默认显示的是直播间在线人数roomHeaderLeading: Container(), // 屏幕左上角显示区域自定义组件,默认显示的是主播信息roomHeaderTag: Container(), // roomHeaderAction 和 roomHeaderLeading 下方,一般用于显示排行榜,热度等。onlineMemberListPanelBuilder: (context, id) { // 直播间在线人数点击后展开的面板自定义return Container();},anchorInfoPanelBuilder: (context, id) { // 主播头像点击后展开的面板自定义return Container();},giftsPanelBuilder: (context) { // 屏幕右下方礼物按钮点击后展示的面板自定义return Container();},messageItemBuilder: (context, message, child) { // 弹幕消息自定义return Container();},messageItemPrefixBuilder: (context, message) { // 弹幕消息前缀自定义,一般用于自定义粉丝牌子等return Container();},giftMessageBuilder: (context, message) { // 礼物消息自定义, 从屏幕左侧滑入的礼物消息return Container();},textFieldActionBuilder: ( // 屏幕右下方区域自定义context,) {return [Container()];},textFieldDecoratorBuilder: (context) { // 屏幕左下方输入框自定义return Container();})
TencentCloudAvChatRoomTheme(backgroundColor: Colors.black, // 小部件的背景色,hintColor: Colors.red, // hint text 颜色highlightColor: Colors.orange, // 高亮颜色accentColor: Colors.white, // 前景色textTheme: TencentCloudAvChatRoomTextTheme(), // 字体主题secondaryColor: Colors.grey, // 次色inputDecorationTheme: InputDecorationTheme() // 输入框主题)
TencentCloudAvChatRoomTextTheme(giftBannerSubTitleStyle: TextStyle(), // 屏幕左侧划入的礼物消息 sub title 字体主题, 礼物名称giftBannerTitleStyle: TextStyle(), // 屏幕左侧划入的礼物消息 title 字体主题anchorTitleStyle: TextStyle(), // 主播名称字体主题anchorSubTitleStyle: TextStyle(), // 点赞字体主题barrageTitleStyle: TextStyle(), // 弹幕消息发送人名称主题barrageTextStyle: TextStyle() // 弹幕消息内容主题)
本页内容是否解决了您的问题?