Android - Java/Kotlin

Overview

Follow this step-by-step tutorial to implement the Watch Together video chat sample application.

While the client-side application will take care of most of the functionality, in order to make this sample application work, you will need to get an access token from the Cluster Authentication Server (CAS).

Requirements

To complete this guide successfully the following prerequisites are required:

Authentication

An Access Token is needed in order to allow a client to connect to a Session.

  • To learn how to acquire an Access Token please look at the Cluster Authentication Server (CAS) reference

  • To simplify the tutorial, in the section below you can see an example of getting an Access Token

Acquiring an Access Token

TheAccess Tokenis a JWT token - more about JWT you can read - here.

A successful response will look like that:

You can get your API_KEY and API_SECRET in your private area, here.

Going to production

To go to production you will need to implement your own authentication server. Using the server the Access Token will be shared to your various clients (Web, Android, and iOS). With this valid Access Token you will be able to use the service.

For that you will need:

  • API_KEY, and API_SECRET - can be retrieved in your private area once you login

  • Your own working authentication server - Authentication overview

Creating a new project

  • Open Android Studio and select New Project from the File menu

  • Select Empty Activity and click next

  • Choose Java or Kotlin as your programming language for the project

  • Configure your project's location, application, and package names

  • Set minimum SDK version of the application to 23 or higher

Adding Watch Together Android SDK library to the project

  • Create (if it does not exist) in the project libs directory under the app folder

  • Take the downloaded file wtsdk_v....aar and put it into the libs folder

  • Edit application's build.gradle file (in the app folder) with SDK

If you are using android.enableJetifier=true to automatically convert third-party libraries to use AndroidX, please add this line android.jetifier.blacklist=wsdk_v2.0.4.aar to your gradle.properties file.

Adding dependencies

  • Modify the build.gradle file (in the app folder) with SDK dependencies, once modified it should look as follows:

  • Sync and Rebuild the project

    • If the Sync and Rebuild was executed successfully, the Watch Together classes and functions should be available in the project

Granting access to camera and microphone

Using the device Audio and Video requires permissions to be granted from the user and some code that will allow the application to work well under different scenarios.

  • Add the following permissions to the AndroidManifest.xml file under the app/src/main folder in the project above the application tag.

The permissions should be validated before every connect of a client to a session.

Sample application UI

The sample application has a UI element that needs to be handled to allow the proper presentation of the streams and stream properties.

  • Use a view container in the application’s resource layout file to display videos. The sample application demonstrates how to manage videos with RecyclerView container

  • For rendering videos, the SDK provides the VideoRenderer class for use in the application’s layout

  • Initialize Session object with SessionBuilder class

  • Start camera preview - can be called without the Session’s url and the Access Token

  • Connecting to the Session using the Session object requires a Session’s URL and a valid Streaming Token to be available before connecting. The connect() function establishes a connection to the Session with the audio and video tracks inside MediaStream object

  • It is possible connect as different types of participants:

    • FULL_PARTICIPANT - publishes Audio and Video to the session and Subscribes to all other participants Audio and Video in the Session

    • VIEWER - Subscribes to all other participants Audio and Video in the Session

    • AV_BROADCASTER - Publishes Audio and Video to the Session

    • A_BROADCASTER - Publishes only Audio to the Session

  • In the case you would like to connect as a Viewer do the following:

Managing Session logic

To manage the session's logic we provided several callbacks that will allow you to customize the interactions as you need.

  • The SessionListener interface, which the MainActivity implements, will allow you to control the flow of logic of the Session you are managing

Managing Reconnection logic

To manage the reconnect session's logic we provide several callbacks and will allow you to customize the interactions you need.

  • The SessionReconnectListener interface, which the MainActivity implements in the sample application, will allow you to control the flow of logic of the Reconnect you are managing

Managing Connection states logic

To manage the connection states session's logic we provided several callbacks and will allow you to customize the interactions you need.

  • The SessionConnectionListener interface, which the MainActivity implements in the sample application, will allow you to control the flow of logic of the Connection states you are managing

Enable/Disable Audio or Video

  • Bind the Participant view to the Participant data in the ParticipantsAdapter class

Switching camera

During streaming, you can switch from front to back camera by a simple call to the Session’s function switchCamera

Running the application

Once coding is finished, you should be able to run the application in the Android Studio emulator.

You can view the complete Watch Together sample application here

Release notes

  • The Android SDK’s support Android 6.0 version and higher

Next steps

Support

Need technical support? contact us at [email protected].

Last updated

Was this helpful?