Java code for Android video calls

Source: Internet
Author: User



Recently learning the Android platform for cross-platform audio and video communications development, although there are many open-source projects on the Internet for our reference study, but the audio and video effect is very general, there are a lot of unstable factors, after all, is open source. In China I found a better audio and video communication solution (Baidu download official anychat for Android demo), the case provides a pure Java language interface for us to call, then I refer to the official Android Demo program and development documentation and in combination with their own ideas to write an Android audio communication software, 200 lines of code is done (hard to believe it). In fact, I just call its relevant API to implement. Here is my own Java code, in case everyone learns from each other:
1./**
2. * android** Days
3. * 1, Initialize SDK 2, connect server, 3, user login, 4, enter room, 5, open local video; 6, request video
4. */
5.public class Videochatactivity extends Activity implements Anychatbaseevent
4.9
7. Private ANYCHATCORESDK Anychat; Core SDK
8. Private Surfaceview Remotesurfaceview; Video of each other
9. Private Surfaceview Localsurfaceview; Local video
-Private configentity configentity;
One. Private Boolean bselfvideoopened = false; Whether the local video is turned on
Private Boolean bothervideoopened = false; Whether the other video is turned on
Private TimerTask Mtimertask; Timer
Private Timer Mtimer = new timer (true);
-Private Handler Handler; Use handler to refresh live video without interruption
Private list<string> userlist = new arraylist<string> ();//Save Online user list
. private int UserID; User ID
@Override.
. public void OnCreate (Bundle savedinstancestate)
20. {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_video_chat);
Remotesurfaceview = (Surfaceview) Findviewbyid (r.id.surface_remote);
Localsurfaceview = (Surfaceview) Findviewbyid (r.id.surface_local);
Configentity = Configservice.loadconfig (this);//load Video Call settings
Loginsystem ();//Initialize SDK connection server
Mtimertask = new TimerTask () {
. public void Run () {
Message Mesasge = new Message ();
Handler.sendmessage (MESASGE);
31.}
32.};
Mtimer.schedule (Mtimertask, 1000, 100);
Handler = new Handler () {
@Override
. public void Handlemessage (Message msg) {
VideoChat ();//uninterrupted display of instant Video Call screen
Super.handlemessage (msg);
39.}
40.};
41.}
42.//Initialize SDK connection server
. private void Loginsystem () {
. if (anychat = = null) {
Anychat = new Anychatcoresdk ();
Anychat. Setbaseevent (this); Set Basic Event callback function
if (configentity.usearmv6lib! = 0)//using ARMV6 instruction Set
Anychat. Setsdkoptionint (Anychatdefine.
Brac_so_coresdk_usearmv6lib, 1);
Anychat. INITSDK (Android.os.Build.VERSION.SDK_INT, 0); Initializing the SDK
51.}
Anychat. Connect ("demo.anychat.cn", 8906);//Connect to Server
53.}
54.//Display Instant Video Call screen
. public void VideoChat () {
. if (!bothervideoopened) {
. if (anychat. Getcamerastate (userid) = = 2
&& Anychat. Getuservideowidth (userid)! = 0) {
Surfaceholder holder = Remotesurfaceview.getholder ();
Holder.setformat (pixelformat.rgb_565);
Holder.setfixedsize (Anychat. Getuservideowidth (userid),
Anychat. Getuservideoheight (userid));
Surface s = holder.getsurface (); Get video footage
Anychat. Setvideopos (userid, S, 0, 0, 0, 0); Call API to display video screen
Bothervideoopened = true;
66.}
67.}
if (!bselfvideoopened) {
. if (anychat. Getcamerastate (-1) = = 2
&& Anychat. Getuservideowidth (-1)! = 0) {
Surfaceholder holder = Localsurfaceview.getholder ();
Holder.setformat (pixelformat.rgb_565);
Holder.setfixedsize (Anychat. Getuservideowidth (-1),
Anychat. Getuservideoheight (-1));
Surface s = holder.getsurface ();
Anychat. Setvideopos ( -1, s, 0, 0, 0, 0);
Bselfvideoopened = true;
78.}
79.}
80.}
Bayi. public void Onanychatconnectmessage (Boolean bsuccess) {
(!bsuccess) {
Toast.maketext (videochatactivity.this, "Connection server failed, auto reconnect, please later ...", Toast.length_short). Show ();
84.}
Anychat. Login ("Android", ""); Server Connection Successful user logon
86.}
Onanychatloginmessage public void (int dwuserid, int dwerrorcode) {
if (Dwerrorcode = = 0) {
Toast.maketext (This, "Login successful! ", Toast.length_short). Show ();
Anychat. Enterroom (1, ""); User login successfully entered the room
Applyvideoconfig ();
.} else {
Toast.maketext (This, "Login failed, error code:" + Dwerrorcode, Toast.length_short). Show ();
94.}
95.}
. public void Onanychatenterroommessage (int dwroomid, int dwerrorcode) {
if (Dwerrorcode = = 0) {//Enter the room to successfully open local audio and video
98. Toast.maketext (This, "Enter room Success", Toast.length_short). Show ();
Anychat. Usercameracontrol (-1, 1); Open Local Video
Anychat. Userspeakcontrol (-1, 1); Turn on local audio
101.} else {
102. Toast.maketext (This, "failed to enter the room, error code:" + Dwerrorcode, Toast.length_short). Show ();
103.}
104.}
Onanychatonlineusermessage public void (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++) {
Userlist.add (user[i]+ "");
111.}
. String temp =userlist.get (0);
113. UserID = Integer.parseint (temp);
Anychat. Usercameracontrol (userid, 1);//Request User Video
Anychat. Userspeakcontrol (userid, 1); Requesting user Audio
116.}
117. else {
118. Toast.maketext (Videochatactivity.this, "Currently there is no online user", Toast.length_short). Show ();
119.}
120.}
121.}
122. public void Onanychatuseratroommessage (int dwuserid, Boolean benter) {
123. if (benter) {//new user enters room
124. Userlist.add (dwuserid+ "");
125.}
126. else {//user leaves room
127. if (Dwuserid = = userid)
128. {
129. Toast.maketext (Videochatactivity.this, "Video user is offline", Toast.length_short). Show ();
Anychat. Usercameracontrol (userid, 0);//close User video
131. Anychat. Userspeakcontrol (userid, 0); Turn off user audio
Userlist.remove (userid+ ""); Remove the user
133. if (Userlist.size ()! = 0)
134. {
135. String Temp =userlist.get (0);
136. UserID = Integer.parseint (temp);
137. Anychat. Usercameracontrol (userid, 1);//Request Other user video
138. Anychat. Userspeakcontrol (userid, 1); Request additional user Audio
139.}
140.}
141. else {
142. Userlist.remove (dwuserid+ ""); Remove the user
143.}
144.}
145.}
146. public void onanychatlinkclosemessage (int dwerrorcode) {
147. Toast.maketext (Videochatactivity.this, "Connection closed, Error:" + Dwerrorcode, Toast.length_short). Show ();
148.}
149. @Override
protected void OnDestroy () {//Program exit
151. Anychat. Leaveroom (-1); Leave the room
Anychat. Logout (); Log Off Login
153. Anychat. Release (); Freeing resources
154. Mtimer.cancel ();
155. Super.ondestroy ();
156.}
157.//Configure video parameters according to the configuration file
158. private void Applyvideoconfig () {
159. if (Configentity.configmode = = 1)//custom Video parameter configuration
160. {
161.//Set the bitrate of the local video encoding (if the bitrate is 0, the quality-first mode is used)
162. Anychat. Setsdkoptionint (anychatdefine.brac_so_localvideo_bitratectrl,configentity.videobitrate);
163. if (configentity.videobitrate = = 0)
164. {
165.//Set the quality of local video encoding
166. Anychat. Setsdkoptionint (anychatdefine.brac_so_localvideo_qualityctrl,configentity.videoquality);
167.}
168.//Set the local video encoding frame rate
169. Anychat. Setsdkoptionint (Anychatdefine.brac_so_localvideo_fpsctrl,configentity.videofps);
170.//Set key frame interval for local video encoding
171. Anychat. Setsdkoptionint (Anychatdefine.brac_so_localvideo_gopctrl,configentity.videofps * 4);
172.//Set local video capture resolution
173. Anychat. Setsdkoptionint (Anychatdefine.brac_so_localvideo_widthctrl,configentity.resolution_width);
174. Anychat. Setsdkoptionint (Anychatdefine.brac_so_localvideo_heightctrl,configentity.resolution_height);
175.//Set video encoding preset parameters (the higher the value, the higher the encoding quality, the higher the CPU resources will be consumed)
176. Anychat. Setsdkoptionint (Anychatdefine.brac_so_localvideo_presetctrl,configentity.videopreset);
177.}
178.//Let the video parameters take effect
179. Anychat. Setsdkoptionint (Anychatdefine.brac_so_localvideo_applyparam,configentity.configmode);
+//Peer set-up
181. Anychat. Setsdkoptionint (ANYCHATDEFINE.BRAC_SO_NETWORK_P2PPOLITIC,CONFIGENTITY.ENABLEP2P);
182.//Local video overlay mode setting
183. Anychat. Setsdkoptionint (Anychatdefine.brac_so_localvideo_overlay,configentity.videooverlay);
184.//Echo Cancellation settings
185. Anychat. Setsdkoptionint (ANYCHATDEFINE.BRAC_SO_AUDIO_ECHOCTRL,CONFIGENTITY.ENABLEAEC);
186.//Platform hardware encoding settings
187. Anychat. Setsdkoptionint (ANYCHATDEFINE.BRAC_SO_CORESDK_USEHWCODEC,CONFIGENTITY.USEHWCODEC);
188.//video Rotation mode setting
189. Anychat. Setsdkoptionint (Anychatdefine.brac_so_localvideo_rotatectrl,configentity.videorotatemode);
190.//Video smooth playback mode setting
191. Anychat. Setsdkoptionint (Anychatdefine.brac_so_stream_smoothplaymode,configentity.smoothplaymode);
192.//Video Capture driver settings
193. Anychat. Setsdkoptionint (Anychatdefine.brac_so_localvideo_capdriver,configentity.videocapdriver);
194.//local video capture offset color correction settings
195. Anychat. Setsdkoptionint (anychatdefine.brac_so_localvideo_fixcolordevia,configentity.fixcolordeviation);
196.//Video Display driver settings
197. Anychat. Setsdkoptionint (Anychatdefine.brac_so_videoshow_driverctrl,configentity.videoshowdriver);
198.}
199.}
Copy Code


The Android audio and video solution also has a very useful function such as text chat, send files, peer-to connect, etc. It's exciting for developers to be able to quickly implement similar QQ chats and audio and video communications.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.