Suggestions

close search

Back to Basic Video Chat Overview

Basic Video Chat Step 5: Publishing an audio video stream to the session

  1. 1
    Basic Video Chat Step 1:
    Setting up the test web service
  2. 2
    Basic Video Chat Step 2:
    Configuring and testing the app
  3. 3
    Basic Video Chat Step 3:
    Initializing an OpenTok session
  4. 4
    Basic Video Chat Step 4:
    Connecting to the session
  5. 5
    Basic Video Chat Step 5:
    Publishing a stream to the session
  6. 6
    Basic Video Chat Step 6:
    Subscribing to audio-video streams

In addition to initializing and connecting to the session, the onSessionConnectionDataReady() method calls the initializePublisher() method.

The Publisher object is defined in the OpenTok Android SDK. A Publisher object acquires an audio and video stream from the device's microphone and camera. These can then be published to the OpenTok session as an audio-video stream.

The Publisher class is a subclass of the PublisherKit class, also defined in the OpenTok Android SDK. The PublisherKit class lets you define custom video drivers (capturers and renderers). The Publisher class uses the device's camera as as the video source, and it implements a pre-built video capturer and renderer.

initializePublisher()

After initializing the Publisher using the Publisher.Builder, the ChatActivity object sets itself, this, to implement the PublisherKit.PublisherListener interface. As such it implements methods of that interface to handle publisher-related events:

  private void initializePublisher() {
    // initialize Publisher and set this object to listen to Publisher events
    mPublisher = new Publisher.Builder(this).build();
    mPublisher.setPublisherListener(this);

The next line sets the Publisher to scale the video to fill the entire area of the renderer, with cropping as needed.

    // set publisher video style to fill view
    mPublisher.getRenderer().setStyle(BaseVideoRenderer.STYLE_VIDEO_SCALE,
    BaseVideoRenderer.STYLE_VIDEO_FILL);

In the last line of this method we add the Publisher's view to the screen. The getView() method of the Publisher object returns a View object that displays a view of the camera:

  mPublisherViewContainer.addView(mPublisher.getView());
}

The mPublisherViewContainer object is a FrameLayout object set to the publisher_container view defined in the main layout XML file.

PublisherListener Events

Upon successfully publishing the stream, the implementation of the onStreamCreated(publisherKit, stream) method (defined in the PublisherKit.PublisherListener interface) is called:

@Override
public void onStreamCreated(PublisherKit publisherKit, Stream stream) {
    Log.i(LOG_TAG, "Publisher Stream Created");
}

If the publisher stops sending its stream to the session, the implementation of the onStreamDestroyed(publisherKit, stream) method is called:

@Override
public void onStreamDestroyed(PublisherKit publisherKit, Stream stream) {
    Log.i(LOG_TAG, "Publisher Stream Destroyed");
}

Almost done. In our last step, we'll see how our application subscribes to other streams in the session.

  1. 1
    Basic Video Chat Step 1:
    Setting up the test web service
  2. 2
    Basic Video Chat Step 2:
    Configuring and testing the app
  3. 3
    Basic Video Chat Step 3:
    Initializing an OpenTok session
  4. 4
    Basic Video Chat Step 4:
    Connecting to the session
  5. 5
    Basic Video Chat Step 5:
    Publishing a stream to the session
  6. 6
    Basic Video Chat Step 6:
    Subscribing to audio-video streams