使用 TAPI3 C# 接听和挂断电话

Answer and Hangup a call using TAPI3 C#

我被困在 TAPI 编程中。我创建了一个程序来监控 activity 的 phone 调用。一切正常,但现在我想实现一个功能来直接接受和拒绝来自网络的呼叫。

我所做的如下:

namespace Shared
{
    public partial class Site : System.Web.UI.MasterPage
    {
        public static ITAddress ln;
        static int index = -1;
        static int line;
        static ITAddress[] ia;
        protected void Page_Load(object sender, EventArgs e)
        {
            #region TAPI
            TAPIClass tobj;
            int[] registertoken;
            tobj = new TAPI3Lib.TAPIClass();
            tobj.Initialize();

            IEnumAddress ea = tobj.EnumerateAddresses();

            uint lines;


            uint arg3 = 0;
            int TotalLines = 0;
            lines = 0;
            foreach (TAPI3Lib.ITAddress ad in (tobj.Addresses as TAPI3Lib.ITCollection))
            {
                TotalLines++;
            }

            callnotification cn = new callnotification();
            tobj.ITTAPIEventNotification_Event_Event += new TAPI3Lib.ITTAPIEventNotification_EventEventHandler(cn.Event);
            tobj.EventFilter = (int)(TAPI_EVENT.TE_CALLNOTIFICATION |
                TAPI_EVENT.TE_DIGITEVENT |
                TAPI_EVENT.TE_PHONEEVENT |
                TAPI_EVENT.TE_CALLSTATE |
                TAPI_EVENT.TE_GENERATEEVENT |
                TAPI_EVENT.TE_GATHERDIGITS |
                TAPI_EVENT.TE_REQUEST);


            registertoken = new int[TotalLines];
            ia = new TAPI3Lib.ITAddress[TotalLines];
            for (int i = 0; i = 0)
                                    {
                                        ln = ia[line];
                                    }
                                    IEnumCall ec = ln.EnumerateCalls();
                                    uint arg = 0;
                                    ITCallInfo ici;
                                    try
                                    {
                                        ec.Next(1, out ici, ref arg);
                                        ITBasicCallControl2 bc = (TAPI3Lib.ITBasicCallControl2)ici;
                                        if (ici != null && ici.CallState == CALL_STATE.CS_OFFERING)
                                        {
                                            if (bc != null)
                                            {
                                                bc.Answer();
                                            }
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        COMException comEx = ex as COMException;
                                        if (comEx != null)
                                            comEx.ErrorCode.ToString();
                                        else
                                        {
                                            string aa = ex.Message;
                                        }
                                    }

                                    //addtolist("Call Offering from " + callernumber + " to Ext " + viaextension + " via DID " + DIDNumber);
                                    break;
                                case TAPI3Lib.CALL_STATE.CS_IDLE:
                                    //addtolist("Call is created!");
                                    break;
                            }
                            break;
                    }
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.StackTrace.ToString());
                }
            }
        }
        #endregion
    }
}

我总是得到 ITBasicCallControl2 bc NULL,当我按下接受按钮时,没有任何反应。

好的,为了成功接听电话,您要做的第一件事就是注册拥有所有者权限的线路。

tobj.RegisterCallNotifications(ln, true, true, TapiConstants.TAPIMEDIATYPE_AUDIO, 2);

之后,您可以使用 EnumerateCalls() 遍历每个调用,或者您可以实现 ITTAPIEventNotification 接口,以便在调用状态发生变化时获得通知(例如)。 不管怎样,在某个时候,您找到了要接听的电话。现在您需要确保呼叫处于警报状态(CS_OFFERING 对于入站呼叫),然后才能最终调用应答方法。

        try
        {
            ec.Next(1, out ici, ref arg);
            if (ici != null && ici.CallState == CALL_STATE.CS_OFFERING)
            {
                ITBasicCallControl2 bc = (TAPI3Lib.ITBasicCallControl2)ici;
                if (bc != null)
                {
                    bc.Answer();
                }
            }
        }
        catch (Exception exp)
        {
            COMException comEx = exp as COMException;
            if (comEx != null)
                MessageBox.Show(comEx.ErrorCode.ToString());
            else
                MessageBox.Show(exp.Message);
        }

如果您要接听的呼叫不在呼叫状态 CS_CONNECTED,该方法将抛出错误代码为 0x800040010 的 COMException。

有关 ITTAPIEventNotification 接口的更多信息,请参阅 https://msdn.microsoft.com/en-us/library/windows/desktop/ms732506(v=vs.85).aspx

编辑:

如果你想检测新来电,我建议使用 TE_CALLNOTIFICATION-Event,因为它只在每个新来电时触发一次。 每次调用状态更改时都会触发 TE_CALLSTATE-Event。

现在,我更新了呼叫通知class:

    public class callnotification : TAPI3Lib.ITTAPIEventNotification
    {
        public InboundCall OnNewIncomingCall;

        public void Event(TAPI_EVENT TapiEvent, object pEvent)
        {
            switch (TapiEvent)
            {
                case TAPI_EVENT.TE_CALLNOTIFICATION:
                    this.OnCallNotification((ITCallNotificationEvent)pEvent);
                    break;
            }
        }

        private void OnCallNotification(ITCallNotificationEvent callNotification)
        {
            ITCallInfo ici = callNotification.Call;

            if (ici != null && ici.CallState == CALL_STATE.CS_OFFERING)
                this.OnNewIncomingCall(ici);
        }
    }

我还声明了一个在有​​新的入站呼叫时使用的委托方法:

public delegate void InboundCall(ITCallInfo ici);

因此您对 callnotification 事件的初始化可能如下所示:

        callnotification cn = new callnotification();
        cn.OnNewIncomingCall += this.OnNewIncomingCall;

最后,在 OnNewIncomingCallMethod 中,您可以接听电话:

    private void OnNewIncomingCall(ITCallInfo ici)
    {
        ITBasicCallControl bcc = (ITBasicCallControl)ici;
        if (bcc != null)
        {
            string caller = ici.get_CallInfoString(CALLINFO_STRING.CIS_CALLERIDNUMBER);
            DialogResult dlg = MessageBox.Show(string.Format("New incoming call from {0}\r\nDo you wish to answer the call now?", caller), "New incoming call", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dlg == System.Windows.Forms.DialogResult.Yes)
                bcc.Answer();
        }
    }

我已经用我添加的代码测试了你的代码,它运行良好。如果您在回答时或初始化期间有任何异常,请告诉我。