Audio/Video Broadcast

To use Audio/Video broadcast functionality, you must download Audio/Video chat enabled SDK. To start using it’s functionality you must have AVBroadcast instance which can be obtained as follows:

AVBroadcast avbroadcast = AVBroadcast.getAVBroadcastInstance(context);

Note that the broadcast feature is available for One-on-One chat.
1. Sending broadcast request
Before starting broadcast, you have to send the request to another user. To send the broadcast use sendAVBroadcastRequest() function. The syntax for this function is as follows:

sendAVBroadcastRequest(String toId, Callbacks callbacks)

e.g:

avbroadcast.sendAVBroadcastRequest("15",new Callbacks() {
	@Override
	public void successCallback(JSONObject response) {
	}
	@Override
	public void failCallback(JSONObject response) {
	}
});

The success response will be as follows:

{"callid":"b7054a443b7eb3f539c01333f0d96904","id":"15"}

The response will contain the id of user to whom you have sent the request, and the callid which is unique id assigned for each Audio Video Broadcast call, so that there will be no cross connection.

2. Start broadcast
Once you sent the request successfully or someone has sent you the request, you can start the broadcast by using startBroadcast() function. The syntax for this function is as follows:

startBroadcast(boolean isInitiator, String callid, RelativeLayout container,Callbacks callbacks)

The description of parameters is as follows:
isInitiator: You have to specify your role in the broadcast. If you are the broadcaster so you will pass true, so your audio and video will be broadcasted, and if you are the listener of broadcast then pass false, so your audio and video will not be broadcasted, you will receive the broadcaster’s audio and video.
callid: This is the call id you get from sending the request, or in the request someone sent to you.
container: Container is the relative layout in which video will be displayed

3. Invite other users to the broadcast
You can invite other users in the broadcast going on. To do so use inviteUsersInBroadcast() function. The syntax for this function is as follows:

inviteUsersInBroadcast(JSONArray users, String callid, Callbacks callbacks)

e.g

JSONArray users = new JSONArray();
users.put(1);
users.put(2);
avbroadcast.inviteUsersInBroadcast(users,new Callbacks() {
	@Override
	public void successCallback(JSONObject response) {
	}
	@Override
	public void failCallback(JSONObject response) {
	}
});

4. End broadcast
In order to end the broadcast use endBroadcast() function. The syntax for this fnuction is as follows:

endBroadcast(boolean isInitiator, String userId, String callid, Callbacks callbacks)

The description for the parameters is as follows:
isInitiator: If you are broadcaster then pass true, so the broadcast will end for all the users, if you are listener then pass false, so the broadcast will not end for all the users, you will just leave the broadcast.
userid: Pass the user id of the user you are having the broadcast with.
callid: Pass the call id of this broadcast which you receive from the request you sent or the request you receive.

e.g:

avbroadcast.endBroadcast(true,"15","b7054a443b7eb3f539c01333f0d96904",new Callbacks() {
	@Override
	public void successCallback(JSONObject response) {
	}
	@Override
	public void failCallback(JSONObject response) {
	}
});

5. Rotation handling
As you know Android devices are sensitive to the rotation, you need to handle the rotation specially while having broadcast, so that you don’t find any difficulties and have smooth transition while rotating the device. You need to use following function to do so:

removeVideoOnRotation()
 and 
addVideoOnRotation()

e.g

@Override
protected void onPause() {
    super.onPause();
    avbroadcast.removeVideoOnRotation(layoutcontainer);
}

@Override
protected void onResume() {
    super.onResume();
    avbroadcast.addVideoOnRotation(layoutcontainer);
}

Pass the container which you have used in the function startBroadcast() function.

Not finding what you need?

The CometChat team is here to help!