Skip to contentSkip to navigationSkip to topbar
On this page

Twilio Video Quickstart for Android


Build your video application with the Twilio Video Android SDK.

(information)

Info


Video API overview

video-api-overview page anchor

The Twilio Video API includes the following resources:

  • Room: The real-time audio, data, video and screen-share session. It's the basic building block for a Programmable Video application.
  • Participants: The client applications that are connected to a Room and sharing audio, data, and video media with one another.
  • Tracks: The audio, data, and video media streams that are shared within a Room.
  • LocalTracks: The audio, data, and video captured from the local client's media sources (for example, microphone and camera).
  • RemoteTracks: The audio, data, and video tracks from other participants connected to the Room.

To start using the Video Android SDK in your apps, you need to perform a few tasks first.

(information)

Info

The Video Android SDK supports the following:

  • Android API level 21 and higher
  • Architectures: armeabi-v7a, arm64-v8a, x86, x86_64

Step 1: Get the Video Android SDK

step-1-get-the-video-android-sdk page anchor

The Video Android SDK is distributed through Maven Central(link takes you to an external page).

Gradle

gradle page anchor

Add the following lines to your build.gradle file:

1
allprojects {
2
repositories {
3
mavenCentral()
4
}
5
}
6
7
android {
8
compileOptions {
9
sourceCompatibility 1.8
10
targetCompatibility 1.8
11
}
12
}
13
14
dependencies {
15
// The Video library resides on Maven Central
16
implementation 'com.twilio:video-android:7.8.0'
17
}

Add the following lines to your proguard-project.txt file:

1
-keep class tvi.webrtc.** { *; }
2
-keep class com.twilio.video.** { *; }
3
-keepattributes InnerClasses

Step 2: Get an API key

step-2-get-an-api-key page anchor

API keys represent credentials to access the Twilio API. You can create API keys from the Twilio Console(link takes you to an external page) or with the REST API. You use them to:

Learn more about API keys.

Step 3: Generate an Access Token

step-3-generate-an-access-token page anchor

An Access Token is a short-lived credential used to authenticate your client-side application to Twilio.

You can generate an Access Token using either the Twilio CLI or a Twilio Helper Library.

For application testing purposes, the Twilio CLI provides a quick way to generate Access Tokens that you can then copy and paste into your application. In a production application, you should use a Twilio Helper Library because your back-end server will need to generate an Access Token for every user in your application.

To use the CLI, you will need to install the Twilio CLI and log in to your Twilio account from the command line; see the CLI Quickstart for instructions. Then, you can install the Token CLI plugin(link takes you to an external page) with the following command:

twilio plugins:install @twilio-labs/plugin-token

To generate an Access Token, run the following command. --identity is a required argument and should be a string that represents the user identity for this Access Token.

twilio token:video --identity=<identity>

You can find examples of how to generate an Access Token for a participant using Twilio Helper Libraries in the User Identity and Access Token guide.


You can use Video.connect() to connect to a Room from your Android application. Once connected, you can send and receive audio and video streams with other Participants who are connected to the Room.

1
private Room.Listener roomListener() {
2
return new Room.Listener() {
3
@Override
4
public void onConnected(Room room) {
5
Log.d(TAG,"Connected to " + room.getName());
6
}
7
}
8
}
9
10
public void connectToRoom(String roomName) {
11
ConnectOptions connectOptions = new ConnectOptions.Builder(accessToken)
12
.roomName(roomName)
13
.audioTracks(localAudioTracks)
14
.videoTracks(localVideoTracks)
15
.dataTracks(localDataTracks)
16
.build();
17
room = Video.connect(context, connectOptions, roomListener);
18
}

You must pass the Access Token when connecting to a Room. You may also optionally pass the following:

  • roomName: The room name to dynamically specify the name of the Room you want to join. You can also encode the Room name in the Access Token, which will allow the user to connect to only the Room specified in the token.
  • localAudioTracks, localVideoTracks or localDataTracks: The local audio, video, or data tracks to begin sharing your local media with other Participants in the Room upon connecting.

The name of the Room specifies which Room you want to join. If you have enabled client-side Room ("ad-hoc Room") creation for your account and a Room by that name doesn't already exist, it will be created upon connection. If a Room by that name is already active, you'll be connected to the Room and receive notifications from any other Participants also connected to the same Room. Room names must be unique within an Account.

(information)

Default Room settings

If you have enabled client-side Room creation, any new Room you create via the Android SDK will follow the default Room settings that you've specified in your account. These settings include options like a StatusCallback URL where you can receive Room creation and other webhook events, the maximum number of Participants, automatic recording, and more. You can view and update your default Room settings in the Twilio Console(link takes you to an external page).

You can also create a Room using the Rooms resource.


To join an existing Room, pass the Room name to the connect method, just as you would when creating a Room.

Once in a Room, you'll receive a participantConnected event for each Participant that successfully joins. Querying the participants getter will return any existing Participants who have already joined the Room.

1
private Room.Listener roomListener() {
2
return new Room.Listener() {
3
@Override
4
public void onConnected(Room room) {
5
Log.d(TAG,"Connected to " + room.getName());
6
}
7
}
8
}
9
10
public void connectToRoom(String roomName) {
11
ConnectOptions connectOptions = new ConnectOptions.Builder(accessToken)
12
.roomName(roomName)
13
.audioTracks(localAudioTracks)
14
.videoTracks(localVideoTracks)
15
.dataTracks(localDataTracks)
16
.build();
17
room = Video.connect(context, connectOptions, roomListener);
18
}

You can capture local media from your device's microphone, camera or screen-share on different platforms in your Android application in the following ways:

  • Begin capturing audio data by creating a LocalAudioTrack.
  • Begin capturing video by adding a LocalVideoTrack with an associated VideoCapturer. The Video Android SDK provides customizable video capturers for both camera and screen sharing.
1
// Create an audio track
2
boolean enable = true;
3
LocalAudioTrack localAudioTrack = LocalAudioTrack.create(context, enable);
4
5
// A video track requires an implementation of a VideoCapturer. Here's how to use the front camera with a Camera2Capturer.
6
Camera2Enumerator camera2Enumerator = new Camera2Enumerator(context);
7
String frontCameraId = null;
8
for (String cameraId : camera2Enumerator.getDeviceNames()) {
9
if (camera2Enumerator.isFrontFacing(cameraId)) {
10
frontCameraId = cameraId;
11
break;
12
}
13
}
14
if(frontCameraId != null) {
15
// Create the CameraCapturer with the front camera
16
CameraCapturer cameraCapturer = new Camera2Capturer(context, frontCameraId);
17
18
// Create a video track
19
LocalVideoTrack localVideoTrack = LocalVideoTrack.create(context, enable, cameraCapturer);
20
21
// Rendering a local video track requires an implementation of VideoSink
22
// Let's assume you have added a VideoView in our view hierarchy
23
VideoView videoView = (VideoView) findViewById(R.id.video_view);
24
25
// Render a local video track to preview your camera
26
localVideoTrack.addSink(videoView);
27
28
// Release the audio track to free native memory resources
29
localAudioTrack.release();
30
31
// Release the video track to free native memory resources
32
localVideoTrack.release();
33
}

Connect as a publish-only Participant

connect-as-a-publish-only-participant page anchor

For some use cases, you may want to connect as a publish-only Participant that's not subscribed to any Tracks. You can disable automatic subscription behavior via ConnectOptions by setting enableAutomaticSubscription to false.

1
public void connectToRoom(String roomName) {
2
ConnectOptions connectOptions = new ConnectOptions.Builder(accessToken)
3
.roomName("my-room")
4
.enableAutomaticSubscription(false)
5
.build();
6
room = Video.connect(context, connectOptions, roomListener);
7
}

Working with remote Participants

working-with-remote-participants page anchor

Handle connected Participants

handle-connected-participants page anchor

When you join a Room, Participants may already be present. You can check for existing Participants in the connected event callback by using the participants getter.

1
// Connect to room
2
Room room = Video.connect(context, connectOptions, new Room.Listener() {
3
@Override
4
public void onConnected(Room room) {}
5
6
@Override
7
public void onConnectFailure(Room room, TwilioException e) {}
8
9
@Override
10
public void onDisconnected(Room room, TwilioException e) {}
11
12
@Override
13
public void onRecordingStarted(Room room) {}
14
15
@Override
16
public void onRecordingStopped(Room room) {}
17
18
@Override
19
public void onParticipantConnected(Room room, RemoteParticipant participant) {
20
Log.i("Room.Listener", participant.getIdentity() + " has joined the room.");
21
}
22
23
@Override
24
public void onParticipantDisconnected(Room room, RemoteParticipant participant) {
25
Log.i("Room.Listener", participant.getIdentity() + " has left the room.");
26
}
27
);
28
29
// ... Assume we have received the connected callback
30
31
// After receiving the connected callback the LocalParticipant becomes available
32
LocalParticipant localParticipant = room.getLocalParticipant();
33
Log.i("LocalParticipant ", localParticipant.getIdentity());
34
35
// Get the first participant from the room
36
RemoteParticipant participant = room.getRemoteParticipants().get(0);
37
Log.i("HandleParticipants", participant.getIdentity() + " is in the room.");
38

Handle Participant connection events

handle-participant-connection-events page anchor

When Participants connect or disconnect from a Room that you're connected to, you'll be notified via an event listener. Similar to Room Events, Twilio will fire Participant events if the StatusCallback webhook URL is set when the Room is created. These events help your application keep track of the participants who join or leave a Room.

1
private Room.Listener roomListener() {
2
return new Room.Listener() {
3
4
@Override
5
public void onParticipantConnected(Room room, RemoteParticipant participant) {
6
Log.v(TAG, "Participant connected: " + participant.getIdentity());
7
}
8
9
@Override
10
public void onParticipantDisconnected(Room room, RemoteParticipant participant) {
11
Log.v(TAG, "Participant disconnected: " + participant.getIdentity());
12
}
13
};
14
}

Display a remote Participant's video

display-a-remote-participants-video page anchor

To see the video tracks being sent by remote Participants, you need to render them to the screen:

1
// First, set a Media Listener when a Participant first connects:
2
private Room.Listener roomListener() {
3
return new Room.Listener() {
4
@Override
5
public void onParticipantConnected(Room room, RemoteParticipant participant) {
6
participant.setListener(remoteParticipantListener());
7
}
8
};
9
}
10
11
/* In the Participant listener, we can respond when the Participant adds a Video
12
Track by rendering it on screen: */
13
private RemoteParticipant.Listener remoteParticipantListener() {
14
return new RemoteParticipant.Listener() {
15
@Override
16
public void onVideoTrackSubscribed(RemoteParticipant participant,
17
RemoteVideoTrackPublication remoteVideoTrackPublication,
18
RemoteVideoTrack remoteVideoTrack) {
19
primaryVideoView.setMirror(false);
20
remoteVideoTrack.addSink(primaryVideoView);
21
}
22
};
23
}

Display a camera preview

display-a-camera-preview page anchor

Sometimes you need to make sure you're looking fantastic before entering a Room. We get it. Each SDK provides a means to render a local camera preview outside the context of an active Room.

Note: See set up local media to learn how to initialize a CameraCapturer.

1
/* The CameraCapturer is a default video capturer provided by Twilio which can
2
capture video from the front or rear-facing device camera */
3
private CameraCapturer cameraCapturer;
4
5
/* A VideoView receives frames from a local or remote video track and renders them
6
to an associated view. */
7
private VideoView primaryVideoView;
8
9
// Start the camera preview
10
LocalVideoTrack localVideoTrack = LocalVideoTrack.create(context, true, cameraCapturer);
11
primaryVideoView.setMirror(true);
12
localVideoTrack.addSink(primaryVideoView);
13
14
// Release the local video track to free native memory resources once you are done
15
localVideoTrack.release();

You can disconnect from a Room you're currently participating in. Other Participants will receive a participantDisconnected event.

1
// To disconnect from a Room:
2
room.disconnect();
3
4
// This results in a call to Room.Listener#onDisconnected
5
private Room.Listener roomListener() {
6
return new Room.Listener() {
7
@Override
8
public void onDisconnected(Room room, TwilioException e) {
9
Log.d(TAG,"Disconnected from " + room.getName());
10
}
11
};
12
}

The Video SDK will raise notifications when a Room is reconnecting due to a network disruption. A Room reconnection is triggered due to a signaling or media reconnection event.

1
private Room.Listener roomListener() {
2
return new Room.Listener() {
3
4
/*
5
* Exception will be either TwilioException.SIGNALING_CONNECTION_DISCONNECTED_EXCEPTION or
6
* TwilioException.MEDIA_CONNECTION_ERROR_EXCEPTION
7
*/
8
@Override
9
public void onReconnecting(Room room, TwilioException exception) {
10
Log.v(TAG, "Reconnecting to room: " + room.getName() + ", exception = " + exception.getMessage());
11
}
12
13
@Override
14
public void onReconnected(Room room) {
15
Log.v(TAG, "Reconnected to room " + room.getName());
16
}
17
};
18
}

The Twilio Video API allows you to control your video applications from your back-end server via HTTP requests.

Learn more about the Video API.


If you're experiencing echo on Android, attempt the following changes before making a support ticket.

You can also find this information in the troubleshooting audio section(link takes you to an external page) of the Android Quickstart README.


To enable debug level logging, add the following code in your application:

1
/*
2
* Set the log level of the Video Android SDK
3
*/
4
Video.setLogLevel(LogLevel.DEBUG);
5
6
/*
7
* If your application is experiencing an issue related to a specific
8
* module, you can set the log level of each of the following modules.
9
*/
10
Video.setModuleLogLevel(LogModule.CORE, LogLevel.DEBUG);
11
Video.setModuleLogLevel(LogModule.PLATFORM, LogLevel.DEBUG);
12
Video.setModuleLogLevel(LogModule.SIGNALING, LogLevel.DEBUG);
13
Video.setModuleLogLevel(LogModule.WEBRTC, LogLevel.DEBUG);

Providing debugging information helps us diagnose and respond quickly. When submitting issues or support tickets, include the following information:

  • Description: Describe the issue.
  • Steps to reproduce: List the steps required to reproduce the issue.
  • Code: Include relevant code snippets that would help in reproduction and troubleshooting of the issue.
  • Expected behavior: Describe what you expected to happen.
  • Actual behavior: Describe what actually happened.
  • Reproduction frequency: Indicate how often the isssue occurs in percentage (for example, 50% of the time).
  • Logs: Include any log output generated when the issue occurs.
  • Video Android SDK version: Include the version of the Video Android SDK where this issue occurs.
  • Android version: Include the version of Android where this issue occurs.
  • Android device: Include the details of the Android device where this issue occurs.
  • Room SID: Include the Room SID. It can be useful for tracing backend issues.

After gathering the required information, you can create an issue on GitHub(link takes you to an external page) or open a support ticket through the Twilio Help Center Assistant(link takes you to an external page).