|
本帖最后由 廖斌 于 2013-10-12 14:07 编辑
下面android端为例,展示实现视频呼叫业务逻辑的整个过程(代码仅供参考,根据实际情况调用)
一、设置呼叫回调事件接收
用户收到呼叫请求、请求回复、会话开始、会话结束等事件都是在呼叫回调事件中处理的。
1、首先要在目标activity中实现AnyChatVideoCallEvent接口
2、然后在接收之前设置回调接收anychat.SetVideoCallEvent(this);
二、发送呼叫请求
1、用户A请求呼叫用户B,调用如下代码:- anychat.VideoCallControl(AnyChatDefine.BRAC_VIDEOCALL_EVENT_REQUEST, useridB, 0, 0,0, "usernameA")
复制代码 发送呼叫请求。
其中“AnyChatDefine.BRAC_VIDEOCALL_EVENT_REQUEST”为请求呼叫类型,“useridB”为用户B的userId,“usernameA”是自定义参数(这里可以传送A的userName)。
2、服务器接收到之后会判断B用户是否在线、是否正在呼叫,并返回“呼叫请求回复”给A,A触发OnAnyChatVideoCallEvent。- public void OnAnyChatVideoCallEvent(int dwEventType, final int dwUserId,
- int dwErrorCode, final int dwFlags, final int dwParam, final String userStr) {
- // TODO Auto-generated method stub
- switch(dwEventType)
- {
- case AnyChatDefine.BRAC_VIDEOCALL_EVENT_REPLY:
- switch(dwErrorCode)
- {
- case AnyChatDefine.RETURNCODE_SUCCESS:
- showToast("呼叫成功,请等待");
- dialogCall=new ProgressDialog(this);
- dialogCall.setMessage("呼叫成功,请等待");
- dialogCall.setButton("取消呼叫", new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- anychat.VideoCallControl(AnyChatDefine.BRAC_VIDEOCALL_EVENT_REPLY, dwUserId, AnyChatDefine.RETURNCODE_SESSION_QUIT, dwFlags, 30, anychat.GetUserName(-1));
- }
- });
- dialogCall.show();
-
- break;
- case AnyChatDefine.RETURNCODE_SESSION_BUSY:
- showToast("目标用户忙");
- break;
- case AnyChatDefine.RETURNCODE_SESSION_DISCONNECT:
- showToast("网络断线");
- break;
- case AnyChatDefine.RETURNCODE_SESSION_OFFLINE:
- showToast("目标用户不在线");
- break;
- case AnyChatDefine.RETURNCODE_SESSION_QUIT:
- showToast("源用户主动放弃会话");
- break;
- case AnyChatDefine.RETURNCODE_SESSION_REFUSE:
- showToast("目标用户拒绝会话");
- if(dialogCall!=null)
- dialogCall.dismiss();
- break;
- case AnyChatDefine.RETURNCODE_SESSION_TIMEOUT:
- showToast("会话请求超时");
- break;
-
-
- }
- break;
复制代码 3、如B是空闲的,服务器会转发“请求指令”给B,B触发OnAnyChatVideoCallEvent- switch(dwEventType)
- {
- case AnyChatDefine.BRAC_VIDEOCALL_EVENT_REQUEST:
- dialogRequest = new AlertDialog.Builder(this).setPositiveButton("同意", new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- targetUserId=String.valueOf(dwUserId);
- anychat.VideoCallControl(AnyChatDefine.BRAC_VIDEOCALL_EVENT_REPLY, dwUserId, AnyChatDefine.RETURNCODE_SUCCESS, dwFlags, 10, anychat.GetUserName(-1));
- }
- }).setNegativeButton("拒绝", new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- Log.i("ANYCHAT", "REPLY:"+"EVENT:"+AnyChatDefine.BRAC_VIDEOCALL_EVENT_REPLY+"dwUserId:"+dwUserId+"ERRORCODE:"+AnyChatDefine.RETURNCODE_SESSION_REFUSE+"NAME:"+anychat.GetUserName(-1));
- anychat.VideoCallControl(AnyChatDefine.BRAC_VIDEOCALL_EVENT_REPLY, dwUserId, AnyChatDefine.RETURNCODE_SESSION_REFUSE, dwFlags, 20, anychat.GetUserName(-1));
- }
- }).setMessage(userStr+":请求通话").create();
- dialogRequest.show();
复制代码 三、呼叫请求回复
B收到“请求指令之后”之后,可以回复A的请求。
1、同意通话,调用- anychat.VideoCallControl(AnyChatDefine.BRAC_VIDEOCALL_EVENT_REPLY, useridA, AnyChatDefine.RETURNCODE_SUCCESS, dwFlags, 10, anychat.GetUserName(-1));
复制代码 其中useridA为用户A的userId,10、anychat.GetUserName(-1)都是用户自定义参数
2、拒绝通话,调用
anychat.VideoCallControl(AnyChatDefine.BRAC_VIDEOCALL_EVENT_REPLY, useridA, AnyChatDefine.RETURNCODE_SESSION_REFUSE, dwFlags, 20, anychat.GetUserName(-1));
其中useridA为用户A的userId,20、anychat.GetUserName(-1)都是用户自定义参数。A端触发OnAnyChatVideoCallEvent,可以得知B拒绝通话- case AnyChatDefine.BRAC_VIDEOCALL_EVENT_REPLY:
- switch(dwErrorCode)
- {
- case AnyChatDefine.RETURNCODE_SESSION_REFUSE:
- showToast("目标用户拒绝会话");
- if(dialogCall!=null)
- dialogCall.dismiss();
- break;
复制代码 3、取消呼叫
A端此时可以取消呼叫,调用- anychat.VideoCallControl(AnyChatDefine.BRAC_VIDEOCALL_EVENT_REPLY, useridB, AnyChatDefine.RETURNCODE_SESSION_QUIT, dwFlags, 30, anychat.GetUserName(-1));
复制代码 B端将触发OnAnyChatVideoCallEvent- case AnyChatDefine.BRAC_VIDEOCALL_EVENT_REPLY:
- switch(dwErrorCode)
- {
- case AnyChatDefine.RETURNCODE_SESSION_QUIT:
- showToast("源用户主动放弃会话");
- break;
复制代码 四、会话开始
B端发送“同意会话回复”之后,服务器端将向A、B同时发送“会话开始”指令,A和B将触发OnAnyChatVideoCallEvent- public void OnAnyChatVideoCallEvent(int dwEventType, final int dwUserId,
- int dwErrorCode, final int dwFlags, final int dwParam, final String userStr) {
- // TODO Auto-generated method stub
- switch(dwEventType)
- {
- case AnyChatDefine.BRAC_VIDEOCALL_EVENT_START:
- anychat.LeaveRoom(-1);
- Intent intent=new Intent();
- intent.putExtra("UserID",targetUserId);
- intent.putExtra("ROOMID",dwParam);
- intent.setClass(RoomActivity.this, VideoActivity.class);
- startActivity(intent);
- break;
复制代码 其中dwParam是roomId,A和B收到之后可以进入同一房间进行音视频通话。
五、会话结束
B或者A任何一方发送会话结束指令,调用- anychat.VideoCallControl(AnyChatDefine.BRAC_VIDEOCALL_EVENT_FINISH, userID, 0, 0, 0, "");
复制代码 其中userID为对方的useId.此时A、B将会触发“呼叫结束事件”OnAnyChatVideoCallEvent- public void OnAnyChatVideoCallEvent(int dwEventType, int dwUserId,
- int dwErrorCode, int dwFlags, int dwParam, String userStr) {
- // TODO Auto-generated method stub
- public void OnAnyChatVideoCallEvent(int dwEventType, final int dwUserId,
- int dwErrorCode, final int dwFlags, final int dwParam, final String userStr) {
- // TODO Auto-generated method stub
- switch(dwEventType)
- {
- case AnyChatDefine.BRAC_VIDEOCALL_EVENT_FINISH:
- Toast.makeText(this, "通话结束",Toast.LENGTH_LONG).show();
- this.finish();
- }
复制代码 六、附录,errorcode定义
public static final int RETURNCODE_SUCCESS=0;
public static final int RETURNCODE_SESSION_QUIT = 100101;// 源用户主动放弃会话
public static final int RETURNCODE_SESSION_OFFLINE = 100102;// 目标用户不在线
public static final int RETURNCODE_SESSION_BUSY = 100103;// 目标用户忙
public static final int RETURNCODE_SESSION_REFUSE = 100104;// 目标用户拒绝会话
public static final int RETURNCODE_SESSION_TIMEOUT = 100105;//会话请求超时
public static final int RETURNCODE_SESSION_DISCONNECT= 100106;// 网络断线 |
|