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 theRoom
class of Flutter SDK. - This method can be called after the live stream is initialized from the
VideoSdk class
.
Events associated with Join
Following callbacks are received when a participant is successfully joined.
- Local Participant will receive a
Events.roomJoined
event, when successfully joined. - Remote Participant will receive a
Events.participantJoined
event with the newly joinedParticipant
object from the event callback.
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() {
//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,
displayName: "John Doe",
micEnabled: true,
camEnabled: true,
defaultCameraIndex:
1, // Index of MediaDevices will be used to set default camera
);
//Calling Join after the room object is created
_room.join()
setupLiveStreamEventListener();
super.initState();
}
@override
Widget build(BuildContext context) {
return YourLiveStreamWidget();
}
void setupLiveStreamEventListener() {
room.on(Events.roomJoined, (){
// Room/Livestream joined Successfully
});
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 theRoom
class of Flutter SDK.
note
This method can be called only after the live stream is joined successfully.
Events associated with the leave() method
Following callbacks are received when a participant leaves the livestream.
- Local Participant will receive a callback on
Events.roomLeft
. - All remote participants will receive a callback
Events.participantLeft
withparticipantId
.
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() {
setupLiveStreamEventListener();
}
@override
Widget build(BuildContext context) {
return Column(
children:[
ElevatedButton(
onPressed:(){
_room.leave();
},
child: const Text("Leave Livestream"),
),
ElevatedButton(
onPressed:(){
_room.end();
},
child: const Text("End Livestream"),
),
]
);
}
void setupLiveStreamEventListener() {
room.on(Events.roomLeft, (){
// Room/Livestream left
});
room.on(Events.participantLeft, (String participantId){
// Participant left the Livestream
});
}
}
tip
You should call the leave()
method on the unmount of your main livestream widget so that livestream is left once the widget is unmounted.
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