1. 程式人生 > 實用技巧 >基於AnyChat的視訊會議程式

基於AnyChat的視訊會議程式

  AnyChat是一款跨平臺的音視訊解決方案。

  可以進行雙人或多人的語音實時通話,支援Windows、Web、Android、iOS、Mac、Linux等跨平臺通訊。

  所提供的SDK支援C++、Delphi、Java、C#、VB、object-c等多種語音開發。

  AnyChat包括音訊視訊錄製,拍照,伺服器錄影,文字聊天,檔案傳送等多種功能。

  介面如下

  呼叫流程:

  1.在所要監聽的類中呼叫過載WndProc方法,實現windows訊息的監聽。

/// <summary>
/// 過載
/// </summary>
/// <param name="m"></param>
protected override void WndProc(ref Message m)
{
    if (m.Msg == AnyChatCoreSDK.WM_GV_CONNECT)
    {
        //客戶端連線伺服器,表示是否連線成功
        int succed = m.WParam.ToInt32();
        //連線伺服器成功
        if (succed == 1)
        {
            //登入伺服器(在WndProc中的獲取方法回撥結果。引數:AnyChatCoreSDK.WM_GV_LOGINSYSTEM)
            int ret = AnyChatCoreSDK.Login(PublicMembers.g_Name, "", 0);
        }
        else
        {
            PublicMembers.ShowRightTip("登入失敗。錯誤程式碼:" + succed, "");
        }
    }
    else if (m.Msg == AnyChatCoreSDK.WM_GV_LOGINSYSTEM)
    {
        //客戶端登入系統,wParam(INT)表示自己的使用者ID號
        int userid = m.WParam.ToInt32();
        if (m.LParam.ToInt32() == 0)
        {
            m_myUserID = userid;
            //進入房間(在WndProc中的獲取方法回撥結果。引數:AnyChatCoreSDK.WM_GV_ENTERROOM)
            int ret = AnyChatCoreSDK.EnterRoom(m_RoomID, "", 0);
        }
        else
        {
            MessageBox.Show("登入伺服器失敗,程式碼出錯為:" + m.LParam.ToInt32(), "警告");
        }
    }
    else if (m.Msg == AnyChatCoreSDK.WM_GV_ENTERROOM)
    {
        //客戶端進入房間
        if (m.LParam.ToInt32() == 0)
        {
            //繫結本機視訊視窗 -1代表自己
            int ret = AnyChatCoreSDK.SetVideoPos(-1, picLocalVideo.Handle, 0, 0, picLocalVideo.Width, picLocalVideo.Height);
            //開啟本地視訊 -1代表自己
            ret = AnyChatCoreSDK.UserCameraControl(-1, true);
            //開啟本地聲音 -1代表自己
            ret = AnyChatCoreSDK.UserSpeakControl(-1, true);
        }
        else
        {
            MessageBox.Show("申請進入房間失敗,出錯程式碼為:" + m.LParam.ToInt32(), "警告");
        }
    }
    else if (m.Msg == AnyChatCoreSDK.WM_GV_ONLINEUSER)
    {
        //收到當前房間的線上使用者資訊,進入房間後觸發一次
        int usrcnt = m.WParam.ToInt32();
        int cnt = 0;//線上使用者數量
        AnyChatCoreSDK.GetOnlineUser(null, ref cnt);//獲取線上使用者數量
        int[] userArr = new int[cnt];//線上使用者ID
        AnyChatCoreSDK.GetOnlineUser(userArr, ref cnt);//獲取線上使用者ID陣列
    }
    else if (m.Msg == AnyChatCoreSDK.WM_GV_LINKCLOSE)
    {
        //客戶端掉線處理
    }
    else if (m.Msg == AnyChatCoreSDK.WM_GV_USERATROOM)
    {
        //使用者進入(離開)房間,wParam(INT)表示使用者ID號、
        //使用者ID
        int userID = m.WParam.ToInt32();
        //發生狀態
        int boEntered = m.LParam.ToInt32();
        if (boEntered == 1)
        {
            //進入房間
            m_others.Add(userID);
            StartVideo(userID);
        }
        else
        {
            //退出房間
            m_others.Remove(userID);
            EndVideo(userID);
        }
    }
    base.WndProc(ref m);
}

  2.初始化AnyChat的SDK

//設定回撥函式
SystemSetting.Text_OnReceive = new TextReceivedHandler(Received_CallBack);//文本回調涵數
SystemSetting.TransBuffer_OnReceive = new TransBufferReceivedHandler(Received_TransBuffer);//透明通道傳輸回撥
SystemSetting.TransFile_OnReceive = new TransFileReceivedHandler(Received_TransFile);//檔案傳輸回撥
SystemSetting.TransRecord_OnReceive = new TransRecordHandler(File_CallBack);//拍照錄像回撥函式
//初始化
SystemSetting.Init(this.Handle);
//設定核心引數  設定儲存路徑
int ret = 0;
ret = AnyChatCoreSDK.SetSDKOption(AnyChatCoreSDK.BRAC_SO_RECORD_TMPDIR, Application.StartupPath, Application.StartupPath.Length);
ret = AnyChatCoreSDK.SetSDKOption(AnyChatCoreSDK.BRAC_SO_SNAPSHOT_TMPDIR, Application.StartupPath, Application.StartupPath.Length);

  3.連線AnyChat伺服器。使用AnyChat功能必須先連線並登入AnyChat伺服器。執行連線操作後會觸發windows訊息回撥 AnyChatCoreSDK.WM_GV_CONNECT

//登入AnyChat (IP從配置檔案中獲取)
string IP = XmlHelper.GetXmlAttribute(PublicMembers.Config, "//Configuration//IP", "value").Value;
//連線伺服器(在WndProc中的獲取方法回撥結果。引數:AnyChatCoreSDK.WM_GV_CONNECT)
ret = AnyChatCoreSDK.Connect(IP, 8906);

  4.登入AnyChat伺服器。執行連線操作後會觸發windows訊息回撥AnyChatCoreSDK.WM_GV_LOGINSYSTEM

//登入伺服器(在WndProc中的獲取方法回撥結果。引數:AnyChatCoreSDK.WM_GV_LOGINSYSTEM)
int ret = AnyChatCoreSDK.Login(PublicMembers.g_Name, "", 0);

  5.伺服器登入成功後進入指定房間,只有在同一個房間內的使用者才可以進行視訊音訊互動。

//進入房間(在WndProc中的獲取方法回撥結果。引數:AnyChatCoreSDK.WM_GV_ENTERROOM)
int ret = AnyChatCoreSDK.EnterRoom(m_RoomID, "", 0);

  6.開啟,關閉音訊視訊

//繫結本機視訊視窗 -1代表自己,通過指定userId來繫結視訊視窗
int ret = AnyChatCoreSDK.SetVideoPos(-1, picLocalVideo.Handle, 0, 0, picLocalVideo.Width, picLocalVideo.Height);
//開啟本地視訊 -1代表自己
ret = AnyChatCoreSDK.UserCameraControl(-1, true);
//開啟本地聲音 -1代表自己
ret = AnyChatCoreSDK.UserSpeakControl(-1, true);

  7.傳送檔案,文字,錄製等操作

//傳送文字
int ret = AnyChatCoreSDK.SendTextMessage(-1, true, text, length);
//傳送檔案  filepath:檔案路徑
int taskId = 0;
int flag = AnyChatCoreSDK.TransFile(userId, filepath, 1, 0, 0, ref taskId);
//開啟聲音
int ret = AnyChatCoreSDK.UserSpeakControl(userId, true);
//關閉聲音
int ret = AnyChatCoreSDK.UserSpeakControl(userId, false);
//開啟視訊
int ret = AnyChatCoreSDK.UserCameraControl(userId, true);
//關閉視訊
int ret = AnyChatCoreSDK.UserCameraControl(userId, false);
//開始錄影
ulong flag = 0;//0為錄製視訊 1為錄製音訊
int ret = AnyChatCoreSDK.StreamRecordCtrl(userId, true, flag, 0);
//停止錄影
ulong flag = 0;//0為錄製視訊 1為錄製音訊
int ret = AnyChatCoreSDK.StreamRecordCtrl(userId, false, flag, 0);
//拍照
AnyChatCoreSDK.SnapShot(userId, 1, 1);

  最後原始碼奉上:http://pan.baidu.com/s/1qXxB1AO

  

  詳細程式碼可以上AnyChat官網獲取,包含SDK,開發文件,原始碼程式等

  官網地址:http://www.anychat.cn/

轉載於:https://www.cnblogs.com/Mo-MaTure/p/5111769.html