JAVA source code for mobile video calls and java source code for calls

Source: Internet
Author: User

JAVA source code for mobile video calls and java source code for calls

I recently learned how to develop cross-platform audio and video communication on the Android platform. Although there are many open-source projects on the Internet for our reference, the audio and video effects are common and there are many unstable factors. After all, they are open-source. In China, I found a better solution for audio/video communication. In this case, we provided a pure Java interface for calling, then I wrote an android audio communication software by referring to the official android demo program and development documentation, and combining my own insights. I got the code within 200 lines (I can't believe it ). In fact, I only need to call its related APIs. The following are the Java code I have written to help you learn from each other:

/**
* Android video chat
* 1. initialize SDK 2, connect to the server, 3. log on to the user, 4. enter the room, 5. Open the local video, and 6. Request the recipient's video.
*/
Public class VideoChatActivity extends Activity implements AnyChatBaseEvent
{
Private AnyChatCoreSDK anychat; // core SDK
Private SurfaceView remoteSurfaceView; // specifies the target video.
Private SurfaceView localSurfaceView; // local video
Private ConfigEntity configEntity;
Private boolean bSelfVideoOpened = false; // whether the local video is enabled
Private boolean bOtherVideoOpened = false; // whether the target video is enabled
Private TimerTask mTimerTask; // Timer
Private Timer mTimer = new Timer (true );
Private Handler handler; // uses Handler to continuously refresh real-time videos
Private List <String> userlist = new ArrayList <String> (); // Save the 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 the video call settings
LoginSystem (); // initialize the SDK to connect to the server
MTimerTask = new TimerTask (){
Public void run (){
Message mesasge = new Message ();
Handler. sendMessage (mesasge );
}
};
MTimer. schedule (mtimerjob, 1000,100 );
Handler = new Handler (){
@ Override
Public void handleMessage (Message msg ){
VideoChat (); // displays the instant video call screen without interruption
Super. handleMessage (msg );
}
};
}
// Initialize the SDK to connect to the server
Private void loginSystem (){
If (anychat = null ){
Anychat = new AnyChatCoreSDK ();
Anychat. SetBaseEvent (this); // sets the basic Event Callback Function.
If (configEntity. useARMv6Lib! = 0) // use the ARMv6 Instruction Set
Anychat. SetSDKOptionInt (AnyChatDefine.
BRAC_SO_CORESDK_USEARMV6LIB, 1 );
Anychat. InitSDK (android. OS. Build. VERSION. SDK_INT, 0); // initialize the SDK
}
Anychat. Connect ("demo.anychat.cn", 8906); // Connect to the server
}
// Display the instant video call Screen
Public void VideoChat (){
If (! BOtherVideoOpened ){
If (anychat. GetCameraState (userid) = 2
& Amp; 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 (); // obtain the video image
Anychat. SetVideoPos (userid, s, 0, 0, 0, 0); // call the API to display the video screen
BOtherVideoOpened = true;
}
}
If (! BSelfVideoOpened ){
If (anychat. GetCameraState (-1) = 2
& Amp; 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 );
BSelfVideoOpened = true;
}
}
}
Public void OnAnyChatConnectMessage (boolean bSuccess ){
If (! BSuccess ){
Toast. makeText (VideoChatActivity. this, "failed to connect to the server, automatic reconnection. Please wait...", Toast. LENGTH_SHORT). show ();
}
Anychat. Login ("android", ""); // log on to the server after the connection is successful.
}
Public void OnAnyChatLoginMessage (int dwUserId, int dwErrorCode ){
If (dwErrorCode = 0 ){
Toast. makeText (this, "Logon successful! ", Toast. LENGTH_SHORT). show ();
Anychat. EnterRoom (1, ""); // the user logs on to the room.
ApplyVideoConfig ();
} Else {
Toast. makeText (this, "Logon Failed, error code:" + dwErrorCode, Toast. LENGTH_SHORT). show ();
}
}
Public void OnAnyChatEnterRoomMessage (int dwRoomId, int dwErrorCode ){
If (dwErrorCode = 0) {// The local audio and video are successfully enabled when you enter the room.
Toast. makeText (this, "successfully entering the room", Toast. LENGTH_SHORT). show ();
Anychat. UserCameraControl (-1, 1); // open the local video
Anychat. UserSpeakControl (-1, 1); // enable the 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 a user's video
Anychat. UserSpeakControl (userid, 1); // request the user's audio
}
Else {
Toast. makeText (VideoChatActivity. this, "No online users currently", Toast. LENGTH_SHORT). show ();
}
}
}
Public void OnAnyChatUserAtRoomMessage (int dwUserId, boolean bEnter ){
If (bEnter) {// new user enters the room
Userlist. add (dwUserId + "");
}
Else {// The user leaves the room
If (dwUserId = userid)
{
Toast. makeText (VideoChatActivity. this, "video user offline", Toast. LENGTH_SHORT). show ();
Anychat. UserCameraControl (userid, 0); // disable the user video
Anychat. UserSpeakControl (userid, 0); // Disable User audio
Userlist. remove (userid + ""); // remove this user
If (userlist. size ()! = 0)
{
String temp = userlist. get (0 );
Userid = Integer. parseInt (temp );
Anychat. UserCameraControl (userid, 1); // request videos of other users
Anychat. UserSpeakControl (userid, 1); // request audio from other users
}
}
Else {
Userlist. remove (dwUserId + ""); & nbsp; // remove this user
}
}
}
Public void OnAnyChatLinkCloseMessage (int dwErrorCode ){
Toast. makeText (VideoChatActivity. this, "Connection closed, error:" + dwErrorCode, Toast. LENGTH_SHORT). show ();
}
@ Override
Protected void onDestroy () {// exit the program
Anychat. LeaveRoom (-1); // leave the room
Anychat. Logout (); // log out
Anychat. Release (); // Release resources
MTimer. cancel ();
Super. onDestroy ();
}
// Configure video parameters according to the configuration file
Private void ApplyVideoConfig (){
If (configEntity. configMode = 1) // custom video parameter configuration
{
// Set the bit rate of the local video encoding (if the bit rate 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 of the Local Video Encoding
Anychat. SetSDKOptionInt (AnyChatDefine. BRAC_SO_LOCALVIDEO_FPSCTRL, configEntity. videoFps );
// Set the key frame interval for Local Video Encoding
Anychat. SetSDKOptionInt (AnyChatDefine. BRAC_SO_LOCALVIDEO_GOPCTRL, configEntity. videoFps * 4 );
// Sets the resolution of the local video collection.
Anychat. SetSDKOptionInt (AnyChatDefine. BRAC_SO_LOCALVIDEO_WIDTHCTRL, configEntity. resolution_width );
Anychat. SetSDKOptionInt (AnyChatDefine. BRAC_SO_LOCALVIDEO_HEIGHTCTRL, configEntity. resolution_height );
// Set preset video encoding parameters (the greater the value, the higher the encoding quality, and the higher the CPU usage)
Anychat. SetSDKOptionInt (AnyChatDefine. BRAC_SO_LOCALVIDEO_PRESETCTRL, configEntity. videoPreset );
}
// Make video parameters take effect
Anychat. SetSDKOptionInt (AnyChatDefine. BRAC_SO_LOCALVIDEO_APPLYPARAM, configEntity. configMode );
// P2P settings
Anychat. SetSDKOptionInt (AnyChatDefine. BRAC_SO_NETWORK_P2PPOLITIC, configEntity. enableP2P );
// Specifies the Overlay mode of the local video.
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 );
// Set the smooth video playback Mode
Anychat. SetSDKOptionInt (AnyChatDefine. BRAC_SO_STREAM_SMOOTHPLAYMODE, configEntity. smoothPlayMode );
// Video capture driver settings
Anychat. SetSDKOptionInt (AnyChatDefine. BRAC_SO_LOCALVIDEO_CAPDRIVER, configEntity. videoCapDriver );
// Set partial color correction for local video capture
Anychat. SetSDKOptionInt (AnyChatDefine. BRAC_SO_LOCALVIDEO_FIXCOLORDEVIA, configEntity. fixcolordeviation );
// Video display driver settings
Anychat. SetSDKOptionInt (AnyChatDefine. BRAC_SO_VIDEOSHOW_DRIVERCTRL, configEntity. videoShowDriver );
}
}


After reading the official development documentation in detail, you will find that the Android audio/video solution provides useful functions such as text chat, File Sending, and P2P connection. It is exciting for developers to quickly implement QQ chat and audio/video communication. If you do not know, you can add QQ to discuss: 2921533637; you can also go to their official website to download a DAMO test: www.anychat.cn


Java small Address Book source code

Package src;

Public class TelBook {
// Name
String name;

// Home phone number
Integer homePhone;

// Personal mobile phone number
Integer personalMobilePhone;

// Office phone number
Integer officePhone;

// Home address
String homeAddress;

// Office address
String officeAddress;

// QQ number
Integer qqNumber;

// MSN number
String msn;

// Email
String email;

// Remarks
String notes;

String getEmail (){
Return email;
}

Void setEmail (String email ){
This. email = email;
}

String getHomeAddress (){
Return homeAddress;
}

Void setHomeAddress (String homeAddress ){
This. homeAddress = homeAddress;
}

Integer getHomePhone (){
Return homePhone;
}

Void setHomePhone (Integer homePhone ){
This. homePhone = homePhone;
}

String getMsn (){
Return msn;
}

Void setMsn (String msn ){
This. msn = msn;
}

String getName (){
Return name;
}

Void setName (String name ){
This. name = name;
}

String getNotes (){
Return notes;
}

Void setNotes (String notes ){
This. notes = notes;
}

String getOfficeAddress (){
Return officeAddress;
}

Void setOfficeAddress (String officeAddress ){
This. officeAddress = officeAddress;
}

Integer getOfficePhone (){
Return officePhone;
}

Void setOfficePhone (Integer officePhone ){
This. officePhone = officePhone;
}

Integer getPersonalMobilePhone (){
Return personalMobilePhone;
}

Void setPersonalMobilePhone (Integer personalMobilePhone ){
This. personalMobilePhone = personalMobilePhone;
... The remaining full text>

Source code of a java Video Player

You only need to reference your own back-to-back MediaPlayer. Code:
<Div id = "FlashFile">
<Object id = "player" height = "170" width = "220"
Classid = "CLSID: 6BF52A52-394A-11d3-B153-00C04F79FAA6">
<Param NAME = "AutoStart" VALUE = "1">
<! -- Automatic playback? -->
<Param NAME = "Balance" VALUE = "0">
<! -- Adjust the left-right channel balance and the old player code above -->
<Param name = "enabled" value = "-1">
<! -- Whether the player can be controlled by humans -->
<Param NAME = "EnableContextMenu" VALUE = "-1">
<! -- Enable context menu -->
<Param NAME = "url" value = "soft/<% = file %>"> // source file path
<! -- Playback file address -->
<Param NAME = "PlayCount" VALUE = "1">
<! -- Control the number of playbacks, which is an integer -->
<Param name = "rate" value = "1">
<! -- The playback speed is controlled. 1 is normal, and decimal places are allowed. -->
<Param name = "currentPosition" value = "0">
<! -- Control settings: Current Position -->
<Param name = "currentMarker" value = "0">
<! -- Control settings: Current tag -->
<Param name = "defaultFrame" value = "">
<! -- Display default frame -->
<Param name = "invokeURLs" value = "0">
<! -- Script command setting: whether to call URL -->
<Param name = "baseURL" value = "">
<! -- Script command setting: called URL -->
<Param name = "stretchToFit" value = "0">
<! -- Scale up or not -->
<Param name = "volume" value = "50">
<! -- The default sound size is 0%-100%, and 50 is 50% -- & gt ...... the remaining full text>

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.