Skip to main content
Version: 5.x.x

Join Live Stream - Flutter

Once, the live stream is configured, the next step is to join it. If you have not initialized the live stream yet, you can follow the guide here.

join()​

  • To join the live stream you can call the join() method which is the part of the Room class of Flutter SDK.
  • This method can be called after the live stream is initialized from the VideoSDK class.
  • join() returns a Future that completes once the live stream is joined. If it cannot be joined, it throws a VideoSDKError, for example 4002 for an invalid token or 3073 for an empty stream id. A room whose join() failed cannot join again, so create a new room to retry.
  • On Android, the SDK does not ask for camera or microphone permission. A host should call VideoSDK.requestPermissions() before joining, or the camera and microphone stay off.

Events associated with Join​

Following callbacks are received when a participant is successfully joined.

import 'dart:async' show unawaited;
import 'package:flutter/material.dart';
import 'package:videosdk/videosdk.dart';

class LiveStreamScreen extends StatefulWidget {
...
}

class _LiveStreamScreenState extends State<LiveStreamScreen> {
late Room _room;

@override
void initState() {
super.initState();
//Creating a new Room based on the passed livestreamId and token from the Joining Screen
// create room
_room = VideoSDK.createRoom(
roomId: widget.livestreamId,
token: widget.token,
participantName: "John Doe",
micEnabled: true,
camEnabled: true,
defaultCamera: FacingMode.FRONT, // Camera the webcam starts on
);

//Calling Join after the room object is created
unawaited(joinLiveStream());

setupLiveStreamEventListener();
}

Future<void> joinLiveStream() async {
// On Android, the SDK does not ask for camera or microphone permission.
await VideoSDK.requestPermissions();
try {
await _room.join();
} on VideoSDKError catch (error) {
// For example 4002 (invalid token) or 3073 (empty stream id).
print("Join failed: ${error.code} ${error.name}");
}
}

@override
Widget build(BuildContext context) {
return YourLiveStreamWidget();
}


void setupLiveStreamEventListener() {
_room.on(Events.roomJoined, (){
// Room/Livestream joined Successfully
Participant localParticipant = _room.localParticipant;
});
_room.on(Events.participantJoined, (Participant participant){
// New Participant joined the livestream
});
}

}

leave()​

  • To leave the live stream you can call the leave() method which is the part of the Room class of Flutter SDK.
  • leave() returns a Future that throws a VideoSDKError when the SDK refuses it. Calling it after the live stream has been left throws 3054.
  • To end the live stream for everyone, call end() instead. It also returns a Future that throws a VideoSDKError, for example 3035 when the live stream has already been left.
note

These methods can be called only after the live stream is joined successfully.

After you leave​

A room is single-use. Once Events.roomLeft is delivered, the room removes every event handler and releases itself, so there is nothing to dispose. A room that has left cannot join again: join() throws a StateError. Create a new room with VideoSDK.createRoom() for the next live stream.

Events associated with the leave() method​

Following callbacks are received when a participant leaves the livestream.

import 'dart:async' show unawaited;
import 'package:flutter/material.dart';
import 'package:videosdk/videosdk.dart';

class LiveStreamScreen extends StatefulWidget {
...
}

class _LiveStreamScreenState extends State<LiveStreamScreen> {
late Room _room;

@override
void initState() {
super.initState();
setupLiveStreamEventListener();

}

Future<void> leaveLiveStream() async {
try {
await _room.leave();
} on VideoSDKError catch (error) {
// 3054: the live stream has already been left.
if (error.code != 3054) {
print("Leave failed: ${error.code} ${error.name}");
}
}
}

Future<void> endLiveStream() async {
try {
await _room.end();
} on VideoSDKError catch (error) {
print("End failed: ${error.code} ${error.name}");
}
}

@override
Widget build(BuildContext context) {
return Column(
children:[
ElevatedButton(
onPressed: leaveLiveStream,
child: const Text("Leave Livestream"),
),
ElevatedButton(
onPressed: endLiveStream,
child: const Text("End Livestream"),
),
]
);
}
void setupLiveStreamEventListener() {
_room.on(Events.roomLeft, (LeaveReason? reason){
// Room/Livestream left. Close the livestream screen here.
});
_room.on(Events.participantLeft, (Participant participant, LeaveReason reason){
// Participant left the Livestream
});
}
}
tip

You should also call the leave() method when the user closes your main livestream widget without tapping Leave Livestream, for example with the back button, so that the livestream is left once the widget is closed. The Interactive Live Streaming Quick Start does this from a PopScope callback.

API Reference​

The API references for all the methods and events utilized in this guide are provided below.

Got a Question? Ask us on discord