Skip to main content
Version: 0.x.x

Initialize Live Stream - Android

To configure livestreaming with VideoSDK you require two things, first the token which will be used for Authentication purpose and a streamId which will be used to specify where the hosts and audience will join.

Generating Token​

You can generate a token in two ways:

  1. Temporary Token : You can visit Dashboard's API Key section and generate a temporary token from there.

  2. Server : You can setup JWT in backend and make an API call to get the token from your server.

To learn more about Authentication and token in detail you can follow this guide.

// With Temporary Token
fun getToken(): String {
// Update the token here from the VideoSDK dashboard
return "YOUR_TOKEN"
}

// Server
fun getToken(): String {
val token = arrayOfNulls<String>(1)
AndroidNetworking.get("http://localhost:3000/get-token")
.build()
.getAsJSONObject(object : JSONObjectRequestListener() {
fun onResponse(response: JSONObject) {
try {
token[0] = response.getString("token")
} catch (e: JSONException) {
e.printStackTrace()
}
}

fun onError(anError: ANError) {
anError.printStackTrace()
}
})
return token[0]
}

Generating Stream Id​

With the token ready, you can now get the streamId from the VideoSDK's rooms API.

fun getStreamId(token: String): String {
var streamId: String =null;
// We will use VideoSDK rooms API endpoint to create a streamId
AndroidNetworking.post("https://api.videosdk.live/v2/rooms")
.addHeaders("Authorization", token) // We will pass the token in the headers
.build()
.getAsJSONObject(object : JSONObjectRequestListener() {
fun onResponse(response: JSONObject) {
try {
streamId = response.getString("roomId")
} catch (e: Exception) {
e.printStackTrace()
}
}

fun onError(anError: ANError) {
anError.printStackTrace()
}
})
//we will return the streamId which we got from the response of the api
return streamId
}

Initialization of Live Stream​

  1. To initialize the live stream , first we have to initialize the VideoSDK. We can initialize the VideoSDK using initialize() method provided by the SDK.
initialize
  VideoSDK.initialize(Context context)
  1. Next, we have to set token property of VideoSDK class. By using config() method, you can set the token property of VideoSDK class.
config
 VideoSDK.config(String token)
  1. Now, we can initialize the live stream using a factory method provided by the SDK called initMeeting().

initMeeting() is responsible for initializing the live stream with the provided configuration, which includes the streamId, name, participantId and many more.

Let's take a deeper look at the available configuration options first.

initMeeting
  VideoSDK.initMeeting(
Context context, // context of activity
String MeetingId,
String name, // name of participant
boolean micEnabled, // flag to enable-mic
boolean webcamEnabled, // flag to enable-webcam
String participantId, // id of participant
String mode, // mode of participant
boolean multiStream, // multiStream
Map<String, CustomStreamTrack> customTracks, // map of customTracks
JSONObject metaData, // metaData
String signalingBaseUrl // signalingBaseUrl
PreferredProtocol preferredProtocol //preferredProtocol
)
  • MeetingId :

    • This is a unique identifier that allows participants to join a specific live stream.
    • It will be in the format of xxx-yyy-zzz and will be generated using the VideoSDK's Room API.
  • mode :

    • This paramter defines the mode the participant will enter the live stream with.
    • SEND_AND_RECV: For hosts/co-hosts who can send and receive media.
    • RECV_ONLY: For audience members who can only receive media.​
  • name:

    • This represents the name of the participant in the live streams.
    • It will accept String type value.
  • micEnabled:

    • This is a boolean flag, indicating whether a participant's microphone will be automatically enabled when they join the live stream. This property will be completely ignored for Audience participants, as they are not allowed to publish their audio.
  • webcamEnabled:

    • This is a boolean flag, indicating whether a participant's webcam will be automatically enabled when they join the live stream. This property will be completely ignored for Audience participants, as they are not allowed to publish their video.
  • metaData:

    • If you want to provide additional details about the participants joining the live stream, such as their profile image, you can pass that information in this parameter.
    • It has to be of Object type.
    • This is an OPTIONAL parameter.
  • participantId:

    • This is a unique identifier for the participant's inside the live stream.

      • It can be used to specify the unique identifier which can be linked with your own database service.
      • It has to be of String type.
      • This is an OPTIONAL parameter. By default VideoSDK will generate unique id for each participant.
  • multistream:

    • This is a boolean flag, indicating whether the host's media stream should send multiple resolution layers or a single resolution layer.
caution

You must ensure that the participantId is not repeated in the same livestream. This will enable VideoSDK to eliminate any participant associated with that participantId.

With all the configuration options explained, here is how you can initialize the live stream.

class LiveStreamActivity : AppCompatActivity() {

fun getToken(): String {
...
}

fun getStreamId(token: String): String {
...
}

override fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// initialize AndroidNetworking to perform Network call
AndroidNetworking.initialize(applicationContext)

//We will fetch token and streamId and store it in local variables
val token: String = getToken()
val streamId = getStreamId(token)

// initialize the VideoSDK
VideoSDK.initialize(this@MainActivity)

// set token property of VideoSDK
VideoSDK.config(token)

// create a new meeting instance
val liveStream: Meeting = VideoSDK.initMeeting(
this@MainActivity, streamId, "NAME HERE",
true, true, null, null, false, null, null
)

Log.d("VideoSDK", "onCreate: $streamId")
}
}

API Reference​

The API references for all the methods utilised in this guide are provided below.

Got a Question? Ask us on discord