音频文件未在通话中播放,而是在本地系统上播放 - Skype for Business (C#)

Audio file not played on the call but on local system - Skype for Business (C#)

我找到了一些代码 并将其修改为使用 Skype 呼叫 phone 号码,播放音频文件然后断开连接。但是,此代码中存在两个问题。

  1. 正在播放的音频文件在本地可以听到 但不在 phone 通话中(接听电话的人无法听到播放的音频)。
  2. 音频文件完成后通话未断开 正在播放。

    using Microsoft.Lync.Model;
    using Microsoft.Lync.Model.Conversation;
    using Microsoft.Lync.Model.Conversation.AudioVideo;
    using Microsoft.Lync.Model.Device;
    using Microsoft.Lync.Model.Extensibility;
    using System;
    using System.Collections.Generic;
    using System.Threading;
    using System.Windows.Forms;
    
    namespace LyncTest
    {
    public partial class frmCaller : Form
    {
        public frmCaller()
        {
            InitializeComponent();
        }
    
    private void btnCall_Click(object sender, EventArgs e)
    {
        //if this client is in UISuppressionMode...
        if (client.InSuppressedMode && client.State == ClientState.Uninitialized)
        {
            //...need to initialize it
            try
            {
                client.BeginInitialize(this.ClientInitialized, null);
            }
            catch (LyncClientException lyncClientException)
            {
                Console.WriteLine(lyncClientException);
            }
            catch (SystemException systemException)
            {
                if (LyncModelExceptionHelper.IsLyncException(systemException))
                {
                    // Log the exception thrown by the Lync Model API.
                    Console.WriteLine("Error: " + systemException);
                }
                else
                {
                    // Rethrow the SystemException which did not come from the Lync Model API.
                    throw;
                }
            }
        }
        else //not in UI Suppression, so the client was already initialized
        {
            //sign-in or contact selection
            SignInToLync();
        }
        SendLyncCall("+6512345678", "Hello, I am calling regarding a pending change request");
    }
    
    LyncClient client = LyncClient.GetClient();
    private void SignInToLync()
    {
        try
        {
            client.BeginSignIn("abc@contoso.com", "abc@contoso.com", "Pass@word99", HandleEndSignIn, null);
        }
        catch (LyncClientException lyncClientException)
        {
            Console.WriteLine(lyncClientException);
        }
        catch (SystemException systemException)
        {
            if (LyncModelExceptionHelper.IsLyncException(systemException))
            {
                // Log the exception thrown by the Lync Model API.
                Console.WriteLine("Error: " + systemException); 
            }
            else
            {
                // Rethrow the SystemException which did not come from the Lync Model API.
                throw;
            }
        }
    }
    
    Automation _automation = LyncClient.GetAutomation();
    ConversationWindow globalConv = null;
    public void SendLyncCall(string numberToCall, string textToSpeech)
    {
        var targetContactUris = new List<string> { numberToCall }; //"tel:+4900000000" 
    
        _automation.BeginStartConversation(AutomationModalities.Audio, targetContactUris, null, StartConversationCallback, null);
    
        while (this.globalConv == null)
        {
            Thread.Sleep(1);
        }
        if (globalConv != null)
        {
            //client.DeviceManager.EndPlayAudioFile(
            //  client.DeviceManager.BeginPlayAudioFile(@"C:\Temp\voice.wav", AudioPlayBackModes.AlertAndCommunication, false, AudioPlayed, null)
            //  );
        }
    }
    
    private void StartConversationCallback(IAsyncResult asyncop)
    {
        // this is called once the dialing completes..
        if (asyncop.IsCompleted == true)
        {
            ConversationWindow newConversationWindow = _automation.EndStartConversation(asyncop);
            globalConv = newConversationWindow;
            AVModality avModality = newConversationWindow.Conversation.Modalities[ModalityTypes.AudioVideo] as AVModality;
            avModality.ModalityStateChanged += ConversationModalityStateChangedCallback;
        }
    }
    
    /// <summary>
    /// Called when the client in done initializing.
    /// </summary>
    /// <param name="result"></param>
    private void ClientInitialized(IAsyncResult result)
    {
        //registers for conversation related events
        //these events will occur when new conversations are created (incoming/outgoing) and removed
        //client.ConversationManager.ConversationAdded += ConversationManager_ConversationAdded;
        //client.ConversationManager.ConversationRemoved += ConversationManager_ConversationRemoved;
    }
    
    private void ConversationModalityStateChangedCallback(object sender, ModalityStateChangedEventArgs e)
    {
        AVModality avModality = sender as AVModality;
        if (avModality != null)
        {
            switch (e.NewState)
            {
                case ModalityState.Disconnected:
                    avModality.ModalityStateChanged -= ConversationModalityStateChangedCallback;
                    break;
    
                case ModalityState.Connected:
                    avModality.ModalityStateChanged -= ConversationModalityStateChangedCallback;
                    //foreach (char c in "SOS")
                    //{
                    //    avModality.AudioChannel.BeginSendDtmf(c.ToString(), null, null);
                    //    System.Threading.Thread.Sleep(500);
                    //}
                    client.DeviceManager.EndPlayAudioFile(client.DeviceManager.BeginPlayAudioFile(@"C:\Temp\voice.wav",
                        AudioPlayBackModes.Communication, false, AudioPlayed, null));
                    break;
                case ModalityState.Invalid:
                    break;
                case ModalityState.Notified:
                    break;
            }
        }
    }
    
    private void AudioPlayed(IAsyncResult audioPlayed)
    {
        if(audioPlayed.IsCompleted == true)
        {
            client.ConversationManager.Conversations[0].End();
        }
    }
    private void HandleEndSignIn(IAsyncResult ar)
    {
        try
        {
            client.EndSignIn(ar);
        }
        catch (Exception e)
        {
            Console.Out.WriteLine(e);
        }
    }
    
    private void frmCaller_FormClosing(object sender, FormClosingEventArgs e)
    {
        GC.Collect();
    }
    }
    }
    

如有任何帮助,我们将不胜感激。谢谢。

Microsoft 已确认使用客户端代码无法做到这一点。我需要使用 UCMA 并为此开发服务器端解决方案。