Subscribing to other client streams
We want clients to be able to subscribe to (or view) other clients’ streams in the session:
- Add a
subscriberproperty to the MainActivity class (after the declaration of thepublisherproperty):
private var subscriber: Subscriber? = null
The Subscriber class is defined in the Android SDK. It defines an object that a client uses to subscribe to (view) a stream published by another client.
- Modify the implementation of the
onStreamReceived(session, stream)method (one of the SessionListener callbacks) to include code to subscribe to other clients’ streams the session:
override fun onStreamReceived(session: Session, stream: Stream) {
Log.d(TAG, "onStreamReceived: New Stream Received ${stream.streamId} in session: ${session.sessionId}")
if (subscriber == null) {
val newSubscriber = Subscriber.Builder(this@MainActivity, stream).build().apply {
renderer.setStyle(
BaseVideoRenderer.STYLE_VIDEO_SCALE,
BaseVideoRenderer.STYLE_VIDEO_FILL
)
setSubscriberListener(subscriberListener)
}
session.subscribe(newSubscriber)
subscriber = newSubscriber
subscriberView = newSubscriber.view
}
}
Subscriber.Builder()creates a new subscriber for the givenStream.setSubscriberListener()attaches a listener for stream events.session.subscribe()starts the subscription to the remote stream.subscriberView = newSubscriber.viewplaces the video view into your layout.
- To log events Add
subscriberListenerproperty belowsessionListenerof theMainActivity:
private val subscriberListener = object : SubscriberKit.SubscriberListener {
override fun onConnected(subscriberKit: SubscriberKit) {
Log.d(TAG, "onConnected: Subscriber connected. Stream: ${subscriberKit.stream?.streamId}")
}
override fun onDisconnected(subscriberKit: SubscriberKit) {
Log.d(TAG, "onDisconnected: Subscriber disconnected. Stream: ${subscriberKit.stream?.streamId}")
}
override fun onError(subscriberKit: SubscriberKit, opentokError: OpentokError) {
Log.e(TAG, "SubscriberKit onError: ${opentokError.message}")
}
}
When another client publishes a stream to a session, this method is called, and a Stream object is passed in. The Stream class is defined in the Android SDK, and it represents an audio-video stream in the session.
The code initializes an instance of the Subscriber class, defined in the Android SDK. The Subscriber.Builder() constructor takes two parameters:
- The Android application context associated with this process.
- The Stream object (for the stream you want to view)
The Session.subscribe(subscriber) method subscribes to the stream that was received.
- Modify the implementation of the
onStreamDropped(Session session, Stream stream)method (another one of the SessionListener callbacks):
override fun onStreamDropped(session: Session, stream: Stream) {
Log.i(TAG, "onStreamDropped: Stream dropped with ID: ${stream.streamId}")
subscriber?.let {
subscriberView = null
subscriber = null
}
}
subscriberView = null removes a subscriber's view once the stream has dropped.
Basic video chat
Learn the basic concepts of the Vonage Video API platform, including how users can communicate through video, voice, and messaging. Explore a basic Vonage Video API flow.