如何使用lync sdk发起视频通话?

时间:2017-09-06 13:24:29

标签: visual-studio c#-4.0 video-processing lync-2013 lync-client-sdk

我的目标:我想从头开始发起视频通话。

我的问题:它已启动进入音频通话,然后在最终用户接听电话后转为视频。

void ConversationManager_ConversationAdded_Video(object sender, ConversationManagerEventArgs e)
{
    Console.WriteLine("Inside  conversation added for Video");

    if (e.Conversation.Modalities[ModalityTypes.AudioVideo].State != ModalityState.Notified)
    {

        if (e.Conversation.CanInvoke(ConversationAction.AddParticipant))
        {
            try
            {
                e.Conversation.ParticipantAdded += Conversation_ParticipantAdded_Video;
                e.Conversation.AddParticipant(client.ContactManager.GetContactByUri(receipient));

            }
            catch (ItemAlreadyExistException ex)
            {
            }

        }
    }
}

void Conversation_ParticipantAdded_Video(object source, ParticipantCollectionChangedEventArgs data)
{
    if (data.Participant.IsSelf != true)
    {
        if (((Conversation)source).Modalities[ModalityTypes.AudioVideo].CanInvoke(ModalityAction.Connect))
        {
            object[] asyncState = { ((Conversation)source).Modalities[ModalityTypes.AudioVideo], "CONNECT" };
            try
            {
                ((Conversation)source).Modalities[ModalityTypes.AudioVideo].ModalityStateChanged += _AVModality_ModalityStateChanged_Video;
                Console.WriteLine("entered video Satheesh participant added");

               // ((Conversation)source).Modalities[ModalityTypes.AudioVideo].BeginConnect(ModalityCallback, asyncState);
                ((Conversation)source).Modalities[ModalityTypes.AudioVideo].BeginConnect(ModalityCallback, asyncState);
                //((Conversation)source).Modalities[ModalityTypes.AudioVideo].EndConnect(ModalityCallback, asyncState);
                Thread.Sleep(6000);
                Console.WriteLine(source);


                Console.WriteLine("entered video participant added");
            }
            catch (LyncClientException lce)
            {
                throw new Exception("Lync Platform Exception on BeginConnect: " + lce.Message);
            }
        }
    }
}



public void ModalityCallback(IAsyncResult ar)
{
    Object[] asyncState = (Object[])ar.AsyncState;
    try
    {
        if (ar.IsCompleted == true)
        {
            if (asyncState[1].ToString() == "RETRIEVE")
            {
                ((AVModality)asyncState[0]).EndRetrieve(ar);
            }
            if (asyncState[1].ToString() == "HOLD")
            {
                ((AVModality)asyncState[0]).EndHold(ar);
            }
            if (asyncState[1].ToString() == "CONNECT")
            {
                Console.WriteLine("inside connect method CONNECT");
                ((AVModality)asyncState[0]).EndConnect(ar);
            }
            if (asyncState[1].ToString() == "FORWARD")
            {
                ((AVModality)asyncState[0]).EndForward(ar);
            }
            if (asyncState[1].ToString() == "ACCEPT")
            {
                ((AVModality)asyncState[0]).Accept();
            }

        }
    }
    catch (LyncClientException)
    { }
}

public void _AVModality_ModalityStateChanged_Video(object sender, ModalityStateChangedEventArgs e)
{
    Console.WriteLine(sender);

    Console.WriteLine("entered video modality changed");
    switch (e.NewState)
    {

        case ModalityState.Connected:
            if (_VideoChannel == null)
            {

                _VideoChannel = ((AVModality)sender).VideoChannel;
                _VideoChannel.StateChanged += new EventHandler<ChannelStateChangedEventArgs>(_VideoChannel_StateChanged);
            }
            if (_VideoChannel.CanInvoke(ChannelAction.Start))
            {
                Console.WriteLine("entered video modality changed123");
                _VideoChannel.BeginStart(MediaChannelCallback, _VideoChannel);

            }
            break;

        case ModalityState.OnHold:
            break;
        case ModalityState.Connecting:
        case ModalityState.Forwarding:
            break;
        case ModalityState.Transferring:
            break;
    }
}

private void MediaChannelCallback(IAsyncResult ar)
{

    ((VideoChannel)ar.AsyncState).EndStart(ar);
}

1 个答案:

答案 0 :(得分:0)

我认为这是因为您在ParticipantAdded事件中调用了_AVModality_ModalityStateChanged_Video。

表示当您呼叫的人(新参与者)加入对话时开始播放视频。这就是为什么它开始进入音频呼叫,然后在最终用户接听电话后变成视频的原因。

解决方案是触发一个新事件: ConversationStateChangedEvent

这是我做到的地方,并且运作良好:

/// <summary>
/// Handles event raised when New meetNow window change it's state 
/// </summary>
private void _NewMeetNowConversation_StateChanged(object sender, ConversationStateChangedEventArgs e)
{
    Conversation conference = (Conversation)sender;
    switch(e.NewState)
    {
        case ConversationState.Active:
            conference.Modalities[ModalityTypes.AudioVideo].ModalityStateChanged += AVConferenceModalityStateChanged;
            conference.ParticipantAdded += ConfParticipantAdded;
            break;
        case ConversationState.Terminated:      //conversation window completely closed
            break;
        case ConversationState.Inactive:   

            break;
        case ConversationState.Parked:

            break;
        case ConversationState.Invalid:

            break;
    }
}