Suggestions

close search

Getting started with Stringee Video Conference API using Android SDK

Step 1: Prepare

Before you use Stringee Video Conference API to make a video conference:

  1. Sign up for a Stringee account free here: https://developer.stringee.com/account/register
  2. Create a project on Stringee Dashboard.

Stringee create project

Then, the best way to learn how to use Stringee Video Conference API is to follow the steps below.

Step 2: Creating a new project

Stringee SDK is designed to be used with Android Studio.

1. Open Android Studio and select New Project from the File menu.
2. Set the minimum SDK for the app to API 21 (Android 5.0 LOLLIPOP) or later.
3. Click through the wizard, ensuring that Empty Views Activity is selected.

Step 3: Adding the Stringee SDK

Stringee Android SDK is distributed as an AAR and can be added to your project from Maven Central.

  1. Make sure mavenCentral() is included in settings.gradle (or in the project-level build.gradle for older projects):

    dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
    }
  2. Add the SDK and WebRTC dependency to the app-level build.gradle:

    android {
       ...
       compileOptions {
           sourceCompatibility JavaVersion.VERSION_17
           targetCompatibility JavaVersion.VERSION_17
       }
    }
    
    dependencies {
       implementation 'com.stringee.sdk.android:stringee-android-sdk:2.1.13'
       implementation 'io.github.webrtc-sdk:android:144.7559.09'
    }

Step 4: Permissions and ProGuard

The Stringee Android SDK requires the following permissions in your app's AndroidManifest.xml file:

<!-- Internet access -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

<!-- Audio and camera access -->
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.CAMERA" />

<!-- Bluetooth headset support -->
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.bluetooth" android:required="false" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="false" />

Request RECORD_AUDIO, CAMERA, and, on Android 12 or later, BLUETOOTH_CONNECT at runtime before using the corresponding feature.

If your project uses R8/ProGuard, add the following rules to proguard-rules.pro:

-dontwarn org.webrtc.**
-keep class org.webrtc.** { *; }
-keep class com.stringee.** { *; }
-keepattributes Signature
-keepattributes *Annotation*

Step 5: Setting up authentication

To connect to Stringee Server, you need an access_token. In production, the token must be generated by your server as described in Client authentication. For testing, go to Dashboard -> Tools -> Generate Access token and generate an access token.

Declare a variable to store the token:

private String token = "PUT_YOUR_ACCESS_TOKEN_HERE";

Step 6: Connecting

Create a StringeeClient, register its listener before connecting, then call connect:

private StringeeClient client;

client = new StringeeClient(this);
client.addConnectionListener(new StringeeConnectionListener() {
    @Override
    public void onConnectionConnected(StringeeClient stringeeClient, boolean isReconnecting) {
        // The client is connected. You can now connect to a video room.
    }

    @Override
    public void onConnectionDisconnected(StringeeClient stringeeClient, boolean isReconnecting) {
    }

    @Override
    public void onIncomingCall(StringeeCall stringeeCall) {
    }

    @Override
    public void onIncomingCall2(StringeeCall2 stringeeCall2) {
    }

    @Override
    public void onConnectionError(StringeeClient stringeeClient, StringeeError stringeeError) {
    }

    @Override
    public void onRequestNewToken(StringeeClient stringeeClient) {
        // Obtain a new access token from your server, then call connect again.
    }

    @Override
    public void onCustomMessage(String from, JSONObject msg) {
    }

    @Override
    public void onTopicMessage(String from, JSONObject msg) {
    }
});

client.connect(token);

Step 7: Preparing a room

Create the room with Stringee's Room Management REST API and generate a room_token on your server. The room token identifies the room and grants the client permission to join it. Do not generate the room token in the mobile application.

Register a StringeeRoomListener to receive room events:

private StringeeRoom room;

private final StringeeRoomListener roomListener = new StringeeRoomListener() {
    @Override
    public void onConnected(StringeeRoom stringeeRoom) {
        room = stringeeRoom;
        createAndPublishLocalTrack();
    }

    @Override
    public void onDisconnected(StringeeRoom stringeeRoom) {
    }

    @Override
    public void onError(StringeeRoom stringeeRoom, StringeeError stringeeError) {
    }

    @Override
    public void onParticipantConnected(StringeeRoom stringeeRoom, RemoteParticipant participant) {
    }

    @Override
    public void onParticipantDisconnected(StringeeRoom stringeeRoom, RemoteParticipant participant) {
    }

    @Override
    public void onVideoTrackAdded(StringeeRoom stringeeRoom, StringeeVideoTrack track) {
        subscribeTrack(track);
    }

    @Override
    public void onVideoTrackRemoved(StringeeRoom stringeeRoom, StringeeVideoTrack track) {
    }

    @Override
    public void onMessage(StringeeRoom stringeeRoom, JSONObject message, RemoteParticipant participant) {
    }

    @Override
    public void onVideoTrackNotification(RemoteParticipant participant,
                                         StringeeVideoTrack track,
                                         StringeeVideoTrack.MediaType mediaType) {
    }
};

Step 8: Connecting to a room

After StringeeClient is connected, connect to the room using the room token:

String roomToken = "PUT_YOUR_ROOM_TOKEN_HERE";
room = StringeeVideo.connect(client, roomToken, roomListener);

Creating or joining a room is determined by the room token. The old makeRoom() and joinRoom() APIs are no longer used.

Step 9: Publishing a local video track

Create a local audio/video track and publish it after onConnected is called:

private StringeeVideoTrack localTrack;

private void createAndPublishLocalTrack() {
    StringeeVideoTrack.Options options = new StringeeVideoTrack.Options();
    options.audio(true);
    options.video(true);

    localTrack = StringeeVideo.createLocalVideoTrack(this, options, new StatusListener() {
        @Override
        public void onSuccess() {
            setTrackListener(localTrack, localViewContainer, true);
            room.publish(localTrack, new StatusListener() {
                @Override
                public void onSuccess() {
                }
            });
        }
    });
}

Step 10: Subscribing to a remote video track

When onVideoTrackAdded is called, subscribe to the remote track:

private void subscribeTrack(StringeeVideoTrack track) {
    StringeeVideoTrack.Options options = new StringeeVideoTrack.Options();
    options.audio(true);
    options.video(true);

    room.subscribe(track, options, new StatusListener() {
        @Override
        public void onSuccess() {
            setTrackListener(track, remoteViewContainer, false);
        }
    });
}

Step 11: Displaying local and remote video tracks

Stringee exposes each video track as an Android View. Add two containers to activity_conference_call.xml:

<FrameLayout
    android:id="@+id/v_remote"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<FrameLayout
    android:id="@+id/v_local"
    android:layout_width="80dp"
    android:layout_height="120dp"
    android:layout_alignParentEnd="true"
    android:layout_margin="10dp" />

Initialize the containers and render a track after its media is available:

private FrameLayout localViewContainer;
private FrameLayout remoteViewContainer;

localViewContainer = findViewById(R.id.v_local);
remoteViewContainer = findViewById(R.id.v_remote);

private void setTrackListener(StringeeVideoTrack track,
                              FrameLayout container,
                              boolean isLocal) {
    track.setListener(new StringeeVideoTrack.Listener() {
        @Override
        public void onMediaAvailable() {
            runOnUiThread(() -> {
                container.removeAllViews();
                container.addView(track.getView2(ConferenceCallActivity.this));
                track.renderView2();
            });
        }

        @Override
        public void onMediaStateChange(StringeeVideoTrack.MediaState mediaState) {
        }
    });
}

Step 12: Leaving a room

Unpublish the local track first, then call leave(...) on the StringeeRoom object:

private void leaveRoom() {
    if (room == null) {
        return;
    }

    if (localTrack == null) {
        leaveAfterUnpublish();
        return;
    }

    room.unpublish(localTrack, new StatusListener() {
        @Override
        public void onSuccess() {
            leaveAfterUnpublish();
        }

        @Override
        public void onError(StringeeError error) {
            leaveAfterUnpublish();
        }
    });
}

private void leaveAfterUnpublish() {
    room.leave(false, new StatusListener() {
        @Override
        public void onSuccess() {
        }

        @Override
        public void onError(StringeeError error) {
        }
    });
}

Step 13: Running the app

Now that your code is complete, you can run the app on a physical device. You can view the latest completed sample on GitHub: VideoConferenceSample