close search

Add Messaging, Voice, and Authentication to your apps with Nexmo APIs

Visit Nexmo

Publishing streams — Android

Once you have connected to a session, you can publish a stream that other clients connected to the session can view.

This topic includes the following sections:

Initializing a Publisher object

A Publisher object is used to capture an audio-video stream from the device's microphone and camera for use in an OpenTok session.

For a basic publisher, use the Publisher.Builder class to instantiate a Publisher object. Pass in the Android application context for the publisher:

mPublisher = new Publisher.Builder(context)

Add a listener object for basic publisher-related events by calling the setPublisherListener(PublisherKit.PublisherListener listener) method of the Publisher object:


Implement the methods of the PublisherKit.PublisherListener interface in the object you specify as the event listener object. These methods are called when publisher-related events occur.

The getView() method of the Publisher object returns the view of the video you publish. Add it as a subview of an android.view.ViewGroup object to display it in the app:


Pass the Publisher object into the Session.publish() method to publish a stream to a session. See Publishing a stream.

You can call other methods of the Publisher.Builder object to define custom settings for the publisher:

mPublisher = new Publisher.Builder(context)

Note that in sessions that use the OpenTok Media Router (sessions with the media mode set to routed), lowering the frame rate proportionally reduces the bandwidth the stream uses. However, in sessions that have the media mode set to relayed, lowering the frame rate does not reduce the stream's bandwidth.

You can use a custom video capturer to publish a stream with a customized video source — see Using a custom video capturer. You can also use the custom video capturer to publish a screen-sharing stream — see Screen-sharing.

You can also use a customized audio source for the published stream — see Using a custom audio driver.

Previewing the publisher's video before streaming

Note that, by default, the view of the Publisher contains the video of the publisher when the publisher starts streaming to the session. To see a preview of the video before the Publisher starts streaming, call the startPreview() method of the Publisher object:


If you call the startPreview() method, you must call the destroy() method of the Publisher to remove the Publisher's view (and the video), when the publisher stops streaming (when the onStreamDestroyed(PublisherKit publisher, Stream stream) method of the PublisherListener is called).

Publishing a stream

Once you create a Publisher object (See Initializing a Publisher object), you can pass it into the publish(PublisherKit publisher) method of a Session object to publish the stream to the session:


This code assumes that mSession is a Session object, and that the client has connected to the session. For more information, see Joining a Session.

The PublisherKit.PublisherListener.onStreamCreated(PublisherKit publisher, Stream stream) method is called when the publisher starts streaming to the session:

public void onStreamCreated(publisher, stream) {
   // The publisher started streaming.

Stopping a publisher from streaming

You can stop publisher from streaming to the session by calling the unpublish(PublisherKit publisher) method of the Session object:


The PublisherKit.PublisherListener.onStreamDestroyed(PublisherKit publisher, Stream stream) method is called when the publisher stops streaming to the session:

public void onStreamDestroyed(publisher, stream) {
   // The publisher stopped streaming.

Testing a publisher's stream

You can publish a test stream and check its audio and video statistics to determine the type of stream (such as high-resolution or audio-only) supported by your connection.

To get statistics for a stream published by the local client, you must use a session that uses the OpenTok Media Router (sessions with the media mode set to routed). You can then use the SubscriberKit.setAudioStatsListener(AudioStatsListener listener) and SubscriberKit.setVideoStatsListener(VideoStatsListener listener) methods of the Subscriber object to get audio and video statistics for the stream you publish. See this topic for more information.

The opentok-network-test repo includes sample code for showing how to use statistics of a test stream before publishing to a session.