Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android的視頻通話的Java代碼

Android的視頻通話的Java代碼

編輯:關於Android編程



最近學習Android平台下跨平台音視頻通信開發,雖然網上有很多開源項目供我們參考學習,但音視頻效果很一般,還有很多不穩定的因素,畢竟是開源嘛。在國內我找到了一個比較好音視頻通信解決方案(百度下載官方Anychat for 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.{
7. private AnyChatCoreSDK anychat; // 核心SDK
8. private SurfaceView remoteSurfaceView; // 對方視頻
9. private SurfaceView localSurfaceView; // 本地視頻
10. private ConfigEntity configEntity;
11. private boolean bSelfVideoOpened = false; // 本地視頻是否已打開
12. private boolean bOtherVideoOpened = false; // 對方視頻是否已打開
13. private TimerTask mTimerTask; // 定時器
14. private Timer mTimer = new Timer(true);
15. private Handler handler; // 用Handler來不間斷刷新即時視頻
16. private List userlist = new ArrayList();//保存在線用戶列表
17. private int userid; // 用戶ID
18. @Override
19. public void onCreate(Bundle savedInstanceState)
20. {
21. super.onCreate(savedInstanceState);
22. setContentView(R.layout.activity_video_chat);
23. remoteSurfaceView = (SurfaceView) findViewById(R.id.surface_remote);
24. localSurfaceView = (SurfaceView) findViewById(R.id.surface_local);
25. configEntity = ConfigService.LoadConfig(this);//加載視頻通話設置
26. loginSystem();// 初始化SDK 連接服務器
27. mTimerTask = new TimerTask(){
28. public void run(){
29. Message mesasge = new Message();
30. handler.sendMessage(mesasge);
31. }
32. };
33. mTimer.schedule(mTimerTask, 1000, 100);
34. handler = new Handler(){
35. @Override
36. public void handleMessage(Message msg){
37. VideoChat();// 不間斷顯示即時視頻通話畫面
38. super.handleMessage(msg);
39. }
40. };
41. }
42. // 初始化SDK 連接服務器
43. private void loginSystem(){
44. if (anychat == null){
45. anychat = new AnyChatCoreSDK();
46. anychat.SetBaseEvent(this); // 設置基本事件回調函數
47. if (configEntity.useARMv6Lib != 0) // 使用ARMv6指令集
48. anychat.SetSDKOptionInt(AnyChatDefine.
49. BRAC_SO_CORESDK_USEARMV6LIB, 1);
50. anychat.InitSDK(android.os.Build.VERSION.SDK_INT, 0); // 初始化SDK
51. }
52. anychat.Connect("demo.anychat.cn", 8906);// 連接服務器
53. }
54. // 顯示即時視頻通話畫面
55. public void VideoChat(){
56. if (!bOtherVideoOpened){
57. if (anychat.GetCameraState(userid) == 2
58. && anychat.GetUserVideoWidth(userid) != 0){
59. SurfaceHolder holder = remoteSurfaceView.getHolder();
60. holder.setFormat(PixelFormat.RGB_565);
61. holder.setFixedSize(anychat.GetUserVideoWidth(userid),
62. anychat.GetUserVideoHeight(userid));
63. Surface s = holder.getSurface(); // 獲得視頻畫面
64. anychat.SetVideoPos(userid, s, 0, 0, 0, 0); // 調用API顯示視頻畫面
65. bOtherVideoOpened = true;
66. }
67. }
68. if (!bSelfVideoOpened){
69. if (anychat.GetCameraState(-1) == 2
70. && anychat.GetUserVideoWidth(-1) != 0){
71. SurfaceHolder holder = localSurfaceView.getHolder();
72. holder.setFormat(PixelFormat.RGB_565);
73. holder.setFixedSize(anychat.GetUserVideoWidth(-1),
74. anychat.GetUserVideoHeight(-1));
75. Surface s = holder.getSurface();
76. anychat.SetVideoPos(-1, s, 0, 0, 0, 0);
77. bSelfVideoOpened = true;
78. }
79. }
80. }
81. public void OnAnyChatConnectMessage(boolean bSuccess){
82. if (!bSuccess){
83. Toast.makeText(VideoChatActivity.this, "連接服務器失敗,自動重連,請稍後...", Toast.LENGTH_SHORT).show();
84. }
85. anychat.Login("android", ""); // 服務器連接成功 用戶登錄
86. }
87. public void OnAnyChatLoginMessage(int dwUserId, int dwErrorCode){
88. if (dwErrorCode == 0) {
89. Toast.makeText(this, "登錄成功!", Toast.LENGTH_SHORT).show();
90. anychat.EnterRoom(1, ""); // 用戶登錄成功 進入房間
91. ApplyVideoConfig();
92. } else {
93. Toast.makeText(this, "登錄失敗,錯誤代碼:" + dwErrorCode, Toast.LENGTH_SHORT).show();
94. }
95. }
96. public void OnAnyChatEnterRoomMessage(int dwRoomId, int dwErrorCode){
97. if (dwErrorCode == 0) { // 進入房間成功 打開本地音視頻
98. Toast.makeText(this, "進入房間成功", Toast.LENGTH_SHORT).show();
99. anychat.UserCameraControl(-1, 1); // 打開本地視頻
100. anychat.UserSpeakControl(-1, 1); // 打開本地音頻
101. } else {
102. Toast.makeText(this, "進入房間失敗,錯誤代碼:" + dwErrorCode, Toast.LENGTH_SHORT).show();
103. }
104. }
105. public void OnAnyChatOnlineUserMessage(int dwUserNum, int dwRoomId){
106. if (dwRoomId == 1){
107. int user[] = anychat.GetOnlineUser();
108. if (user.length != 0){
109. for (int i = 0; i < user.length; i++){
110. userlist.add(user[i]+"");
111. }
112. String temp =userlist.get(0);
113. userid = Integer.parseInt(temp);
114. anychat.UserCameraControl(userid, 1);// 請求用戶視頻
115. anychat.UserSpeakControl(userid, 1); // 請求用戶音頻
116. }
117. else {
118. Toast.makeText(VideoChatActivity.this, "當前沒有在線用戶", Toast.LENGTH_SHORT).show();
119. }
120. }
121. }
122. public void OnAnyChatUserAtRoomMessage(int dwUserId, boolean bEnter){
123. if (bEnter) {//新用戶進入房間
124. userlist.add(dwUserId+"");
125. }
126. else { //用戶離開房間
127. if (dwUserId == userid)
128. {
129. Toast.makeText(VideoChatActivity.this, "視頻用戶已下線", Toast.LENGTH_SHORT).show();
130. anychat.UserCameraControl(userid, 0);// 關閉用戶視頻
131. anychat.UserSpeakControl(userid, 0); // 關閉用戶音頻
132. userlist.remove(userid+""); //移除該用戶
133. if (userlist.size() != 0)
134. {
135. String temp =userlist.get(0);
136. userid = Integer.parseInt(temp);
137. anychat.UserCameraControl(userid, 1);// 請求其他用戶視頻
138. anychat.UserSpeakControl(userid, 1); // 請求其他用戶音頻
139. }
140. }
141. else {
142. userlist.remove(dwUserId+""); //移除該用戶
143. }
144. }
145. }
146. public void OnAnyChatLinkCloseMessage(int dwErrorCode){
147. Toast.makeText(VideoChatActivity.this, "連接關閉,error:" + dwErrorCode, Toast.LENGTH_SHORT).show();
148. }
149. @Override
150. protected void onDestroy(){ //程序退出
151. anychat.LeaveRoom(-1); //離開房間
152. anychat.Logout(); //注銷登錄
153. anychat.Release(); //釋放資源
154. mTimer.cancel();
155. super.onDestroy();
156. }
157. // 根據配置文件配置視頻參數
158. private void ApplyVideoConfig(){
159. if (configEntity.configMode == 1) // 自定義視頻參數配置
160. {
161. // 設置本地視頻編碼的碼率(如果碼率為0,則表示使用質量優先模式)
162. anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_BITRATECTRL,configEntity.videoBitrate);
163. if (configEntity.videoBitrate == 0)
164. {
165. // 設置本地視頻編碼的質量
166. anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_QUALITYCTRL,configEntity.videoQuality);
167. }
168. // 設置本地視頻編碼的幀率
169. anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_FPSCTRL,configEntity.videoFps);
170. // 設置本地視頻編碼的關鍵幀間隔
171. anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_GOPCTRL,configEntity.videoFps * 4);
172. // 設置本地視頻采集分辨率
173. anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_WIDTHCTRL,configEntity.resolution_width);
174. anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_HEIGHTCTRL,configEntity.resolution_height);
175. // 設置視頻編碼預設參數(值越大,編碼質量越高,占用CPU資源也會越高)
176. anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_PRESETCTRL,configEntity.videoPreset);
177. }
178. // 讓視頻參數生效
179. anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_APPLYPARAM,configEntity.configMode);
180. // P2P設置
181. anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_NETWORK_P2PPOLITIC,configEntity.enableP2P);
182. // 本地視頻Overlay模式設置
183. anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_OVERLAY,configEntity.videoOverlay);
184. // 回音消除設置
185. anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_AUDIO_ECHOCTRL,configEntity.enableAEC);
186. // 平台硬件編碼設置
187. anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_CORESDK_USEHWCODEC,configEntity.useHWCodec);
188. // 視頻旋轉模式設置
189. anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_ROTATECTRL,configEntity.videorotatemode);
190. // 視頻平滑播放模式設置
191. anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_STREAM_SMOOTHPLAYMODE,configEntity.smoothPlayMode);
192. // 視頻采集驅動設置
193. anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_CAPDRIVER,configEntity.videoCapDriver);
194. // 本地視頻采集偏色修正設置
195. anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_FIXCOLORDEVIA,configEntity.fixcolordeviation);
196. // 視頻顯示驅動設置
197. anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_VIDEOSHOW_DRIVERCTRL,configEntity.videoShowDriver);
198. }
199.}
復制代碼


該Android音視頻解決方案還有文字聊天、發送文件、P2P連接等非常實用的功能。對於開發者來說,能快速實現類似QQ聊天和音視頻通信是令人振奮的事情。
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved