Java source code for mobile 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, 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 combined with my own insight wrote an android audio communication software, Within 200 lines The code is done (hard to believe). In fact, I just call its relevant API to implement. Here is my own Java code, in case everyone learns from each other:

/**
* Android Video Chat
* 1, initialize the SDK 2, connect the server, 3, user login, 4, enter the room, 5, open the local video; 6, request the other side of the video
*/
public class Videochatactivity extends Activity implements Anychatbaseevent
{
Private ANYCHATCORESDK Anychat;Core SDK
Private Surfaceview Remotesurfaceview;Video of each other
Private Surfaceview Localsurfaceview;Local video
Private Configentity configentity;
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)
{
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);
}
};
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);
}
};
}
Initializing the 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 the ARMV6 instruction set
Anychat. Setsdkoptionint (Anychatdefine.
Brac_so_coresdk_usearmv6lib, 1);
Anychat. INITSDK (Android.os.Build.VERSION.SDK_INT, 0); Initializing the SDK
}
Anychat. Connect ("demo.anychat.cn", 8906);//Connect to Server
}
Show live 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;
}
}
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;
}
}
}
public void Onanychatconnectmessage (Boolean bsuccess) {
if (!bsuccess) {
Toast.maketext (videochatactivity.this, "Connection server failed, auto reconnect, please later ...", Toast.length_short). Show ();
}
Anychat. Login ("Android", "");Server Connection Successful user logon
}
public void onanychatloginmessage (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 ();
}
}
public void onanychatenterroommessage (int dwroomid, int dwerrorcode) {
if (Dwerrorcode = = 0) {Enter the room to successfully open local audio and video
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
} else {
Toast.maketext (This, "failed to enter the room, error code:" + Dwerrorcode, Toast.length_short). Show ();
}
}
public void onanychatonlineusermessage (int dwusernum, int dwroomid) {
if (Dwroomid = = 1) {
int user[] = anychat. Getonlineuser ();
if (user.length! = 0) {
for (int i = 0; i < user.length; i++) {
Userlist.add (user[i]+ "");
}
String temp =userlist.get (0);
UserID = Integer.parseint (temp);
Anychat. Usercameracontrol (userid, 1);//Request User Video
Anychat. Userspeakcontrol (userid, 1); Requesting user Audio
}
else {
Toast.maketext (Videochatactivity.this, "Currently there are no online users", Toast.length_short). Show ();
}
}
}
public void onanychatuseratroommessage (int dwuserid, Boolean benter) {
if (benter) {//new user enters room
Userlist.add (dwuserid+ "");
}
else { The user leaves the room
if (Dwuserid = = userid)
{
Toast.maketext (videochatactivity.this, "Video user is offline", Toast.length_short). Show ();
Anychat. Usercameracontrol (userid, 0);//close User video
Anychat. Userspeakcontrol (userid, 0); Turn off user audio
Userlist.remove (userid+ "");Remove the user
if (userlist.size ()! = 0)
{
String temp =userlist.get (0);
UserID = Integer.parseint (temp);
Anychat. Usercameracontrol (userid, 1);//Request Other user video
Anychat. Userspeakcontrol (userid, 1); Request additional user Audio
}
}
else {
Userlist.remove (dwuserid+ "");&nbsp;&nbsp;&nbsp; Remove the user
}
}
}
public void onanychatlinkclosemessage (int dwerrorcode) {
Toast.maketext (videochatactivity.this, "Connection off, Error:" + Dwerrorcode, Toast.length_short). Show ();
}
@Override
protected void OnDestroy () {Program exit
Anychat. Leaveroom (-1);Leave the room
Anychat. Logout ();Log Off Login
Anychat. Release ();Freeing resources
Mtimer.cancel ();
Super.ondestroy ();
}
Configuring video parameters based on configuration files
private void Applyvideoconfig () {
if (Configentity.configmode = = 1)//custom Video parameter configuration
{
Set the bitrate of the local video encoding (if the bitrate is 0, the quality first mode is used)
Anychat. Setsdkoptionint (anychatdefine.brac_so_localvideo_bitratectrl,configentity.videobitrate);
if (configentity.videobitrate = = 0)
{
Set the quality of local video encoding
Anychat. Setsdkoptionint (anychatdefine.brac_so_localvideo_qualityctrl,configentity.videoquality);
}
Set the frame rate for local video encoding
Anychat. Setsdkoptionint (Anychatdefine.brac_so_localvideo_fpsctrl,configentity.videofps);
Set key frame intervals for local video encoding
Anychat. Setsdkoptionint (Anychatdefine.brac_so_localvideo_gopctrl,configentity.videofps * 4);
Set local video capture resolution
Anychat. Setsdkoptionint (Anychatdefine.brac_so_localvideo_widthctrl,configentity.resolution_width);
Anychat. Setsdkoptionint (Anychatdefine.brac_so_localvideo_heightctrl,configentity.resolution_height);
Set the video encoding preset parameters (the higher the value, the higher the encoding quality, the higher the CPU resources will be consumed)
Anychat. Setsdkoptionint (Anychatdefine.brac_so_localvideo_presetctrl,configentity.videopreset);
}
Let the video parameters take effect
Anychat. Setsdkoptionint (Anychatdefine.brac_so_localvideo_applyparam,configentity.configmode);
Peer settings
Anychat. Setsdkoptionint (ANYCHATDEFINE.BRAC_SO_NETWORK_P2PPOLITIC,CONFIGENTITY.ENABLEP2P);
Local Video overlay mode settings
Anychat. Setsdkoptionint (Anychatdefine.brac_so_localvideo_overlay,configentity.videooverlay);
Echo Cancellation settings
Anychat. Setsdkoptionint (ANYCHATDEFINE.BRAC_SO_AUDIO_ECHOCTRL,CONFIGENTITY.ENABLEAEC);
Platform hardware encoding settings
Anychat. Setsdkoptionint (ANYCHATDEFINE.BRAC_SO_CORESDK_USEHWCODEC,CONFIGENTITY.USEHWCODEC);
Video Rotation mode settings
Anychat. Setsdkoptionint (Anychatdefine.brac_so_localvideo_rotatectrl,configentity.videorotatemode);
Video Smoothing Playback mode settings
Anychat. Setsdkoptionint (Anychatdefine.brac_so_stream_smoothplaymode,configentity.smoothplaymode);
Video Capture driver settings
Anychat. Setsdkoptionint (Anychatdefine.brac_so_localvideo_capdriver,configentity.videocapdriver);
Local video capture color correction settings
Anychat. Setsdkoptionint (anychatdefine.brac_so_localvideo_fixcolordevia,configentity.fixcolordeviation);
Video Display driver settings
Anychat. Setsdkoptionint (Anychatdefine.brac_so_videoshow_driverctrl,configentity.videoshowdriver);
}
}


Read the official development document in detail you will find that the Android audio and video solution also has text chat, send files, peer-to connect and other very useful features. It's exciting for developers to be able to quickly implement similar QQ chats and audio and video communications. If you do not understand can add QQ discussion: 2921533637; You can also go to their official website to download a Damo test: www.anychat.cn

Java source code for mobile video calls

Related Article

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.