|
这个程序二次进入的时候没有对方声音,加入android.os.Process.killProcess(android.os.Process.myPid());这个以后,按返回键的时候,整个程序都会退出,是怎么回事啊?
public class OnlineMonitoringActivity extends Activity implements AnyChatBaseEvent, AnyChatTransDataEvent, OnClickListener {
private AnyChatCoreSDK anychat;
private SurfaceView otherView; //别人的视频
private SurfaceView myView; //自己的视频
private int userID = 500;
private boolean bVideoAreaLoaded = false; // 视频区域大小是否加载
private ProgressBar OtherProgressBar; //别人的音量条
private ProgressBar MyProgressBar; //自己的音量条
private int dwLocalVideoWidth = 0;
private int dwLocalVideoHeight = 0;
private int dwRemoteVideoHeight = 0;
private int dwRemoteVideoWidth = 0;
private ImageView mCameraSwitchImage; // 前后摄像头切换按钮
private Timer mTimer = new Timer(true);
private TimerTask mTimerTask;
private Handler handler;
boolean bOnPaused = false; //暂停按钮
private boolean bSelfVideoOpened = false; // 本地视频是否已打开
private boolean bOtherVideoOpened = false; // 对方视频是否已打开
private boolean isbegin = false; //是否已经开始录像
private boolean loadSelf = false;
private ConfigEntity configEntity ;
private ConfigService configService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.online_monitoring);
configEntity = ConfigService.LoadConfig(this);
InitialSDK();
InitialLayout();
mTimerTask = new TimerTask() {
public void run() {
if(handler == null)
return;
Message mesasge = new Message();
handler.sendMessage(mesasge);
}
};
mTimer.schedule(mTimerTask, 1000, 100);
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
CheckVideoStatus();
SetVolum();
super.handleMessage(msg);
}
};
}
public void refreshOnther(){
if (!bOtherVideoOpened) {
Log.i("打开别人的", anychat.GetCameraState(userID) +" "+anychat.GetUserVideoWidth(userID));
if (anychat.GetCameraState(userID) == 2 && anychat.GetUserVideoWidth(userID) != 0) {
SurfaceHolder holder = otherView.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);
bOtherVideoOpened = true;
}
}else {
}
}
public void refreshMe(){
if (!bSelfVideoOpened) {
Log.i("打开自己的", anychat.GetCameraState(-1) +" "+anychat.GetUserVideoWidth(-1));
if (anychat.GetCameraState(-1) == 2 && anychat.GetUserVideoWidth(-1) != 0) {
SurfaceHolder holder = myView.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;
}else {
anychat.UserCameraControl(-1, 1);
anychat.UserSpeakControl(-1, 1);
}
}
}
private void CheckVideoStatus() {
if(bOnPaused)
return;
if (isbegin) {
refreshOnther();
}
if (!bSelfVideoOpened) {
Log.i("打开自己的", anychat.GetCameraState(-1) +" "+anychat.GetUserVideoWidth(-1));
if (anychat.GetCameraState(-1) == 2 && anychat.GetUserVideoWidth(-1) != 0) {
SurfaceHolder holder = myView.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;
}else {
}
}
}
//初始化SDK连接服务器
public void InitialSDK(){
if (anychat == null) {
anychat = new AnyChatCoreSDK();
}
if (configEntity.useARMv6Lib != 0) // 是否强制使用ARMv6指令集,默认是内核自动判断
AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_CORESDK_USEARMV6LIB, 1);
anychat.InitSDK(android.os.Build.VERSION.SDK_INT, 0 );
anychat.SetBaseEvent(this);
anychat.SetTransDataEvent(this);
// 启动AnyChat传感器监听
anychat.mSensorHelper.InitSensor(this);
ApplyVideoConfig();
//anychat.Connect("demo.anychat.cn", 8906);// 连接服务器
connectServer();
}
public void connectServer(){
try {
String iptext = setIpText();
anychat.Connect(iptext, 8906);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// anychat.Connect("218.194.144.244", 8906);// 连接服务器
}
//初始化界面
private void InitialLayout() {
// TODO Auto-generated method stub
this.setContentView(R.layout.online_monitoring);
this.setTitle("与" + anychat.GetUserName(userID) + "视频通话");
myView = (SurfaceView) findViewById(R.id.surface_local);
otherView = (SurfaceView) findViewById(R.id.surface_remote);
Button button = (Button) findViewById(R.id.beginBtn);
Button endButton = (Button) findViewById(R.id.endBtn);
endButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(OnlineMonitoringActivity.this);
builder.setMessage("您确定要退出在线监控吗?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
SendMessageToFWQ("11");
onDestroy();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadSelf = true;
//refreshMe();
SendMessageToFWQ("10");
}
});
// 如果是采用Java视频采集,则需要设置Surface的CallBack
if(AnyChatCoreSDK.GetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_CAPDRIVER) == AnyChatDefine.VIDEOCAP_DRIVER_JAVA) {
myView.getHolder().addCallback(AnyChatCoreSDK.mCameraHelper);
//myView.setZOrderOnTop(true);
}
// 如果是采用Java视频显示,则需要设置Surface的CallBack
if(AnyChatCoreSDK.GetSDKOptionInt(AnyChatDefine.BRAC_SO_VIDEOSHOW_DRIVERCTRL) == AnyChatDefine.VIDEOSHOW_DRIVER_JAVA) {
int index = anychat.mVideoHelper.bindVideo(otherView.getHolder());
anychat.mVideoHelper.SetVideoUser(index, userID);
}
otherView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// TODO Auto-generated method stub
if(!bVideoAreaLoaded)
{
adjuestVideoSize(otherView.getWidth(), otherView.getHeight());
bVideoAreaLoaded=true;
}
}
});
mCameraSwitchImage = (ImageView) findViewById(R.id.image_switch_camera);
mCameraSwitchImage.setOnClickListener(this);
MyProgressBar = (ProgressBar) findViewById(R.id.progress_local);
OtherProgressBar = (ProgressBar) findViewById(R.id.progress_remote);
myView.setOnClickListener(this);
ConfigEntity configEntity = ConfigService.LoadConfig(this);
if (configEntity.videoOverlay != 0) {
myView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
// 判断是否显示本地摄像头切换图标
if(AnyChatCoreSDK.GetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_CAPDRIVER) == AnyChatDefine.VIDEOCAP_DRIVER_JAVA) {
if(AnyChatCoreSDK.mCameraHelper.GetCameraNumber() > 1) {
mCameraSwitchImage.setVisibility(View.VISIBLE);
// 默认打开前置摄像头
AnyChatCoreSDK.mCameraHelper.SelectVideoCapture(AnyChatCoreSDK.mCameraHelper.CAMERA_FACING_FRONT);
}
}else {
String[] strVideoCaptures = anychat.EnumVideoCapture();
if (strVideoCaptures != null && strVideoCaptures.length > 1) {
mCameraSwitchImage.setVisibility(View.VISIBLE);
// 默认打开前置摄像头
for(int i=0;i<strVideoCaptures.length;i++)
{
String strDevices=strVideoCaptures[i];
if(strDevices.indexOf("Front")>=0) {
anychat.SelectVideoCapture(strDevices);
break;
}
}
}
}
};
OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
}
};
private void openOnther(){
SurfaceHolder holder = otherView.getHolder();
holder.setKeepScreenOn(true);
anychat.UserCameraControl(userID, 1);
anychat.UserSpeakControl(userID, 1);
}
//设置音量条
private void SetVolum() {
OtherProgressBar.setProgress(anychat.GetUserSpeakVolume(userID));
MyProgressBar.setProgress(anychat.GetUserSpeakVolume(-1));
}
private void adjuestVideoSize(int width, int height) {
// if (3 * width > 4 * height) {
// dwRemoteVideoHeight = height;
// dwRemoteVideoWidth = (int) (4.0 / 3.0 * dwRemoteVideoHeight);
// } else {
// dwRemoteVideoWidth = width;
// dwRemoteVideoHeight = (int) (3.0 / 4.0 * dwRemoteVideoWidth);
// }
// dwLocalVideoWidth = dwRemoteVideoWidth;
// dwLocalVideoHeight = dwRemoteVideoHeight;
dwLocalVideoWidth = myView.getWidth();
dwLocalVideoHeight = myView.getHeight();
FrameLayout.LayoutParams layoutParamSelf=new FrameLayout.LayoutParams(dwLocalVideoWidth, dwLocalVideoHeight);
myView.setLayoutParams(layoutParamSelf);
LinearLayout.LayoutParams layoutPramOther=new LinearLayout.LayoutParams(width, height);
otherView.setLayoutParams(layoutPramOther);
}
private void refreshAV() {
//anychat.UserCameraControl(userID, 1);
//anychat.UserSpeakControl(userID, 1);
anychat.UserCameraControl(-1, 1);
anychat.UserSpeakControl(-1, 1);
bOtherVideoOpened = false;
bSelfVideoOpened = false;
}
protected void onRestart() {
super.onRestart();
// 如果是采用Java视频采集,则需要设置Surface的CallBack
if(AnyChatCoreSDK.GetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_CAPDRIVER) == AnyChatDefine.VIDEOCAP_DRIVER_JAVA) {
myView.getHolder().addCallback(AnyChatCoreSDK.mCameraHelper);
}
// 如果是采用Java视频显示,则需要设置Surface的CallBack
if(AnyChatCoreSDK.GetSDKOptionInt(AnyChatDefine.BRAC_SO_VIDEOSHOW_DRIVERCTRL) == AnyChatDefine.VIDEOSHOW_DRIVER_JAVA) {
int index = anychat.mVideoHelper.bindVideo(otherView.getHolder());
anychat.mVideoHelper.SetVideoUser(index, userID);
}
refreshAV();
bOnPaused = false;
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
bOnPaused = true;
anychat.UserCameraControl(userID, 0);
anychat.UserSpeakControl(userID, 0);
anychat.UserCameraControl(-1, 0);
anychat.UserSpeakControl(-1, 0);
anychat.PrivateChatExit(userID);
}
protected void onDestroy() {
// super.onDestroy();
// mTimer.cancel();
// anychat.Logout();
mTimer.cancel();
anychat.mSensorHelper.DestroySensor();
anychat.PrivateChatExit(userID);
anychat.LeaveRoom(-1);
SendMessageToFWQ("11");
anychat.Logout();
anychat.Release();
//android.os.Process.killProcess(android.os.Process.myPid());
super.onDestroy();
finish();
}
//发消息给服务器
public void SendMessageToFWQ(String str){
Log.v("22222222", "SendMessage--------"+str);
byte[] msg = new String (str).getBytes();
anychat.TransBuffer(userID, msg, msg.length);
Log.v("55555", "SendMessage--发送成功------"+str);
}
@Override
public void OnAnyChatConnectMessage(boolean bSuccess) {
if(!bSuccess){
Toast.makeText(OnlineMonitoringActivity.this, "连接服务器失败,自动重连,请稍后...", Toast.LENGTH_SHORT).show();
}
anychat.Login("daiwei", ""); // 服务器连接成功 用户登录
}
@Override
public void OnAnyChatLoginMessage(int dwUserId, int dwErrorCode) {
if(dwErrorCode==0){
Toast.makeText(this, "登录成功!", Toast.LENGTH_SHORT).show();
//SendMessageToFWQ("10");
anychat.EnterRoom(1, ""); // 用户登录成功 进入房间,1为房间编号 第二个参数为密码,可以为空
}else {
Toast.makeText(this, "登录失败,错误代码:" + dwErrorCode, Toast.LENGTH_SHORT).show();
}
}
@Override
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); // 打开本地音频
// anychat.UserCameraControl(userID, 1);
} else {
Toast.makeText(this, "进入房间失败,错误代码:" + dwErrorCode, Toast.LENGTH_SHORT).show();
}
}
@Override
public void OnAnyChatOnlineUserMessage(int dwUserNum, int dwRoomId) {
refreshAV();
}
@Override
public void OnAnyChatUserAtRoomMessage(int dwUserId, boolean bEnter) {
if (dwUserId == userID) {
if (!bEnter) {
anychat.UserCameraControl(dwUserId, 0);
anychat.UserSpeakControl(dwUserId, 0);
bOtherVideoOpened = false;
} else {
anychat.UserCameraControl(dwUserId, 1);
anychat.UserSpeakControl(dwUserId, 1);
}
}
}
@Override
public void OnAnyChatLinkCloseMessage(int dwErrorCode) {
if(bOtherVideoOpened)
{
anychat.UserCameraControl(userID, 0);
anychat.UserSpeakControl(userID, 0);
bOtherVideoOpened = false;
}
if(bSelfVideoOpened)
{
anychat.UserCameraControl(-1, 0);
anychat.UserSpeakControl(-1, 0);
bSelfVideoOpened = false;
}
}
public void onClick(View v){
if (v == mCameraSwitchImage) {
// 如果是采用Java视频采集,则在Java层进行摄像头切换
if(AnyChatCoreSDK.GetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_CAPDRIVER) == AnyChatDefine.VIDEOCAP_DRIVER_JAVA) {
AnyChatCoreSDK.mCameraHelper.SwitchCamera();
return;
}
String strVideoCaptures[] = anychat.EnumVideoCapture();
;
String temp = anychat.GetCurVideoCapture();
for (int i = 0; i < strVideoCaptures.length; i++) {
if (!temp.equals(strVideoCaptures[i])) {
anychat.UserCameraControl(-1, 0);
bSelfVideoOpened = false;
anychat.SelectVideoCapture(strVideoCaptures[i]);
anychat.UserCameraControl(-1, 1);
break;
}
}
}
}
@Override
public void OnAnyChatTransFile(int dwUserid, String FileName,
String TempFilePath, int dwFileLength, int wParam, int lParam,
int dwTaskId) {
// TODO Auto-generated method stub
}
@Override
public void OnAnyChatTransBuffer(int dwUserid, byte[] lpBuf, int dwLen) {
//String serverID = Config.getProperty("serverID");
String result = new String(lpBuf);
AlertDialog.Builder builder = new AlertDialog.Builder(OnlineMonitoringActivity.this);
builder.setCancelable(false);//键盘退建无效
if (result.equals("10")) {
//Toast.makeText(this, "返回的东西:" + result, Toast.LENGTH_SHORT).show();
//AlertDialog.Builder builder = new AlertDialog.Builder(OnlineMonitoringActivity.this);
builder.setTitle("请求来了");
builder.setMessage("有人请求你视频,你确定接收吗?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(OnlineMonitoringActivity.this, "返回的东西:" + "222222", Toast.LENGTH_SHORT).show();
isbegin = true;
loadSelf = true;
SendMessageToFWQ("12");
openOnther();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
SendMessageToFWQ("13");
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}else if(result.equals("11")){
builder.setMessage("已经录制完毕!");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}else{
}
}
@Override
public void OnAnyChatTransBufferEx(int dwUserid, byte[] lpBuf, int dwLen,
int wparam, int lparam, int taskid) {
// TODO Auto-generated method stub
}
@Override
public void OnAnyChatSDKFilterData(byte[] lpBuf, int dwLen) {
// TODO Auto-generated method stub
}
public String setIpText() throws IOException{
FileInputStream inputStream = openFileInput("iptext.txt");
byte[] data = readFile(inputStream);
return new String(data);
}
public byte[] readFile(InputStream inputStream) throws IOException{
int len = 0;
byte[] buffer = new byte[1024];
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
while((len = inputStream.read(buffer))!=-1){
outputStream.write(buffer, 0, len);
}
outputStream.close();
return outputStream.toByteArray();
}
// 根据配置文件配置视频参数
private void ApplyVideoConfig()
{
ConfigEntity configEntity = ConfigService.LoadConfig(this);
if(configEntity.configMode == 1) // 自定义视频参数配置
{
// 设置本地视频编码的码率(如果码率为0,则表示使用质量优先模式)
AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_BITRATECTRL, configEntity.videoBitrate);
if(configEntity.videoBitrate==0)
{
// 设置本地视频编码的质量
AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_QUALITYCTRL, configEntity.videoQuality);
}
// 设置本地视频编码的帧率
AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_FPSCTRL, configEntity.videoFps);
// 设置本地视频编码的关键帧间隔
AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_GOPCTRL, configEntity.videoFps*4);
// 设置本地视频采集分辨率
AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_WIDTHCTRL, configEntity.resolution_width);
AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_HEIGHTCTRL, configEntity.resolution_height);
// 设置视频编码预设参数(值越大,编码质量越高,占用CPU资源也会越高)
AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_PRESETCTRL, configEntity.videoPreset);
}
// 让视频参数生效
AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_APPLYPARAM, configEntity.configMode);
// P2P设置
AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_NETWORK_P2PPOLITIC, configEntity.enableP2P);
// 本地视频Overlay模式设置
AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_OVERLAY, configEntity.videoOverlay);
// 回音消除设置
AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_AUDIO_ECHOCTRL, configEntity.enableAEC);
// 平台硬件编码设置
AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_CORESDK_USEHWCODEC, configEntity.useHWCodec);
// 视频旋转模式设置
AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_ROTATECTRL, configEntity.videorotatemode);
// 视频平滑播放模式设置
AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_STREAM_SMOOTHPLAYMODE, configEntity.smoothPlayMode);
// 视频采集驱动设置
AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_CAPDRIVER, configEntity.videoCapDriver);
// 本地视频采集偏色修正设置
AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_FIXCOLORDEVIA, configEntity.fixcolordeviation);
// 视频显示驱动设置
AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_VIDEOSHOW_DRIVERCTRL, configEntity.videoShowDriver);
// 音频播放驱动设置
AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_AUDIO_PLAYDRVCTRL, configEntity.audioPlayDriver);
// 音频采集驱动设置
AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_AUDIO_RECORDDRVCTRL, configEntity.audioRecordDriver);
// 视频GPU渲染设置
AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_VIDEOSHOW_GPUDIRECTRENDER, configEntity.videoShowGPURender);
}
}
|
|