Suggestions

close search

Getting started with Stringee Video Conference API using Android SDK

Step 1: Prepare

  1. Before using Stringee Video Conference API for the first time, you must have a Stringee account If you do not have a Stringee account, sign up for free here: https://developer.stringee.com/account/register

  2. Create a Project on Stringee Dashboard Stringee create Project

Step 2: Adding the Stringee SDK

  1. Stringee Android SDK is distributed as an AAR and can be added to your project by referencing the AAR remotely with Maven.
    buildscript {
        repositories {
            ...
            mavenCentral()
        }
    }
    ...
    allprojects {
        repositories {
            ...
            mavenCentral()
        }
    }
  2. Navigate to your build.gradle at the app level and include the following:
    android {
        ...
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    }
    dependencies {    
        ...
        implementation 'com.stringee.sdk.android:stringee-android-sdk:1.7.8'
        implementation 'com.android.volley:volley:1.2.0'
    }

Step 3: Permissions and proguard

  1. Permissions

    The Stringee Android SDK requires some permissions from your AndroidManifest

    • Open up android/app/src/main/AndroidManifest.xml
    • Add the following lines:
    // for 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" />
    // for audio access
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    // for camera access
    <uses-permission android:name="android.permission.CAMERA" />
  2. Proguard

    If your project uses ProGuard, you may have to add the following settings to the ProGuard configuration file to ensure Stringee builds correctly:

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

Step 4: Connect to Stringee Server

In order to connect to Stringee Server, 3-parties authentication is required as described here: Client authentication

For testing purpose, go to Dashboard -> Tools -> Generate Access token and generates an access_token. In production, the access_token should be generated by your server, sample code generates access token here: https://github.com/stringeecom/server-samples/tree/master/access_token

  1. Initialize StringeeClient:

    private StringeeClient client;
    ...
    client = new StringeeClient(this);
  2. Register the client's events in your State

    client.setConnectionListener(new StringeeConnectionListener() {
        @Override
        public void onConnectionConnected(final StringeeClient stringeeClient, boolean isReconnecting) {
    
        }
    
        @Override
        public void onConnectionDisconnected(StringeeClient stringeeClient, boolean isReconnecting) {
    
        }
    
        @Override
        public void onIncomingCall(final StringeeCall stringeeCall) {
    
        }
    
        @Override
        public void onIncomingCall2(StringeeCall2 stringeeCall2) {
    
        }
    
        @Override
        public void onConnectionError(StringeeClient stringeeClient, final StringeeError stringeeError) {
    
        }
    
        @Override
        public void onRequestNewToken(StringeeClient stringeeClient) {
        // Get new token here and connect to Stringe server
        }
    
        @Override
        public void onCustomMessage(String s, JSONObject jsonObject) {
    
        }
        @Override
        public void onTopicMessage(String s, JSONObject jsonObject) {
    
        }
    });        
    • When the client connects to Stringee Server, the onConnectionConnected(StringeeClient stringeeClient, boolean isReconnecting) method is called.
    • When the client disconnects to Stringee Server, the onConnectionDisconnected(StringeeClient stringeeClient, boolean isReconnecting) method is called
    • When the client fails to connect to Stringee Server, the onConnectionError(StringeeClient stringeeClient, StringeeError stringeeError) method is called.
    • When the token is expired, the onRequestNewToken(StringeeClient stringeeClient) is called. You will need re-generate a new token and connect again.
  3. Connect Stringee server with an access token generated:

    client.connect(token);

Step 5: Connect room

After the client connects to Stirngee server, follows these steps to connect room:

In order to create room, you will need to use rest api, described here: https://developer.stringee.com/docs/rest-api-reference/room-management.

In order to connect to room, you will need token to the room credential : room_token. In production application, the room_token should be generated by your server sample code generates room token here: https://developer.stringee.com/docs/room-token

```
private StringeeRoom room;
...
room = StringeeVideo.connect(client, "YOUR_ROOM_TOKEN", new StringeeRoomListener() {
        @Override
        public void onConnected(StringeeRoom stringeeRoom) {
            // Invoke when connected to room
        }

        @Override
        public void onDisconnected(StringeeRoom stringeeRoom) {
            // Invoke when disconnected from room
        }

        @Override
        public void onError(StringeeRoom stringeeRoom, StringeeError stringeeError) {
            // Invoke when connect to room error
        }

        @Override
        public void onParticipantConnected(StringeeRoom stringeeRoom, RemoteParticipant remoteParticipant) {
            // Invoke when new participant connected to room
        }

        @Override
        public void onParticipantDisconnected(StringeeRoom stringeeRoom, RemoteParticipant remoteParticipant) {
            // Invoke when new participant disconnected from room
        }

        @Override
        public void onVideoTrackAdded(StringeeRoom stringeeRoom, StringeeVideoTrack stringeeVideoTrack) {
            // Invoke when new video track is added to room
        }

        @Override
        public void onVideoTrackRemoved(StringeeRoom stringeeRoom, StringeeVideoTrack stringeeVideoTrack) {
            // Invoke when new video track is removed from room
        }

        @Override
        public void onMessage(StringeeRoom stringeeRoom, JSONObject jsonObject, RemoteParticipant remoteParticipant) {
            // Invoke when get message in room
        }
    });
```

Step 6: Create local video track and publish to room

/// Create video track options
StringeeVideoTrack.Options options = new StringeeVideoTrack.Options();
options.audio(true);
options.video(true);
options.screen(false);

/// Create local video track
StringeeVideoTrack localTrack = StringeeVideo.createLocalVideoTrack(getActivity(), options);
Common.room.publish(localTrack, new StatusListener() {
    @Override
    public void onSuccess() {

        }
    });

Step 7: Get current participant and video track in room

List<RemoteParticipant> participantList = Common.room.getRemoteParticipants();
if (participantList != null && participantList.size() > 0) {
    for (int i = 0; i < participantList.size(); i++) {
        RemoteParticipant remoteParticipant = participantList.get(i);

        // Get remoteParticipant's video track
        List<StringeeVideoTrack> videoTracks = remoteParticipant.getVideoTracks();
        for (int j = 0; j < videoTracks.size(); j++) {
            StringeeVideoTrack videoTrack = videoTracks.get(j);
        }

Step 8: Subscribe another video track

After receiver another video track, you need to subscribe track to display video

// Set track listener for check media of track
videoTrack.setListener(new Listener() {
    @Override
    public void onMediaAvailable() {
        // When track media available you can display this track                 
    }

    @Override
    public void onMediaStateChange(MediaState mediaState) {

    }
});

StringeeVideoTrack.Options opts = new StringeeVideoTrack.Options();
opts.audio(true);
opts.video(true);
stringeeRoom.subscribe(videoTrack, opts, new StatusListener() {
    @Override
    public void onSuccess() {
    }
});

Step 9: Display track

The Stringee Android SDK exposes videos of track as View objects. You can add these as children of ViewGroup objects in your app. This sample app will use FrameLayout objects (which extend ViewGroup) as containers for track views:

  1. Open your xml file and include the following:

    <FrameLayout
        android:id="@+id/v_track"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
  2. Declare trackViewcontainer as properties of YourActivity class and initialize layout view objects:

    private FrameLayout trackViewcontainer;
    ...
    trackViewcontainer = findViewById(R.id.v_track);
  3. When media available you can display track:

    trackViewcontainer.addView(videoTrack.getView(getActivity()));
    videoTrack.renderView(false);

Step 10: Un publish video track:

If you don't want to show your track in room anymore, you can remove your track in room by un publish it:

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

Step 11: Un subscribe video track:

If you don't want to receive another track's audio or track'svideo in room anymore, you can un subscribe this track:

room.unsubscribe(videoTrack, new StatusListener() {
    @Override
    public void onSuccess() {

    }
});

Step 12: Leave room

After you unpublish your track in room, you need to release track after leave room

// allClient: - true: All devices connected as your userId leave room, false: only this devices leave room
room.leave(allClient, new StatusListener() {
    @Override
    public void onSuccess() {
        StringeeVideo.release(room);
    }
});

Step 13: Mute

Mute local sound:

```
boolean mute = true; // true: mute, false: unmute
localTrack.mute(mute);
```

Step 14: Switch camera

Switch camera in local:

```
localTrack.switchCamera(new StatusListener() {
    @Override
    public void onSuccess() {

    }
});
```

Step 15: Turn on/off video

Turn on/off video:

```
boolean enableVideo = true; // true: turn on, false: turn off
localTrack.enableVideo(enableVideo);
```

Step 16: Share the screen (require android API >= 21)

  1. Initialize StringeeScreenCapture and start capture:

    We have 2 approaches to start sharing the screen

    • Using AppCompatActivity

      • Initialize StringeeScreenCapture:

        import com.stringee.video.StringeeScreenCapture;
        ...
        StringeeScreenCapture screenCapture = new StringeeScreenCapture.Builder().buildWithAppCompatActivity(this)
      • Start capture screen: Before start capture screen, you need to create and start a Foreground service with foregroundServiceType is mediaProjection.

        StringeeVideoTrack screenTrack;
        ...
        capture.startCapture(new CallbackListener<StringeeVideoTrack>() {
            @Override
            public void onSuccess(StringeeVideoTrack videoTrack) {
                // You can publish your screen track now
                screenTrack = videoTrack;
            }
        });
    • Using Activity

      • Initialize StringeeScreenCapture:

        import com.stringee.video.StringeeScreenCapture;
        ...
        StringeeScreenCapture screenCapture = new StringeeScreenCapture.Builder().buildWithActivity(this)
        ...
        // Register receive activityResult
        @Override
        protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
        }
      • Start capture screen: Before start capture screen, you need to create and start a Foreground service with foregroundServiceType is mediaProjection.

        int REQUEST_CODE = YOUR_REQUEST_CODE;
        capture.startCapture(REQUEST_CODE, new StatusListener() {
            @Override
            public void onSuccess(StringeeVideoTrack videoTrack) {
                // You can publish your screen track now
                screenTrack = videoTrack;
            }
        });
        ...
        // After start capture, you will receive capture data onActivityResult then you need to create capture  
        @Override
        protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if(requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK){
                screenCapture.createCapture(data);
            }
        }
  2. Display capture video:

After start capture success, you can display track by following step 9

  1. Stop capture screen:

When you don't want to show your screen track, you can release and unpublish this track

    screenTrack.release();
    room.unpublish(screenTrack, new StatusListener() {
        @Override
        public void onSuccess() {

        }
    });