NEWDolby Interactivity APIs are now the Dolby.io Communications APIs Learn More >
X

Integrate Video for Android

This tutorial guides how to add video features to a conference.

Start the video

Layout modification

Edit the main_activity.xml with the following items:

<LinearLayout ...>
    ...

    <!-- Step 3. Put the layout changes for the video step here -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/startVideo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="start video" />

        <!-- Step 3.2. This layout will be upgraded in the stop video step -->

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="300dp">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <com.voxeet.sdk.views.VideoView
                android:id="@+id/video"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <com.voxeet.sdk.views.VideoView
                android:id="@+id/videoOther"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
    </LinearLayout>

    <!-- Step 4. ...

</LinearLayout>

2. Modify the interface linking in the MainActivity class in MainActivity.java:

  • New fields for MainActivity:
@Bind(R.id.video)
protected VideoView video;

@Bind(R.id.videoOther)
protected VideoView videoOther;
  • New methods for MainActivity:
@OnClick(R.id.startVideo)
public void onStartVideo() {

}

private void updateStreams() {

}
  • Add the following code to the onCreate method:
@Override
protected void onCreate(Bundle savedInstanceState) {
    ...

    //adding the startVideo in the flow
    add(views, R.id.startVideo);
    add(buttonsInConference, R.id.startVideo);
    add(buttonsNotInOwnVideo, R.id.startVideo);
}

3. Add the following logic to the application:

  • In updateViews enable and disable buttons based on the own video state.
private void updateViews() {
    ...

    if (null != current) {
        if (current.isOwnVideoStarted()) {
            setEnabled(buttonsInOwnVideo, true);
            setEnabled(buttonsNotInOwnVideo, false);
        } else {
            setEnabled(buttonsInOwnVideo, false);
            setEnabled(buttonsNotInOwnVideo, true);
        }
    }
}
  • Use the following implementation for onStartVideo:
public void onStartVideo() {
    VoxeetSDK.conference().startVideo()
            .then((result, solver) -> updateViews())
            .error(error());
}
  • Use the following implementation for updateStreams:
private void updateStreams() {
    for (Participant user : VoxeetSDK.conference().getParticipants()) {
        boolean isLocal = user.getId().equals(VoxeetSDK.session().getParticipantId());
        MediaStream stream = user.streamsHandler().getFirst(MediaStreamType.Camera);

        VideoView video = isLocal ? this.video : this.videoOther;

        if (null != stream && !stream.videoTracks().isEmpty()) {
            video.setVisibility(View.VISIBLE);
            video.attach(user.getId(), stream);
        }
    }
}
  • Add handlers for StreamAddedEvent and StreamUpdatedEvent.
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(StreamAddedEvent event) {
    updateStreams();
    updateViews();
}

@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(StreamUpdatedEvent event) {
    updateStreams();
    updateViews();
}

Stop the video

1. To modify the layout, edit the main_activity.xml file, adding the following content for Step 3.2:

<LinearLayout ...>
    ...
    <LinearLayout ...>
        ...

        <!-- Step 3.2. This layout will be upgraded in the stop video step -->
        <Button
            android:id="@+id/stopVideo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="stop video" />

    </LinearLayout>
    ...
</LinearLayout>

2. Modify the interface linking in the MainActivity class in MainActivity.java:

  • New method for MainActivity:
@OnClick(R.id.stopVideo)
public void onStopVideo() {

}
  • Add the following code to the onCreate method:
@Override
protected void onCreate(Bundle savedInstanceState) {
    ...

    //adding the stopVideo in the flow
    add(views, R.id.stopVideo);
    add(buttonsInConference, R.id.stopVideo);
    add(buttonsInOwnVideo, R.id.stopVideo);
}

3. Add the following logic to the application:

  • Use the following implementation for onLogin:
@OnClick(R.id.stopVideo)
public void onStopVideo() {
    VoxeetSDK.conference().stopVideo()
            .then((result, solver) -> updateViews())
            .error(error());
}
  • Add a handler for the StreamRemovedEvent:
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(StreamRemovedEvent event) {
    updateStreams();
    updateViews();
}

Did this page help you?