當前位置:編程學習大全網 - 編程語言 - Android開發視頻通話怎麽實現?

Android開發視頻通話怎麽實現?

/**

* Android視頻聊天

* 1、初始化SDK 2、連接服務器、 3、用戶登錄;4、進入房間;5、打開本地視頻;6、請求對方視頻

*/

public class VideoChatActivity extends Activity implements AnyChatBaseEvent

{

private AnyChatCoreSDK anychat; // 核心SDK

private SurfaceView remoteSurfaceView; // 對方視頻

private SurfaceView localSurfaceView; // 本地視頻

private ConfigEntity configEntity;

private boolean bSelfVideoOpened = false; // 本地視頻是否已打開

private boolean bOtherVideoOpened = false; // 對方視頻是否已打開

private TimerTask mTimerTask; // 定時器

private Timer mTimer = new Timer(true);

private Handler handler; // 用Handler來不間斷刷新即時視頻

private List<String> userlist = new ArrayList<String>();//保存在線用戶列表

private int userid; // 用戶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);//加載視頻通話設置

loginSystem();// 初始化SDK 連接服務器

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();// 不間斷顯示即時視頻通話畫面

super.handleMessage(msg);

}

};

}

// 初始化SDK 連接服務器

private void loginSystem(){

if (anychat == null){

anychat = new AnyChatCoreSDK();

anychat.SetBaseEvent(this); // 設置基本事件回調函數

if (configEntity.useARMv6Lib != 0) // 使用ARMv6指令集

anychat.SetSDKOptionInt(AnyChatDefine.

BRAC_SO_CORESDK_USEARMV6LIB, 1);

anychat.InitSDK(android.os.Build.VERSION.SDK_INT, 0); // 初始化SDK

}

anychat.Connect("demo.anychat.cn", 8906);// 連接服務器

}

// 顯示即時視頻通話畫面

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(); // 獲得視頻畫面

anychat.SetVideoPos(userid, s, 0, 0, 0, 0); // 調用API顯示視頻畫面

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, "連接服務器失敗,自動重連,請稍後...", Toast.LENGTH_SHORT).show();

}

anychat.Login("android", ""); // 服務器連接成功 用戶登錄

}

public void OnAnyChatLoginMessage(int dwUserId, int dwErrorCode){

if (dwErrorCode == 0) {

Toast.makeText(this, "登錄成功!", Toast.LENGTH_SHORT).show();

anychat.EnterRoom(1, ""); // 用戶登錄成功 進入房間

ApplyVideoConfig();

} else {

Toast.makeText(this, "登錄失敗,錯誤代碼:" + dwErrorCode, Toast.LENGTH_SHORT).show();

}

}

public void OnAnyChatEnterRoomMessage(int dwRoomId, int dwErrorCode){

if (dwErrorCode == 0) { // 進入房間成功 打開本地音視頻

Toast.makeText(this, "進入房間成功", Toast.LENGTH_SHORT).show();

anychat.UserCameraControl(-1, 1); // 打開本地視頻

anychat.UserSpeakControl(-1, 1); // 打開本地音頻

} else {

Toast.makeText(this, "進入房間失敗,錯誤代碼:" + 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);// 請求用戶視頻

anychat.UserSpeakControl(userid, 1); // 請求用戶音頻

}

else {

Toast.makeText(VideoChatActivity.this, "當前沒有在線用戶", Toast.LENGTH_SHORT).show();

}

}

}

public void OnAnyChatUserAtRoomMessage(int dwUserId, boolean bEnter){

if (bEnter) {//新用戶進入房間

userlist.add(dwUserId+"");

}

else { //用戶離開房間

if (dwUserId == userid)

{

Toast.makeText(VideoChatActivity.this, "視頻用戶已下線", Toast.LENGTH_SHORT).show();

anychat.UserCameraControl(userid, 0);// 關閉用戶視頻

anychat.UserSpeakControl(userid, 0); // 關閉用戶音頻

userlist.remove(userid+""); //移除該用戶

if (userlist.size() != 0)

{

String temp =userlist.get(0);

userid = Integer.parseInt(temp);

anychat.UserCameraControl(userid, 1);// 請求其他用戶視頻

anychat.UserSpeakControl(userid, 1); // 請求其他用戶音頻

}

}

141. else {

userlist.remove(dwUserId+""); //移除該用戶

}

}

}

public void OnAnyChatLinkCloseMessage(int dwErrorCode){

Toast.makeText(VideoChatActivity.this, "連接關閉,error:" + dwErrorCode, Toast.LENGTH_SHORT).show();

}

@Override

protected void onDestroy(){ //程序退出

anychat.LeaveRoom(-1); //離開房間

anychat.Logout(); //註銷登錄

anychat.Release(); //釋放資源

mTimer.cancel();

super.onDestroy();

}

// 根據配置文件配置視頻參數

private void ApplyVideoConfig(){

if (configEntity.configMode == 1) // 自定義視頻參數配置

{

// 設置本地視頻編碼的碼率(如果碼率為0,則表示使用質量優先模式)

anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_BITRATECTRL,configEntity.videoBitrate);

if (configEntity.videoBitrate == 0)

{

// 設置本地視頻編碼的質量

anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_QUALITYCTRL,configEntity.videoQuality);

}

// 設置本地視頻編碼的幀率

anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_FPSCTRL,configEntity.videoFps);

// 設置本地視頻編碼的關鍵幀間隔

anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_GOPCTRL,configEntity.videoFps * 4);

// 設置本地視頻采集分辨率

anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_WIDTHCTRL,configEntity.resolution_width);

anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_HEIGHTCTRL,configEntity.resolution_height);

// 設置視頻編碼預設參數(值越大,編碼質量越高,占用CPU資源也會越高)

anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_PRESETCTRL,configEntity.videoPreset);

}

// 讓視頻參數生效

anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_APPLYPARAM,configEntity.configMode);

// P2P設置

anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_NETWORK_P2PPOLITIC,configEntity.enableP2P);

// 本地視頻Overlay模式設置

anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_OVERLAY,configEntity.videoOverlay);

// 回音消除設置

anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_AUDIO_ECHOCTRL,configEntity.enableAEC);

// 平臺硬件編碼設置

anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_CORESDK_USEHWCODEC,configEntity.useHWCodec);

// 視頻旋轉模式設置

anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_ROTATECTRL,configEntity.videorotatemode);

// 視頻平滑播放模式設置

anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_STREAM_SMOOTHPLAYMODE,configEntity.smoothPlayMode);

// 視頻采集驅動設置

anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_CAPDRIVER,configEntity.videoCapDriver);

// 本地視頻采集偏色修正設置

anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_FIXCOLORDEVIA,configEntity.fixcolordeviation);

// 視頻顯示驅動設置

anychat.SetSDKOptionInt(AnyChatDefine.BRAC_SO_VIDEOSHOW_DRIVERCTRL,configEntity.videoShowDriver);

}

}

  • 上一篇:幫忙看看C++程序該如何設計
  • 下一篇:手機上用微信怎樣添加公眾號來進行西寧教育問卷調查
  • copyright 2024編程學習大全網