当前位置: 首页 > 工具软件 > anychat > 使用案例 >

anychat java_AnyChatSDK 实现视频通话

欧阳骏俊
2023-12-01

packagecom.bairuitech.main;importjava.util.ArrayList;importjava.util.List;importandroid.app.Activity;importandroid.app.Dialog;importandroid.content.BroadcastReceiver;importandroid.content.Context;importandroid.content.Intent;importandroid.content.IntentFilter;importandroid.util.DisplayMetrics;importandroid.util.Log;importandroid.view.inputmethod.InputMethodManager;importcom.bairuitech.anychat.AnyChatBaseEvent;importcom.bairuitech.anychat.AnyChatCoreSDK;importcom.bairuitech.anychat.AnyChatDefine;importcom.bairuitech.anychat.AnyChatObjectDefine;importcom.bairuitech.anychat.AnyChatObjectEvent;importcom.bairuitech.anychat.AnyChatVideoCallEvent;importcom.bairuitech.common.ConfigEntity;importcom.bairuitech.common.ConfigService;importcom.bairuitech.common.DialogFactory;importcom.bairuitech.common.ScreenInfo;public class AnyChat_Login implementsAnyChatBaseEvent, AnyChatObjectEvent,

AnyChatVideoCallEvent {//private static final String mStrIP = "demo.anychat.cn";

private static final String mStrIP = "**.*.*.***"; //服务器地址private static final int mSPort = ****; //端口号private static final String TAG = "AnyChat_Login";private String mStrName = "Tom";privateString passWord;private List mobject = new ArrayList(); //装载营业厅ID

private int[] queueIds;private int USER_TYPE_ID; //0代表是进入客户界面,2代表是接入座席界面

publicAnyChatCoreSDK anyChatSDK;privateAnyChat_LoginCallBack loginCallback;privateAnyChat_GetFunRoomCallBack roomCallBack;privateAnyChat_UpdateQueueInfoCallBack updateCallBack;privateAnyChat_VideoResultCallBack videoCallBack;privateActivity context;privateQueueInfoEntity entity;private longtime;/*** 用户当前状态

*

*@authorAdministrator

**/

public enumLoginStatus {

Success, Waiting, Failure, Finish, NoNet, UnKnownErr, Reconnection

}/***

*@paramcontext

* 上下文环境

*@paramtermId

* 终端号 (用户名)

*@paramapplyId

* 业务ID(Password)*/

publicAnyChat_Login(Activity context, String termId, String applyId) {this.context =context;this.mStrName =termId;this.passWord =applyId;

entity=QueueInfoEntity.getQueueInfoEntity();

setDisPlayMetrics();

ApplyVideoConfig();

InitSDK();

}public voidsetAnyChat_LoginCallBack(AnyChat_LoginCallBack callback) {this.loginCallback =callback;

}public voidsetAnyChat_GetFunRoom(AnyChat_GetFunRoomCallBack callback) {this.roomCallBack =callback;

}public voidsetUpdateCallBack(AnyChat_UpdateQueueInfoCallBack updateCallBack) {this.updateCallBack =updateCallBack;

}public voidsetVideoCallBack(AnyChat_VideoResultCallBack videoCallBack) {this.videoCallBack =videoCallBack;

}//初始化SDK

private voidInitSDK() {if (anyChatSDK == null) {

anyChatSDK=AnyChatCoreSDK.getInstance(context);

}

anyChatSDK.SetBaseEvent(this);//基本事件

anyChatSDK.SetObjectEvent(this);//营业厅排队事件

anyChatSDK.SetVideoCallEvent(this);

anyChatSDK.InitSDK(android.os.Build.VERSION.SDK_INT,0);//初始化sdk

AnyChatCoreSDK.SetSDKOptionInt(

AnyChatDefine.BRAC_SO_LOCALVIDEO_AUTOROTATION,1);

}public voidloginAnyChat() {

loginCallback.getLoginStatus(LoginStatus.Waiting,-1);

anyChatSDK.Connect(mStrIP, mSPort);//注册广播

registerBoradcastReceiver();

}//对键盘显示进行控制

private voidhideKeyboard() {

InputMethodManager imm=(InputMethodManager) context

.getSystemService(Context.INPUT_METHOD_SERVICE);if(imm.isActive()) {

imm.hideSoftInputFromWindow(context.getCurrentFocus()

.getApplicationWindowToken(),

InputMethodManager.HIDE_NOT_ALWAYS);

}

}/*** 屏幕大小*/

private voidsetDisPlayMetrics() {

DisplayMetrics dMetrics= newDisplayMetrics();

context.getWindowManager().getDefaultDisplay().getMetrics(dMetrics);

ScreenInfo.WIDTH=dMetrics.widthPixels;

ScreenInfo.HEIGHT=dMetrics.heightPixels;

}public voidonDestroy() {

anyChatSDK.LeaveRoom(-1);

anyChatSDK.Logout();

anyChatSDK.Release();

context.unregisterReceiver(mBroadcastReceiver);

}public voidonRestart() {

anyChatSDK.SetBaseEvent(this);

anyChatSDK.SetObjectEvent(this);

}

@Overridepublic void OnAnyChatConnectMessage(booleanbSuccess) {if (!bSuccess) {

Log.e(TAG,"连接服务器失败,自动重连,请稍后...");

loginCallback.getLoginStatus(LoginStatus.Reconnection,-1);

}

anyChatSDK.Login(mStrName, passWord);

}

@Overridepublic void OnAnyChatLoginMessage(int dwUserId, intdwErrorCode) {//连接成功

if (dwErrorCode == 0) {//hideKeyboard();//保存用户id和用户角色信息

entity.selfUserName =mStrName;

entity.mUserID=dwUserId;

entity.userType=USER_TYPE_ID;

Log.e(TAG,"Connect to the server success");//初始化业务对象属性身份

InitClientObjectInfo(dwUserId);

loginCallback.getLoginStatus(LoginStatus.Success, dwUserId);

}else{

Log.e(TAG,"登录失败,errorCode:" +dwErrorCode);

loginCallback.getLoginStatus(LoginStatus.Failure,-1);

}

}//初始化服务对象事件;触发回调OnAnyChatObjectEvent函数

private void InitClientObjectInfo(intdwUserId) {//业务对象身份初始化;0代表普通客户,2是代表座席 (USER_TYPE_ID)

AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_OBJECT_INITFLAGS,

USER_TYPE_ID);//业务对象优先级设定;

int dwPriority = 10;

AnyChatCoreSDK.ObjectSetIntValue(

AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_CLIENTUSER, dwUserId,

AnyChatObjectDefine.ANYCHAT_OBJECT_INFO_PRIORITY, dwPriority);//业务对象属性设定,必须是-1;

int dwAttribute = -1;

AnyChatCoreSDK.ObjectSetIntValue(

AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_CLIENTUSER, dwUserId,

AnyChatObjectDefine.ANYCHAT_OBJECT_INFO_ATTRIBUTE, dwAttribute);//向服务器发送数据同步请求指令

AnyChatCoreSDK.ObjectControl(

AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_AREA,

AnyChatObjectDefine.ANYCHAT_INVALID_OBJECT_ID,

AnyChatObjectDefine.ANYCHAT_OBJECT_CTRL_SYNCDATA, dwUserId,0,0, 0, "");

}

@Overridepublic void OnAnyChatEnterRoomMessage(int dwRoomId, intdwErrorCode) {

}

@Overridepublic void OnAnyChatOnlineUserMessage(int dwUserNum, intdwRoomId) {

}

@Overridepublic void OnAnyChatUserAtRoomMessage(int dwUserId, booleanbEnter) {

}//网络端口

@Overridepublic void OnAnyChatLinkCloseMessage(intdwErrorCode) {

anyChatSDK.LeaveRoom(-1);

anyChatSDK.Logout();

Log.e(TAG,"连接关闭,errorCode:" +dwErrorCode);

loginCallback.getLoginStatus(LoginStatus.UnKnownErr,-1);

}//广播

private BroadcastReceiver mBroadcastReceiver = newBroadcastReceiver() {

@Overridepublic voidonReceive(Context context, Intent intent) {

String action=intent.getAction();if (action.equals("NetworkDiscon")) {

anyChatSDK.LeaveRoom(-1);

anyChatSDK.Logout();

loginCallback.getLoginStatus(LoginStatus.NoNet,-1);

}

}

};private voidregisterBoradcastReceiver() {

IntentFilter myIntentFilter= newIntentFilter();

myIntentFilter.addAction("NetworkDiscon");//注册广播

context.registerReceiver(mBroadcastReceiver, myIntentFilter);

}

@Overridepublic void OnAnyChatObjectEvent(int dwObjectType, intdwObjectId,int dwEventType, int dwParam1, int dwParam2, intdwParam3,intdwParam4, String strParam) {

Log.e("OnAnyChatObjectEvent", "----------------");switch(dwEventType) {//营业厅数据同步,回调一次返回一个营业厅对象id(有多少营业厅回调多少次)

caseAnyChatObjectDefine.ANYCHAT_OBJECT_EVENT_UPDATE://装入集合

mobject.add(dwObjectId);break;//进入营业厅结果回调

caseAnyChatObjectDefine.ANYCHAT_AREA_EVENT_ENTERRESULT:

AnyChatEnterAreaResult(dwObjectType, dwObjectId, dwParam1);break;caseAnyChatObjectDefine.ANYCHAT_AREA_EVENT_LEAVERESULT:

Log.e(TAG,"退出营业厅");

context.finish();break;//数据同步完成回调

caseAnyChatObjectDefine.ANYCHAT_OBJECT_EVENT_SYNCDATAFINISH:

DataFinshed(dwObjectType);break;caseAnyChatObjectDefine.ANYCHAT_QUEUE_EVENT_ENTERRESULT:break;caseAnyChatObjectDefine.ANYCHAT_QUEUE_EVENT_STATUSCHANGE:

updateCallBack.onUpdateQueueInfo(dwObjectId);break;

}

}//进入营业厅的回调,跳转到业务列表

private void AnyChatEnterAreaResult(int dwObjectType, intdwObjectId,intdwParam1) {//dwParam1 进入营业厅返回结果,0表示进入营业厅成功

Log.e(TAG, "TAG:" + "dwObjectType:" + dwObjectType + ";dwObjectId:"

+ dwObjectId + ";dwParam1:" +dwParam1);//客户角色

if (entity.userType == 0) {

startDeal();//座席角色

}

}private void DataFinshed(intdwObjectType) {

Log.e("TAG", "dwObjectType:" +dwObjectType);if (dwObjectType ==AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_AREA) {//适配器的数据获取

List list = new ArrayList();for (int index = 0; index < mobject.size(); index++) {

String name=AnyChatCoreSDK.ObjectGetStringValue(

AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_AREA,

mobject.get(index),

AnyChatObjectDefine.ANYCHAT_OBJECT_INFO_NAME);

list.add(name);

Log.e("TAG", "TAG:" + index + "; name:" +name);

}

roomCallBack.getAnyChat_GetFunRoom(mobject);

}

}/*** 根据配置文件配置视频参数*/

private voidApplyVideoConfig() {

ConfigEntity configEntity=ConfigService.LoadConfig(context);if (configEntity.configMode == 1) //自定义视频参数配置

{//设置本地视频编码的码率(如果码率为0,则表示使用质量优先模式)

AnyChatCoreSDK.SetSDKOptionInt(

AnyChatDefine.BRAC_SO_LOCALVIDEO_BITRATECTRL,

configEntity.videoBitrate);if (configEntity.videoBitrate == 0) {//设置本地视频编码的质量

AnyChatCoreSDK.SetSDKOptionInt(

AnyChatDefine.BRAC_SO_LOCALVIDEO_QUALITYCTRL,

configEntity.videoQuality);

}//设置本地视频编码的帧率

AnyChatCoreSDK.SetSDKOptionInt(

AnyChatDefine.BRAC_SO_LOCALVIDEO_FPSCTRL,

configEntity.videoFps);//设置本地视频编码的关键帧间隔

AnyChatCoreSDK.SetSDKOptionInt(

AnyChatDefine.BRAC_SO_LOCALVIDEO_GOPCTRL,

configEntity.videoFps* 4);//设置本地视频采集分辨率

AnyChatCoreSDK.SetSDKOptionInt(

AnyChatDefine.BRAC_SO_LOCALVIDEO_WIDTHCTRL,

configEntity.resolution_width);

AnyChatCoreSDK.SetSDKOptionInt(

AnyChatDefine.BRAC_SO_LOCALVIDEO_HEIGHTCTRL,

configEntity.resolution_height);//设置视频编码预设参数(值越大,编码质量越高,占用CPU资源也会越高)

AnyChatCoreSDK.SetSDKOptionInt(

AnyChatDefine.BRAC_SO_LOCALVIDEO_PRESETCTRL,

configEntity.videoPreset);

}//让视频参数生效

AnyChatCoreSDK.SetSDKOptionInt(

AnyChatDefine.BRAC_SO_LOCALVIDEO_APPLYPARAM,

configEntity.configMode);//P2P设置

AnyChatCoreSDK.SetSDKOptionInt(

AnyChatDefine.BRAC_SO_NETWORK_P2PPOLITIC,

configEntity.enableP2P);//本地视频Overlay模式设置

AnyChatCoreSDK.SetSDKOptionInt(

AnyChatDefine.BRAC_SO_LOCALVIDEO_OVERLAY,

configEntity.videoOverlay);//回音消除设置

AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_AUDIO_ECHOCTRL,

configEntity.enableAEC);//平台硬件编码设置

AnyChatCoreSDK.SetSDKOptionInt(

AnyChatDefine.BRAC_SO_CORESDK_USEHWCODEC,

configEntity.useHWCodec);//视频旋转模式设置

AnyChatCoreSDK.SetSDKOptionInt(

AnyChatDefine.BRAC_SO_LOCALVIDEO_ROTATECTRL,

configEntity.videorotatemode);//本地视频采集偏色修正设置

AnyChatCoreSDK.SetSDKOptionInt(

AnyChatDefine.BRAC_SO_LOCALVIDEO_FIXCOLORDEVIA,

configEntity.fixcolordeviation);//视频GPU渲染设置

AnyChatCoreSDK.SetSDKOptionInt(

AnyChatDefine.BRAC_SO_VIDEOSHOW_GPUDIRECTRENDER,

configEntity.videoShowGPURender);//本地视频自动旋转设置

AnyChatCoreSDK.SetSDKOptionInt(

AnyChatDefine.BRAC_SO_LOCALVIDEO_AUTOROTATION,

configEntity.videoAutoRotation);

}

@Overridepublic void OnAnyChatVideoCallEvent(int dwEventType, intdwUserId,int dwErrorCode, int dwFlags, intdwParam, String userStr) {

Log.e("OnAnyChatVideoCallEvent", "dwEventType:" +dwEventType+ ";dwErrorCode:" +dwErrorCode);switch(dwEventType) {caseAnyChatDefine.BRAC_VIDEOCALL_EVENT_REQUEST://呼叫请求事件

BussinessCenter.getBussinessCenter().onVideoCallRequest(dwUserId,

dwFlags, dwParam, userStr);

entity.targetUserName=anyChatSDK.GetUserName(dwUserId);

Dialog dialog=DialogFactory.getDialog(DialogFactory.DIALOGID_REQUEST,

dwUserId, context,entity);

dialog.show();break;caseAnyChatDefine.BRAC_VIDEOCALL_EVENT_REPLY://呼叫成功的时候的所做出的反应;

Log.e("queueactivity", "呼叫成功等待对方反应的回调");

BussinessCenter.getBussinessCenter().onVideoCallReply(dwUserId,

dwErrorCode, dwFlags, dwParam, userStr, videoCallBack);if (dwErrorCode ==AnyChatDefine.BRAC_ERRORCODE_SUCCESS) {

}else{

}break;caseAnyChatDefine.BRAC_VIDEOCALL_EVENT_START:

Log.e("queueactivity", "会话开始回调");

entity.TargetUserId=dwUserId;

entity.RoomId=dwParam;

BussinessCenter.getBussinessCenter().stopSessionMusic();

videoCallBack.onAnyChat_VieoResult(dwUserId);break;

}

}/*** 用户当前状态

*

*@authorAdministrator

**/

public interfaceAnyChat_LoginCallBack {public void getLoginStatus(LoginStatus status, intdwUserId);

}/*** 返回营业厅ID

*

*@authorAdministrator

**/

public interfaceAnyChat_GetFunRoomCallBack {public void getAnyChat_GetFunRoom(Listlist);

}/*** 立即办理*/

public voidstartDeal() {

queueIds=AnyChatCoreSDK

.ObjectGetIdList(AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_QUEUE);for (int i = 0; i < queueIds.length; i++) {

String name=AnyChatCoreSDK.ObjectGetStringValue(

AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_QUEUE, queueIds[i],

AnyChatObjectDefine.ANYCHAT_OBJECT_INFO_NAME);int number =AnyChatCoreSDK.ObjectGetIntValue(

AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_QUEUE, queueIds[i],

AnyChatObjectDefine.ANYCHAT_QUEUE_INFO_LENGTH);

Log.e("TAG", "业务字符名称:" + name + ";业务排队人数:" +number);

}if (queueIds != null && queueIds.length != 0) {

entity.CurrentQueueId= (queueIds[0]);//进入队列的控制指令

AnyChatCoreSDK.ObjectControl(

AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_QUEUE, queueIds[0],

AnyChatObjectDefine.ANYCHAT_QUEUE_CTRL_USERENTER,0, 0, 0,0, "");

}else{

entity.CurrentQueueId= 0;//进入队列的控制指令

AnyChatCoreSDK.ObjectControl(

AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_QUEUE,0,

AnyChatObjectDefine.ANYCHAT_QUEUE_CTRL_USERENTER,0, 0, 0,0, "");

}

}public interfaceAnyChat_UpdateQueueInfoCallBack {public void onUpdateQueueInfo(intdwObjectId);

}public interfaceAnyChat_VideoResultCallBack {public void onAnyChat_VieoResult(intobj);

}

}

 类似资料: