logo
On this page

Calculate live duration

This doc describes how to calculate the livestream duration through configuration.

To calculate the livestream duration, do the following:

  1. Set the isVisible property of ZegoLiveStreamingDurationConfig to display the current timer. (It is displayed by default)

  2. Assuming that the livestream duration is 5 minutes, the livestream will automatically end when the time is up (refer to the following code). You will be notified of the end of the livestream duration through ZegoLiveStreamingDurationEvents.onDurationUpdate. To end the livestream, you can call the leave method of ZegoUIKitPrebuiltLiveStreamingController.

Untitled
class LivePage extends StatefulWidget {
  final String liveID;
  final bool isHost;

  const LivePage({
    Key? key,
    required this.liveID,
    this.isHost = false,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() => LivePageState();
}

class LivePageState extends State<LivePage> {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: ZegoUIKitPrebuiltLiveStreaming(
        appID: yourAppID /*input your AppID*/,
        appSign: yourAppSign /*input your AppSign*/,
        userID: 'userID',
        userName: 'userName',
        liveID: widget.liveID,
        events: ZegoUIKitPrebuiltLiveStreamingEvents(
            duration: ZegoLiveStreamingDurationEvents(
          onUpdate: (Duration duration) {
            if (duration.inSeconds == 5 * 60) {
              ZegoUIKitPrebuiltLiveStreamingController().leave(context);
            }
          },
        )),
        config: (
          widget.isHost
              ? ZegoUIKitPrebuiltLiveStreamingConfig.host(
                  plugins: [ZegoUIKitSignalingPlugin()],
                )
              : ZegoUIKitPrebuiltLiveStreamingConfig.audience(
                  plugins: [ZegoUIKitSignalingPlugin()],
                ),
        )..duration.isVisible = true,
      ),
    );
  }
}
1
Copied!

On this page

Back to top