|
现在我想要做一个音频聊天的功能,两端接通后,有时听到的语音有时听不到语音。这什么情况呢?下面是语音聊天的界面代码。
package com.bairuitech.callcenter;
import com.bairuitech.anychat.AnyChatBaseEvent;
import com.bairuitech.anychat.AnyChatCoreSDK;
import com.bairuitech.anychat.AnyChatDefine;
import com.bairuitech.anychat.AnyChatVideoCallEvent;
import com.yunmai.entcc.R;
import com.yunmai.imdemo.controller.voicechat.VoiceChatController;
import com.yunmai.imdemo.controller.voicechat.VoiceChatListener;
import com.yunmai.imdemo.controller.webrtc.RingtoneController;
import com.yunmai.imdemo.ui.ChatActivity;
import com.yunmai.imdemo.util.HandlerUnit;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
/**
* 单人语音聊天
* @author Administrator
*
*/
public class SingleVoiceChatActivity extends Activity implements OnClickListener, AnyChatBaseEvent, AnyChatVideoCallEvent,VoiceChatListener{
private TextView voiceChatInfo;
private Button handUp;
public AnyChatCoreSDK anychat;
int userID;// 对方视频id
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_voice_chat);
VoiceChatController.listener = this;
initView();
setListener();
Intent intent = getIntent();
userID = intent.getIntExtra("UserID", -1);
InitialSDK();
RingtoneController.stopRingTone();
}
private void InitialSDK() {
anychat = new AnyChatCoreSDK();
anychat.SetBaseEvent(this);
// 启动AnyChat传感器监听
anychat.mSensorHelper.InitSensor(this);
// 初始化Camera上下文句柄
AnyChatCoreSDK.mCameraHelper.SetContext(this);
anychat.SetVideoCallEvent(this);
}
private void initView() {
voiceChatInfo = (TextView) findViewById(R.id.voice_chat_info);
handUp = (Button) findViewById(R.id.btn_handup);
voiceChatInfo.setText("与 "+VoiceChatController.instance.getUserName()
+ "("
+ VoiceChatController.instance.getUser().substring(0,
VoiceChatController.instance.getUser().indexOf("#"))
+ ")"+" 语音中");
}
private void setListener() {
handUp.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_handup:
anychat.VideoCallControl(AnyChatDefine.BRAC_VIDEOCALL_EVENT_FINISH,userID, 0, 0, 0,"");
if (HandlerUnit.getMessageHandler() != null) {
HandlerUnit
.getMessageHandler()
.obtainMessage(
ChatActivity.HANDLER_REFRESH_IMMSGLIST)
.sendToTarget();
}
onVoiceChatRefuse();
break;
}
}
@Override
protected void onResume() {
super.onResume();
reflashVoice();
}
@Override
protected void onPause() {
super.onPause();
anychat.UserSpeakControl(-1, 0);
anychat.UserSpeakControl(userID, 0);
}
@Override
protected void onRestart() {
super.onRestart();
reflashVoice();
}
private void reflashVoice() {
anychat.UserSpeakControl(userID, 1);
anychat.UserSpeakControl(-1, 1);
}
private void finishActivits() {
if(VoiceChatConnectingActivity.voiceChatConnectingActivity != null){
VoiceChatConnectingActivity.voiceChatConnectingActivity.finish();
}
this.finish();
}
@Override
public void OnAnyChatConnectMessage(boolean bSuccess) {
}
@Override
public void OnAnyChatLoginMessage(int dwUserId, int dwErrorCode) {
}
@Override
public void OnAnyChatEnterRoomMessage(int dwRoomId, int dwErrorCode) {
}
@Override
public void OnAnyChatOnlineUserMessage(int dwUserNum, int dwRoomId) {
}
@Override
public void OnAnyChatUserAtRoomMessage(int dwUserId, boolean bEnter) {
}
@Override
public void OnAnyChatLinkCloseMessage(int dwErrorCode) {
anychat.UserSpeakControl(userID, 0);
anychat.UserSpeakControl(-1, 0);
Toast.makeText(SingleVoiceChatActivity.this, "视频连接中断,请确定网络是否正常。", 1).show();
}
@Override
public void onVoiceChatAsk() {
}
@Override
public void onVoiceChatAgree() {
}
@Override
public void onVoiceChatRefuse() {
if (HandlerUnit.getMessageHandler() != null) {
HandlerUnit.getMessageHandler()
.obtainMessage(ChatActivity.HANDLER_REFRESH_IMMSGLIST)
.sendToTarget();
}
finishActivits();
}
@Override
public void onTimeOut() {
}
@Override
public void onVersionLow() {
// TODO Auto-generated method stub
}
@Override
public void OnAnyChatVideoCallEvent(int dwEventType, int dwUserId,
int dwErrorCode, int dwFlags, int dwParam, String userStr) {
Log.w("VideoActivity", dwEventType + "");
switch (dwEventType) {
case AnyChatDefine.BRAC_VIDEOCALL_EVENT_FINISH:
Log.e("BRAC_VIDEOCALL_EVENT_FINISH", dwEventType + "");
onVoiceChatRefuse();
anychat.VideoCallControl(AnyChatDefine.BRAC_VIDEOCALL_EVENT_FINISH,dwUserId, 0, 0, 0,"");
break;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
onVoiceChatRefuse();
anychat.VideoCallControl(AnyChatDefine.BRAC_VIDEOCALL_EVENT_FINISH,userID, 0, 0, 0,"");
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onDestroy() {
super.onDestroy();
if(anychat != null){
anychat.Logout();
anychat.Release();
anychat.mSensorHelper.DestroySensor();
}
}
}
|
|