Kotlin

Connecting to the session

Next, we will connect to the Vonage Video session. You must do this before you can publish your audio-video stream to the session or view other participants streams.

  1. Add a session property to the MainActivity class:
private var session: Session? = null

The Session class is defined in the Android SDK. It represents a Vonage Video session and includes methods for interacting with the session.

  1. To facilitate logging, add a TAG property at the top of the MainActivity class:
companion object {
    private const val TAG = "MainActivity"
}
  1. Expand your initializeSession() function to take in 3 parameters and initialize the session, add few simple logs:
private fun initializeSession(appId: String, sessionId: String, token: String) {
    Log.i(TAG, "appId: $appId")
    Log.i(TAG, "sessionId: $sessionId")
    Log.i(TAG, "token: $token")

    session = Session.Builder(this, appId, sessionId).build().apply {
        setSessionListener(sessionListener)
        connect(token)
    }
}

This code uses the Session.Builder() to instantiate a Session object. The constructor takes three parameters:

  • The Android application context associated with this process
  • The Vonage Video APP ID
  • The session ID

The Session.Builder.build() method returns a new Session instance.

The Session.setSessionListener() method sets the object that will implement the SessionListener interface. This interface includes callback methods are called in response to session-related events. (We will implement them in the next steps).

The Session.connect() method of the session object connects the client application to the Vonage Video session. You must connect before sending or receiving audio-video streams in the session (or before interacting with the session in any way). The connect() method takes one parameter: the authentication token for this client to connect to the session.

  1. Update the usage of initializeSession() method you changed in the last step, use the following lines of code:
initializeSession(VonageVideoConfig.APP_ID, VonageVideoConfig.SESSION_ID, VonageVideoConfig.TOKEN)
  1. Next we will create the sessionListener property. Add the following code at the top of the MainActivity class:
private val sessionListener = object : Session.SessionListener {
    override fun onConnected(session: Session) {
        Log.d(TAG, "onConnected: Connected to session: ${session.sessionId}")
    }

    override fun onDisconnected(session: Session) {
        Log.d(TAG, "onDisconnected: Disconnected from session: ${session.sessionId}")
    }

    override fun onStreamReceived(session: Session, stream: Stream) {
        Log.d(TAG, "onStreamReceived: New Stream Received ${stream.streamId} in session: ${session.sessionId}")
    }

    override fun onStreamDropped(session: Session, stream: Stream) {
        Log.d(TAG, "onStreamDropped: Stream Dropped: ${stream.streamId} in session: ${session.sessionId}")
    }

    override fun onError(session: Session, opentokError: OpentokError) {
        Log.e(TAG, "Session error: ${opentokError.message}")
    }
}
  • When the client connects to the session, the implementation of the SessionListener.onConnected(session) method is called.
  • When the client disconnects from the session, the implementation of the SessionListener.onDisconnected(session) method is called.
  • If the client fails to connect to the session, the implementation of the SessionListener.onError(session, error) method is called.
  • When another client publishes a stream to the session, the implementation of the SessionListener.onStreamReceived(session, stream) method is called.
  • When another client stops publishing a stream to the session, the implementation of the SessionListener.onStreamDropped(session, stream) method is called.

For now, the app prints to the debugger console when any of these events occur.

Debug your application. If the app successfully connects to the Vonage Video session, the SessionListener.onConnected(session) method logs to the debug console.

  1. Add the methods below to the MainActivity class to notify the session about Activity lifecycle events:
override fun onPause() {
    super.onPause()
    session?.onPause()
}

override fun onResume() {
    super.onResume()
    session?.onResume()
}