live_stream_core 3.0.0 copy "live_stream_core: ^3.0.0" to clipboard
live_stream_core: ^3.0.0 copied to clipboard

Live Stream Core is a core control designed specifically for live streaming scenarios, providing a series of powerful API functionalities to help developers quickly implement voice chat room features

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:live_stream_core/live_stream_core.dart';
import 'package:rtc_room_engine/rtc_room_engine.dart';

import 'constants.dart';
import 'debug/generate_test_user_sig.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      localizationsDelegates: [...LiveCoreLocalizations.localizationsDelegates],
      supportedLocales: [...LiveCoreLocalizations.supportedLocales],
      home: IndexWidget(),
    );
  }
}

class IndexWidget extends StatelessWidget {
  const IndexWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('LiveStreamCore Demo'),
      ),
      body: Column(
        children: [
          const SizedBox(
            height: 100,
          ),
          ElevatedButton(
              onPressed: () async {
                final result = await TUIRoomEngine.login(
                    GenerateTestUserSig.sdkAppId,
                    DemoConstants.loginUserId,
                    GenerateTestUserSig.genTestSig(DemoConstants.loginUserId));
                if (result.code == TUIError.success) {
                  debugPrint('LiveStreamCore login success');
                } else {
                  debugPrint(
                      'LiveStreamCore login fail, code = ${result.code}, message = ${result.message}');
                }
              },
              child: const Text('Login')),
          ElevatedButton(
              onPressed: () {
                Navigator.of(context).push(MaterialPageRoute(
                    builder: (context) => const SeatGridExampleWidget()));
              },
              child: const Text('SeatGridWidget')),
        ],
      ),
    );
  }
}

class SeatGridExampleWidget extends StatefulWidget {
  const SeatGridExampleWidget({super.key});

  @override
  State<SeatGridExampleWidget> createState() => _SeatGridExampleWidgetState();
}

class _SeatGridExampleWidgetState extends State<SeatGridExampleWidget> {
  final controller = SeatGridController();
  final testObserver = TestObserver();
  bool isLockSeat = false;

  @override
  void initState() {
    super.initState();
    controller.addObserver(testObserver);
  }

  @override
  void dispose() {
    controller.removeObserver(testObserver);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final screenWidth = MediaQuery.of(context).size.width;
    return SafeArea(
      child: Container(
          color: Colors.white30,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Padding(
                padding: const EdgeInsets.symmetric(vertical: 8.0),
                child: SizedBox(
                    width: screenWidth,
                    height: 300,
                    child: SeatGridWidget(
                      controller: controller,
                      onSeatWidgetTap: (seatInfo) {
                        debugPrint('click seatWidget index:${seatInfo.index}');
                      },
                      // build custom seatView
                      // seatWidgetBuilder:
                      //     (context, seatInfoNotifier, volumeNotifier) {
                      //   return _buildCustomSeatWidget(seatInfoNotifier, volumeNotifier);
                      // },
                    )),
              ),
              Expanded(
                  child: GridView.count(
                crossAxisCount: 3,
                childAspectRatio: 2.5,
                children: [
                  FunctionTestWidget(
                      callback: () async {
                        final roomInfo = TUIRoomInfo(roomId: 'voice_13999');
                        roomInfo.name = 'voice_13999';
                        roomInfo.seatMode = TUISeatMode.applyToTake;
                        roomInfo.isSeatEnabled = true;
                        roomInfo.roomType = TUIRoomType.livingRoom;
                        final result = await controller.startVoiceRoom(roomInfo);
                        if (result.code == TUIError.success) {
                          TUIRoomInfo newRoomInfo = result.data!;
                          debugPrint(
                              'startVoiceRoom success. roomInfo:[roomId:${newRoomInfo.roomId},maxSeatCount:${newRoomInfo.maxSeatCount}');
                        } else {
                          debugPrint("startVoiceRoom failed");
                        }
                      },
                      title: 'StartVoiceRoom'),
                  FunctionTestWidget(
                      callback: () async {
                        final result = await controller.stopVoiceRoom();
                        if (result.code == TUIError.success) {
                          debugPrint('stopVoiceRoom success.');
                        } else {
                          debugPrint('stopVoiceRoom failed.');
                        }
                      },
                      title: 'StopVoiceRoom'),
                  FunctionTestWidget(
                      callback: () async {
                        const roomId = 'voice_krab1';
                        final result = await controller.joinVoiceRoom(roomId);
                        if (result.code == TUIError.success) {
                          TUIRoomInfo roomInfo = result.data!;
                          debugPrint(
                              'JoinVoiceRoom success. roomInfo:[roomId:${roomInfo.roomId},maxSeatCount:${roomInfo.maxSeatCount}');
                        } else {
                          debugPrint("JoinVoiceRoom failed");
                        }
                      },
                      title: 'JoinVoiceRoom'),
                  FunctionTestWidget(
                      callback: () async {
                        final result = await controller.leaveVoiceRoom();
                        if (result.code == TUIError.success) {
                          debugPrint('LeaveVoiceRoom success.');
                        } else {
                          debugPrint('LeaveVoiceRoom failed.');
                        }
                      },
                      title: 'LeaveVoiceRoom'),
                  FunctionTestWidget(
                      callback: () async {
                        const seatMode = TUISeatMode.freeToTake;
                        final result =
                            await controller.updateRoomSeatMode(seatMode);
                        if (result.code == TUIError.success) {
                          debugPrint('UpdateSeatMode success.');
                        } else {
                          debugPrint('UpdateSeatMode failed.');
                        }
                      },
                      title: 'UpdateSeatMode'),
                  FunctionTestWidget(
                      callback: () async {
                        final result = await controller.takeSeat(1, 30);
                        if (result.code == TUIError.success) {
                          switch (result.type) {
                            case RequestResultType.onAccepted:
                              debugPrint('TakeSeat onAccepted.');
                              break;
                            case RequestResultType.onRejected:
                              debugPrint('TakeSeat onRejected.');
                              break;
                            case RequestResultType.onCancelled:
                              debugPrint('TakeSeat onCancelled.');
                              break;
                            case RequestResultType.onTimeout:
                              debugPrint('TakeSeat onCancelled.');
                              break;
                            default:
                              break;
                          }
                        } else {
                          debugPrint(
                              'TakeSeat failed. code:${result.code}, message:${result.message}');
                        }
                      },
                      title: 'TakeSeat'),
                  FunctionTestWidget(
                      callback: () async {
                        final result = await controller.leaveSeat();
                        if (result.code == TUIError.success) {
                          debugPrint('LeaveSeat success.');
                        } else {
                          debugPrint('LeaveSeat failed.');
                        }
                      },
                      title: 'LeaveSeat'),
                  FunctionTestWidget(
                      callback: () async {
                        final result = await controller.moveToSeat(2);
                        if (result.code == TUIError.success) {
                          debugPrint('MoveToSeat success.');
                        } else {
                          debugPrint('MoveToSeat failed.');
                        }
                      },
                      title: 'MoveToSeat'),
                  FunctionTestWidget(
                      callback: () async {
                        final lockMode = TUISeatLockParams();
                        lockMode.lockSeat = isLockSeat ? false : true;
                        final result = await controller.lockSeat(3, lockMode);
                        if (result.code == TUIError.success) {
                          debugPrint('LockSeat success.');
                          isLockSeat = !isLockSeat;
                        } else {
                          debugPrint('LockSeat failed.');
                        }
                      },
                      title: 'LockSeat'),
                  FunctionTestWidget(
                      callback: () async {
                        final result = await controller.takeUserOnSeatByAdmin(
                            4, 'krab1', 30);
                        if (result.code == TUIError.success) {
                          switch (result.type) {
                            case RequestResultType.onAccepted:
                              debugPrint('takeUserOnSeatByAdmin onAccepted.');
                              break;
                            case RequestResultType.onRejected:
                              debugPrint('takeUserOnSeatByAdmin onRejected.');
                              break;
                            case RequestResultType.onCancelled:
                              debugPrint('takeUserOnSeatByAdmin onCancelled.');
                              break;
                            case RequestResultType.onTimeout:
                              debugPrint('takeUserOnSeatByAdmin onCancelled.');
                              break;
                            default:
                              break;
                          }
                        } else {
                          debugPrint('takeUserOnSeatByAdmin failed.');
                        }
                      },
                      title: 'takeUserOnSeatByAdmin'),
                  FunctionTestWidget(
                      callback: () async {
                        final result =
                            await controller.kickUserOffSeatByAdmin('krab1');
                        if (result.code == TUIError.success) {
                          debugPrint('kickUserOffSeatByAdmin success.');
                        } else {
                          debugPrint('kickUserOffSeatByAdmin failed.');
                        }
                      },
                      title: 'kickUserOffSeatByAdmin'),
                  FunctionTestWidget(
                      callback: () async {
                        final result =
                            await controller.responseRemoteRequest('krab1', true);
                        if (result.code == TUIError.success) {
                          debugPrint('ResponseRemoteRequest success.');
                        } else {
                          debugPrint('ResponseRemoteRequest failed.');
                        }
                      },
                      title: 'ResponseRemoteRequest'),
                  FunctionTestWidget(
                      callback: () async {
                        final result = await controller.cancelRequest('1239999');
                        if (result.code == TUIError.success) {
                          debugPrint('CancelRequest success.');
                        } else {
                          debugPrint('CancelRequest failed.');
                        }
                      },
                      title: 'CancelRequest'),
                  FunctionTestWidget(
                      callback: () async {
                        final result = await controller.startMicrophone();
                        if (result.code == TUIError.success) {
                          debugPrint('StartMicrophone success.');
                        } else {
                          debugPrint(
                              'StartMicrophone failed. code:${result.code},message${result.message}');
                        }
                      },
                      title: 'StartMicrophone'),
                  FunctionTestWidget(
                      callback: () {
                        controller.stopMicrophone();
                        debugPrint('StopMicrophone success.');
                      },
                      title: 'StopMicrophone'),
                  FunctionTestWidget(
                      callback: () {
                        controller.muteMicrophone();
                      },
                      title: 'MuteMicrophone'),
                  FunctionTestWidget(
                      callback: () async {
                        final result = await controller.unmuteMicrophone();
                        if (result.code == TUIError.success) {
                          debugPrint('UnmuteMicrophone success.');
                        } else {
                          debugPrint('UnmuteMicrophone failed.');
                        }
                      },
                      title: 'UnmuteMicrophone'),
                  FunctionTestWidget(
                      callback: () {
                        controller.setLayoutMode(LayoutMode.grid, null);
                      },
                      title: 'GridLayout'),
                  FunctionTestWidget(
                      callback: () {
                        controller.setLayoutMode(LayoutMode.focus, null);
                      },
                      title: 'FocusLayout'),
                  FunctionTestWidget(
                      callback: () {
                        controller.setLayoutMode(LayoutMode.vertical, null);
                      },
                      title: 'VerticalLayout'),
                  FunctionTestWidget(
                      callback: () {
                        final rowConfig = SeatWidgetLayoutRowConfig(
                            count: 2,
                            seatSpacing: 20.0,
                            seatSize: const Size(80, 80),
                            alignment: SeatWidgetLayoutRowAlignment.spaceBetween);
                        final layoutConfig = SeatWidgetLayoutConfig(
                            rowConfigs: [rowConfig, rowConfig]);
                        controller.setLayoutMode(LayoutMode.free, layoutConfig);
                      },
                      title: 'FreeLayout'),
                ],
              ))
            ],
          )),
    );
  }

  Widget _buildCustomSeatWidget(ValueNotifier<TUISeatInfo> seatInfoNotifier,
      ValueNotifier<int> volumeNotifier) {
    return Column(
      children: [
        ValueListenableBuilder(
            valueListenable: seatInfoNotifier,
            builder: (context, value, _) {
              return SizedBox(
                width: 50,
                height: 30,
                child: Text('userId:${seatInfoNotifier.value.userId}',
                    style: const TextStyle(
                        fontSize: 10,
                        color: Colors.white,
                        decoration: TextDecoration.none)),
              );
            }),
        ValueListenableBuilder(
            valueListenable: volumeNotifier,
            builder: (context, value, _) {
              return SizedBox(
                  width: 50,
                  height: 30,
                  child: Text(
                    'Volume:${volumeNotifier.value}',
                    style: const TextStyle(
                        fontSize: 10,
                        color: Colors.white,
                        decoration: TextDecoration.none),
                  ));
            }),
      ],
    );
  }
}

class FunctionTestWidget extends StatelessWidget {
  final VoidCallback callback;
  final String title;

  const FunctionTestWidget(
      {super.key, required this.callback, required this.title});

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
        onPressed: () async {
          callback.call();
        },
        child: Text(title, style: const TextStyle(fontSize: 10)));
  }
}

class TestObserver extends SeatGridWidgetObserver {
  TestObserver() {
    super.onRoomDismissed = (roomId) {
      debugPrint('TestObserver.onRoomDismissed. roomId:$roomId');
    };

    super.onKickedOutOfRoom = (roomId, reason, message) {
      debugPrint('TestObserver.onKickedOutOfRoom. roomId:$roomId');
    };

    super.onKickedOffSeat = (userInfo) {
      debugPrint(
          'TestObserver.onKickedOffSeat. operator.userId:${userInfo.userId}');
    };

    super.onSeatRequestReceived = (type, userInfo) {
      debugPrint(
          'TestObserver.onSeatRequestReceived. type:$type, userInfo.userId:${userInfo.userId}');
    };

    super.onSeatRequestCancelled = (type, userInfo) {
      debugPrint(
          'TestObserver.onSeatRequestCancelled. type:$type, userInfo.userId:${userInfo.userId}');
    };

    super.onUserAudioStateChanged = (userInfo, hasAudio, reason) {
      debugPrint(
          'TestObserver.onUserAudioStateChanged. userInfo:$userInfo, hasAudio:$hasAudio');
    };
  }
}
2
likes
110
points
212
downloads

Publisher

unverified uploader

Weekly Downloads

Live Stream Core is a core control designed specifically for live streaming scenarios, providing a series of powerful API functionalities to help developers quickly implement voice chat room features

Homepage

Documentation

API reference

License

MIT (license)

Dependencies

cached_network_image, flutter, flutter_localizations, intl, permission_handler, plugin_platform_interface, rtc_room_engine, stack_trace, tencent_cloud_chat_sdk, tencent_trtc_cloud

More

Packages that depend on live_stream_core