Handle Lync Audio/Video ModalityStateChanged with VB.net

da57591e-7377-46d3-959c-005816e755c4So todays challenge was around handling the ModalityState change on the Lync 2013 client. The ModalityState change event happens during certain stages of Lync calls, and client actions. The ModalityState is part of the Microsoft.Lync.Model.Conversation namespace. We are using it to configure certain custom Lync Status’ dependant of the current state of the client.

To capture and handle these events, you need to create and event handler for the conversation manager namespace and for ModalityStateChangedEventArgs. The code below is written in VB.Net but could easily be transferred to C# etc.

In my example, I am only interested in the AudioVideo modality state changes.

Imports Microsoft.Lync.Model
Imports Microsoft.Lync.Model.Conversation
Imports Lync = Microsoft.Lync.Model.Conversation

Public Class frmMain
    Private _LyncClient As LyncClient
    Public WithEvents _ConversationMgr As Microsoft.Lync.Model.Conversation.ConversationManager
    Public WithEvents _conv As Conversation

    Private Sub MainWindow_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            _LyncClient = LyncClient.GetClient()
            _ConversationMgr = _LyncClient.ConversationManager
            DisplayCurrentState()
        Catch generatedExceptionName As ClientNotFoundException
            MessageBox.Show("client is not running")
        End Try
    End Sub

    Private Sub _ConversationMgr_ConversationAdded(ByVal sender As Object, ByVal e As Microsoft.Lync.Model.Conversation.ConversationManagerEventArgs) Handles _ConversationMgr.ConversationAdded
        AddHandler e.Conversation.Modalities(ModalityTypes.AudioVideo).ModalityStateChanged, AddressOf AVModalityStateChanged
    End Sub

    Private Sub AVModalityStateChanged(ByVal sender As Object, ByVal e As ModalityStateChangedEventArgs)
        Select Case e.NewState
            Case ModalityState.ConnectingToCaller
                'Insert your code here
            Case ModalityState.Connecting
                'Insert your code here
            Case ModalityState.Joining
                'Insert your code here
            Case ModalityState.Transferring
                'Insert your code here
            Case ModalityState.Disconnected
                'Insert your code here
            Case ModalityState.Disconnecting
                'Insert your code here
            Case ModalityState.Forwarding
                'Insert your code here
            Case ModalityState.OnHold
                'Insert your code here
            Case ModalityState.Suspended
                'Insert your code here
        End Select
    End Sub

So as you can see, it’s quite a simple concept – which holds lots of potential!

Read More