> 监听图片发送进度,可获取百分比
NIMClient.getService(MsgServiceObserve.class).observeAttachmentProgress(new Observer<AttachmentProgress>() {
@Override
public void onEvent(AttachmentProgress attachmentProgress) {
long total = attachmentProgress.getTotal();
long transferred = attachmentProgress.getTransferred();
int percentInt = NumberUtils.percentInt(attachmentProgress.getTotal(), attachmentProgress.getTransferred());
}
}, true);
接收消息
NIMClient.getService(MsgServiceObserve.class).observeReceiveMessage(new Observer<List<IMMessage>>() {
@Override
public void onEvent(List<IMMessage> imMessages) {
toChat(imMessages);
}
}, true);
监听消息发送进度
NIMClient.getService(MsgServiceObserve.class).observeMsgStatus(new Observer<IMMessage>() {
@Override
public void onEvent(IMMessage imMessage) {
switch (imMessage.getAttachStatus()){
case transferring://发送中
case transferred://发送完成
case def://默认状态,未开始
case fail://发送失败
}
}
}, true);
发文字
//发文字
private void senMsg(String msg) {
IMMessage imMessage = MessageBuilder.createTextMessage(toId, SessionTypeEnum.P2P, msg);
imMessage.setRemoteExtension(getHeaders());
NIMClient.getService(MsgService.class).sendMessage(imMessage, false);
}
发图片
//发图片
private void sendPic(String path) {
File file = new File(path);
IMMessage imMessage = MessageBuilder.createImageMessage(toId, SessionTypeEnum.P2P, file, file.getName());
imMessage.setAttachStatus(AttachStatusEnum.transferring);
imMessage.setRemoteExtension(getHeaders());
NIMClient.getService(MsgService.class).sendMessage(imMessage, false);
}
发语音
private void sendVoice() {
File file = new File(voicePath);
IMMessage imMessage = MessageBuilder.createAudioMessage(toId, SessionTypeEnum.P2P, file, voiceTime);
imMessage.setRemoteExtension(getHeaders());
NIMClient.getService(MsgService.class).sendMessage(imMessage, false);
}
消息置为已读
//接收消息状态全为sucess,语音默认sucess为未读,播放读取后,可手动置为read状态,更新到消息记录
chat.setStatus(MsgStatusEnum.read);
NIMClient.getService(MsgService.class).updateIMMessageStatus(chat); //置为已读
获取托管信息
NimUserInfo userInfo = NIMClient.getService(UserService.class).getUserInfo(sessionId);
if (userInfo == null) {
ArrayList<String> accounts = new ArrayList<>();
accounts.add(sessionId);
NIMClient.getService(UserService.class).fetchUserInfo(accounts).setCallback(new RequestCallback<List<NimUserInfo>>() {
@Override
public void onSuccess(List<NimUserInfo> nimUserInfos) {
userInfo = nimUserInfos.get(0);
}
@Override
public void onFailed(int i) {
}
@Override
public void onException(Throwable throwable) {
}
});
}
会话列表中移除指定会话
- 需要传入一个最近联系人参数,android没有ios直接创建会话来获取联系的方法。①可以通话发送本地消息来生成一个会话,然后本地数据库删除消息(比较麻烦,性能好)②根据好友来循环遍历最近会话列表(较省事,性能消耗)
NIMClient.getService(MsgService.class).deleteRecentContact(RecentContact recent);
拨打音频
常见状态码
- -1:未通话时调用挂断方法,或者多次调用挂断方法
- 9:正忙,一般上一次通话还未结束
- 1000:登录成功之前,调用SDK相关接口
- 11001:对方不在线
- 9102 :通道失败
/**
* 主叫方拨打音视频
*/
private void outGoingCalling(String account, final AVChatType callTypeEnum) {
AVChatNotifyOption notifyOption = new AVChatNotifyOption();
//附加字段
notifyOption.extendMessage = "extra_data";
notifyOption.forceKeepCalling = false;
//打开Rtc模块
AVChatManager.getInstance().enableRtc();
//如果是视频,打开相关操作
if (isVideo) {
smalRender.setVisibility(View.VISIBLE);
bigRender.setVisibility(View.VISIBLE);
//打开视频模块
AVChatManager.getInstance().enableVideo();
//创建视频采集模块并且设置到系统中
avChatCameraCapturer = AVChatVideoCapturerFactory.createCameraCapturer();
AVChatManager.getInstance().setupVideoCapturer(avChatCameraCapturer);
//开始视频预览
AVChatManager.getInstance().startVideoPreview();
//设置本地显示
AVChatManager.getInstance().setupLocalVideoRender(bigRender, false, 1);
}
//设置铃声
player.play(AVChatSoundPlayer.RingerTypeEnum.CONNECTING);
//开始呼叫
AVChatManager.getInstance().call2(account, callTypeEnum, notifyOption, new AVChatCallback<AVChatData>() {
@Override
public void onSuccess(AVChatData data) {
// ToastUtils.Toast(ChatCallActivity.this, "发起会话成功");
AVChatProfile.getInstance().setAVChatting(true);
//设置铃声
player.play(AVChatSoundPlayer.RingerTypeEnum.RING);
avChatData = data;
//发起会话成功
}
@Override
public void onFailed(int code) {//11001 通话不可达,对方离线状态 9103 已经在他端对这个呼叫响应过了 9102 通道失效
switch (code) {
case 11001:
ToastUtils.showToast("对方不在线...");
break;
case 9:
ToastUtils.showToast("对象正忙...");
callEndFinish();//结束停止呼叫状态
break;
default:
ToastUtils.showToast("发起会话异常...");
callEndFinish();
break;
}
}
@Override
public void onException(Throwable exception) {
ToastUtils.showToast("发起会话异常...");
callEndFinish();
}
});
}