当前位置: 代码迷 >> Android >> 200行Java代码兑现Android下的视频通话
  详细解决方案

200行Java代码兑现Android下的视频通话

热度:33   发布时间:2016-05-01 11:23:41.0
200行Java代码实现Android下的视频通话

    最近学习Android平台下跨平台音视频通信开发,虽然网上有很多开源项目供我们参考学习,但音视频效果很一般,还有很多不稳定的因素,毕竟是开源嘛。在国内我找到了一个比较好音视频通信解决方案(点击下载官方demo),该案例提供了纯Java语言接口供我们调用,随后我参照官方android demo程序和开发文档并结合自己的见解写了一个android音频通信软件,200行以内代码就搞定了(难以相信吧)。其实我只是调用其相关API即可实现。以下是我自己写的Java代码,以备大家互相学习:

  1. /** 
  2.  * Android视频聊天  
  3.  * 1、初始化SDK 2、连接服务器、 3、用户登录;4、进入房间;5、打开本地视频;6、请求对方视频 
  4.  */ 
  5. public class VideoChatActivity extends Activity implements AnyChatBaseEvent 
  6.     private AnyChatCoreSDK anychat;         // 核心SDK 
  7.     private SurfaceView remoteSurfaceView;  // 对方视频 
  8.     private SurfaceView localSurfaceView;   // 本地视频 
  9.     private ConfigEntity configEntity; 
  10.     private boolean bSelfVideoOpened = false;   // 本地视频是否已打开 
  11.     private boolean bOtherVideoOpened = false;  // 对方视频是否已打开 
  12.     private TimerTask mTimerTask;               // 定时器 
  13.     private Timer mTimer = new Timer(true); 
  14.     private Handler handler;                    // 用Handler来不间断刷新即时视频 
  15.     private List<String> userlist = new ArrayList<String>();//保存在线用户列表 
  16.     private int userid;                         // 用户ID 
  17.     @Override 
  18.     public void onCreate(Bundle savedInstanceState) 
  19.     { 
  20.         super.onCreate(savedInstanceState); 
  21.         setContentView(R.layout.activity_video_chat); 
  22.         remoteSurfaceView = (SurfaceView) findViewById(R.id.surface_remote); 
  23.         localSurfaceView = (SurfaceView) findViewById(R.id.surface_local); 
  24.         configEntity = ConfigService.LoadConfig(this);//加载视频通话设置 
  25.         loginSystem();// 初始化SDK 连接服务器 
  26.         mTimerTask = new TimerTask(){ 
  27.             public void run(){ 
  28.                 Message mesasge = new Message(); 
  29.                 handler.sendMessage(mesasge); 
  30.             } 
  31.         }; 
  32.         mTimer.schedule(mTimerTask, 1000100); 
  33.         handler = new Handler(){ 
  34.             @Override 
  35.             public void handleMessage(Message msg){ 
  36.                 VideoChat();// 不间断显示即时视频通话画面 
  37.                 super.handleMessage(msg); 
  38.             } 
  39.         }; 
  40.     } 
  41.     // 初始化SDK 连接服务器 
  42.     private void loginSystem(){ 
  43.         if (anychat == null){ 
  44.             anychat = new AnyChatCoreSDK(); 
  45.             anychat.SetBaseEvent(this);         // 设置基本事件回调函数 
  46.             if (configEntity.useARMv6Lib != 0)  // 使用ARMv6指令集 
  47.                 anychat.SetSDKOptionInt(AnyChatDefine. 
  48.                         BRAC_SO_CORESDK_USEARMV6LIB, 1); 
  49.             anychat.InitSDK(android.os.Build.VERSION.SDK_INT, 0); // 初始化SDK 
  50.         } 
  51.         anychat.Connect("demo.anychat.cn"8906);// 连接服务器 
  52.     } 
  53.     // 显示即时视频通话画面 
  54.     public void VideoChat(){ 
  55.         if (!bOtherVideoOpened){ 
  56.             if (anychat.GetCameraState(userid) == 2 
  57.                     && anychat.GetUserVideoWidth(userid) != 0){ 
  58.                 SurfaceHolder holder = remoteSurfaceView.getHolder(); 
  59.                 holder.setFormat(PixelFormat.RGB_565); 
  60.                 holder.setFixedSize(anychat.GetUserVideoWidth(userid), 
  61.                         anychat.GetUserVideoHeight(userid)); 
  62.                 Surface s = holder.getSurface();            // 获得视频画面 
  63.                 anychat.SetVideoPos(userid, s, 0000); // 调用API显示视频画面 
  64.                 bOtherVideoOpened = true
  65.             } 
  66.         } 
  67.         if (!bSelfVideoOpened){ 
  68.             if (anychat.GetCameraState(-1) == 2 
  69.                     && anychat.GetUserVideoWidth(-1) != 0){ 
  70.                 SurfaceHolder holder = localSurfaceView.getHolder(); 
  71.                 holder.setFormat(PixelFormat.RGB_565); 
  72.                 holder.setFixedSize(anychat.GetUserVideoWidth(-1), 
  73.                         anychat.GetUserVideoHeight(-1)); 
  74.                 Surface s = holder.getSurface(); 
  75.                 anychat.SetVideoPos(-1, s, 0000); 
  76.                 bSelfVideoOpened = true
  77.             } 
  78.         } 
  79.     } 
  80.     public void OnAnyChatConnectMessage(boolean bSuccess){ 
  81.         if (!bSuccess){ 
  82.             Toast.makeText(VideoChatActivity.this"连接服务器失败,自动重连,请稍后...", Toast.LENGTH_SHORT).show(); 
  83.         } 
  84.         anychat.Login("android""");               // 服务器连接成功 用户登录 
  85.     } 
  86.     public void OnAnyChatLoginMessage(int dwUserId, int dwErrorCode){ 
  87.         if (dwErrorCode == 0) { 
  88.             Toast.makeText(this"登录成功!", Toast.LENGTH_SHORT).show(); 
  89.             anychat.EnterRoom(1"");               // 用户登录成功 进入房间 
  90.             ApplyVideoConfig(); 
  91.         } else { 
  92.             Toast.makeText(this"登录失败,错误代码:" + dwErrorCode, Toast.LENGTH_SHORT).show(); 
  93.         } 
  94.     } 
  95.     public void OnAnyChatEnterRoomMessage(int dwRoomId, int dwErrorCode){ 
  96.         if (dwErrorCode == 0) {                     // 进入房间成功  打开本地音视频 
  97.             Toast.makeText(this"进入房间成功", Toast.LENGTH_SHORT).show(); 
  98.             anychat.UserCameraControl(-11);       // 打开本地视频 
  99.             anychat.UserSpeakControl(-11);        // 打开本地音频 
  100.         } else { 
  101.             Toast.makeText(this"进入房间失败,错误代码:" + dwErrorCode, Toast.LENGTH_SHORT).show(); 
  102.         } 
  103.     } 
  104.     public void OnAnyChatOnlineUserMessage(int dwUserNum, int dwRoomId){ 
  105.         if (dwRoomId == 1){ 
  106.             int user[] = anychat.GetOnlineUser(); 
  107.             if (user.length != 0){ 
  108.                 for (int i = 0; i < user.length; i++){ 
  109.                     userlist.add(user[i]+""); 
  110.                 } 
  111.                 String temp =userlist.get(0); 
  112.                 userid = Integer.parseInt(temp); 
  113.                 anychat.UserCameraControl(userid, 1);// 请求用户视频 
  114.                 anychat.UserSpeakControl(userid, 1); // 请求用户音频 
  115.             } 
  116.             else { 
  117.                 Toast.makeText(VideoChatActivity.this"当前没有在线用户", Toast.LENGTH_SHORT).show(); 
  118.             } 
  119.         } 
  120.     } 
  121.     public void OnAnyChatUserAtRoomMessage(int dwUserId, boolean bEnter){ 
  122.         if (bEnter) {//新用户进入房间 
  123.             userlist.add(dwUserId+""); 
  124.         } 
  125.         else {       //用户离开房间 
  126.             if (dwUserId == userid) 
  127.             { 
  128.                 Toast.makeText(VideoChatActivity.this"视频用户已下线", Toast.LENGTH_SHORT).show(); 
  129.                 anychat.UserCameraControl(userid, 0);// 关闭用户视频 
  130.                 anychat.UserSpeakControl(userid, 0); // 关闭用户音频 
  131.                 userlist.remove(userid+"");          //移除该用户 
  132.                 if (userlist.size() != 0
  133.                 { 
  134.                     String temp =userlist.get(0); 
  135.                     userid = Integer.parseInt(temp); 
  136.                     anychat.UserCameraControl(userid, 1);// 请求其他用户视频 
  137.                     anychat.UserSpeakControl(userid, 1); // 请求其他用户音频 
  138.                 } 
  139.             } 
  140.             else { 
  141.                 userlist.remove(dwUserId+"");&nbsp;&nbsp;&nbsp; //移除该用户 
  142.             } 
  143.         } 
  144.     } 
  145.     public void OnAnyChatLinkCloseMessage(int dwErrorCode){ 
  146.         Toast.makeText(VideoChatActivity.this"连接关闭,error:" + dwErrorCode, Toast.LENGTH_SHORT).show(); 
  147.     } 
  148.     @Override 
  149.     protected void onDestroy(){ //程序退出  
  150.         anychat.LeaveRoom(-1);  //离开房间  
  151.         anychat.Logout();       //注销登录  
  152.         anychat.Release();      //释放资源 
  153.         mTimer.cancel(); 
  154.         super.onDestroy(); 
  155.     } 
  156.     // 根据配置文件配置视频参数 
  157.     private void ApplyVideoConfig(){ 
  158.         if (configEntity.configMode == 1// 自定义视频参数配置 
  159.         { 
  160.             // 设置本地视频编码的码率(如果码率为0,则表示使用质量优先模式) 
  161.             anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_BITRATECTRL,configEntity.videoBitrate); 
  162.             if (configEntity.videoBitrate == 0
  163.             { 
  164.                 // 设置本地视频编码的质量 
  165.                 anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_QUALITYCTRL,configEntity.videoQuality); 
  166.             } 
  167.             // 设置本地视频编码的帧率 
  168.             anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_FPSCTRL,configEntity.videoFps); 
  169.             // 设置本地视频编码的关键帧间隔 
  170.             anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_GOPCTRL,configEntity.videoFps * 4); 
  171.             // 设置本地视频采集分辨率 
  172.             anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_WIDTHCTRL,configEntity.resolution_width); 
  173.             anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_HEIGHTCTRL,configEntity.resolution_height); 
  174.             // 设置视频编码预设参数(值越大,编码质量越高,占用CPU资源也会越高) 
  175.             anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_PRESETCTRL,configEntity.videoPreset); 
  176.         } 
  177.         // 让视频参数生效 
  178.         anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_APPLYPARAM,configEntity.configMode); 
  179.         // P2P设置 
  180.         anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_NETWORK_P2PPOLITIC,configEntity.enableP2P); 
  181.         // 本地视频Overlay模式设置 
  182.         anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_OVERLAY,configEntity.videoOverlay); 
  183.         // 回音消除设置 
  184.         anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_AUDIO_ECHOCTRL,configEntity.enableAEC); 
  185.         // 平台硬件编码设置 
  186.         anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_CORESDK_USEHWCODEC,configEntity.useHWCodec); 
  187.         // 视频旋转模式设置 
  188.         anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_ROTATECTRL,configEntity.videorotatemode); 
  189.         // 视频平滑播放模式设置 
  190.         anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_STREAM_SMOOTHPLAYMODE,configEntity.smoothPlayMode); 
  191.         // 视频采集驱动设置 
  192.         anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_CAPDRIVER,configEntity.videoCapDriver); 
  193.         // 本地视频采集偏色修正设置 
  194.         anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_FIXCOLORDEVIA,configEntity.fixcolordeviation); 
  195.         // 视频显示驱动设置 
  196.         anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_VIDEOSHOW_DRIVERCTRL,configEntity.videoShowDriver); 
  197.     } 
  相关解决方案