Suggestions

close search

Back to Archiving Overview

Archiving: Recording your session to an archive

To see the code for this sample, switch to the archiving branch of the learning-opentok-android repo:

git checkout archiving

This page shows the difference between this branch and the basics.step-6 branch, which this branch builds from.

Important: You can only archive sessions that use the OpenTok Media Router (sessions with the media mode set to routed). The default learning-opentok-php code used by this tutorial app uses routed sessions.

The OpenTok archiving API lets you record audio-video streams in a session to MP4 files. You use server-side code to start and stop archive recordings. In the WebServiceCoordinator file, you set the following property to the base URL and endpoints of the web service the app calls to start archive recording, stop recording, and play back the recorded video:

private static final String CHAT_SERVER_URL = BuildConfig.CHAT_SERVER_URL;
private static final String SESSION_INFO_ENDPOINT = CHAT_SERVER_URL + "/session";
private static final String ARCHIVE_START_ENDPOINT = CHAT_SERVER_URL + "/start/:sessionId";
private static final String ARCHIVE_STOP_ENDPOINT = CHAT_SERVER_URL + "/stop/:archiveId";
private static final String ARCHIVE_PLAY_ENDPOINT = CHAT_SERVER_URL + "/view/:archiveId"

When the user selects the Start Archive, Stop Archive, and Play Archive menu items from the action bar or the options menu, the app calls the startArchive() and stopArchive(), and playArchive() methods. These call web services that call server-side code start and stop archive recordings. (See Creating a session and defining archive REST API calls.)

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    switch(item.getItemId()) {
        case R.id.action_settings:
            return true;
        case R.id.action_start_archive:
            startArchive();
            return true;
        case R.id.action_stop_archive:
            stopArchive();
            return true;
        case R.id.action_play_archive:
            playArchive();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

Note that the ChatActivity class now implements the Session.ArchiveListener interface. This means that it implements methods for handling archive-related events.

When archive recording starts, the implementation of the onArchiveStarted(Session session, String archiveId, String archiveName) method (defined by the Session.ArchiveListener interface) is called:

@Override
public void onArchiveStarted(Session session, String archiveId, String archiveName) {
    mCurrentArchiveId = archiveId;
    setStopArchiveEnabled(true);
    mArchivingIndicatorView.setVisibility(View.VISIBLE);
}

The method stores the archive ID (identifying the archive) to an mCurrentArchiveId property. The method also calls the setStopArchiveEnabled(true) method, which causes the Stop Recording menu item to be displayed. And it causes the mArchivingIndicatorView to be displayed (which displays an archiving indicator image).

When the user selects the Stop Archive command, the app passes the archive ID along to the web service that stops the archive recording.

When archive recording stops, the implementation of the onArchiveStopped(Session session, String archiveId) method (defined by the Session.ArchiveListener interface) is called:

@Override
public void onArchiveStopped(Session session, String archiveId) {
    mPlayableArchiveId = archiveId;
    mCurrentArchiveId = null;
    setPlayArchiveEnabled(true);
    setStartArchiveEnabled(true);
    mArchivingIndicatorView.setVisibility(View.INVISIBLE);
}

The method stores the archive ID (identifying the archive) to an mPlayableArchiveId property (and sets mCurrentArchiveId to null). The method also calls the setPlayArchiveEnabled(false) method, which disables the Play Archive menu item, and it calls setStartArchiveEnabled(true) to enable the Start Archive menu item. And it causes the mArchivingIndicatorView to be hidden.

When the user clicks the Play Archive button, the playArchive() method opens a web page (in the device's web browser) that displays the archive recording, by calling the archive playback REST API, defined in Creating a session and defining archive REST API calls.