使用TAPI3 C#

时间:2017-05-16 10:00:45

标签: c# tapi

我陷入了TAPI编程。我创建了一个监控电话活动的程序。一切都很好,但现在我想实现一个功能,直接接受和拒绝来自网络的电话。

我完成的工作如下:

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,当我按下“接受”按钮时,没有任何反应。

1 个答案:

答案 0 :(得分:3)

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

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

之后,您可以使用EnumerateCalls()遍历每个调用,或者您可以实现ITTAPIEventNotification接口,以便在callstate(例如)中发生更改时收到通知。 无论如何,在某种程度上,您已找到要回答的呼叫。现在,您需要确保呼叫处于警报状态(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);
        }

如果您要回答的调用不在callstate 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。

现在,我已经更新了callnotification类:

    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();
        }
    }

我已经使用我的附加功能测试了您的代码并且运行正常。如果您在回答或初始化期间有任何异常,请告诉我。