import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
/**
* 发送语音消息,微聊的,注意转码
*
- * @param content 声音地址
- * @param voiceId 数据库里的_id,用于更新状态
+ * @param remoteNum
+ * @param content 声音地址
+ * @param voiceId 数据库里的_id,用于更新状态
*/
- public void sendVoice(String content, String voiceId) {
+ public void sendVoice(String remoteNum, String content, String voiceId) {
File file = new File(content);
if (!file.exists()) {
//TODO:直接删除数据库记录
//fos.flush();
//fos.close();
//以上代码,验证成功,说明转换是对的
- //发送消息
+
+ //判断远程ID,remoteNum,如果这个不为空,那么是单聊
+ //如果remoteNum为空,那么是群聊
+ ICLogger.d("remote id == > " + remoteNum);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
- String cmd = cmdMap.get(CMD_TYPE.TK);
- int tkLength = cmd.length();
- String header = genHeader(tkLength, cmd);
- StringBuilder sb = new StringBuilder();
- if (doEncryption) {
- String keyHeader = keyheader(cmd, tkLength);
- sb.append(CMDHEADER).append(keyHeader).append(header);
+ if (TextUtils.isEmpty(remoteNum)) {
+ //发送消息
+ String cmd = cmdMap.get(CMD_TYPE.TK);
+ int tkLength = cmd.length();
+ String header = genHeader(tkLength, cmd);
+ StringBuilder sb = new StringBuilder();
+ if (doEncryption) {
+ String keyHeader = keyheader(cmd, tkLength);
+ sb.append(CMDHEADER).append(keyHeader).append(header);
+ } else {
+ sb.append(CMDHEADER).append(header);
+ }
+ //头部构造好了,添加逗号
+ sb.append(CMDSPLIT);
+ //转成byte
+ bos.write(sb.toString().getBytes());
+ //追加数据
+ bos.write(result);
+ //追加结尾
+ bos.write(CMDEND.getBytes());
+ mConnection.send(cmd, bos.toByteArray());
+ bos.reset();
+ bos.close();
} else {
- sb.append(CMDHEADER).append(header);
+ //单聊
+ String cmd = cmdMap.get(CMD_TYPE.TKOne);
+ int tkLength = cmd.length();
+ String header = genHeader(tkLength, cmd);
+ StringBuilder sb = new StringBuilder();
+ if (doEncryption) {
+ String keyHeader = keyheader(cmd, tkLength);
+ sb.append(CMDHEADER).append(keyHeader).append(header);
+ } else {
+ sb.append(CMDHEADER).append(header);
+ }
+ //头部构造好了,添加逗号
+ sb.append(CMDSPLIT);
+
}
- //头部构造好了,添加逗号
- sb.append(CMDSPLIT);
- //转成byte
- bos.write(sb.toString().getBytes());
- //追加数据
- bos.write(result);
- //追加结尾
- bos.write(CMDEND.getBytes());
- mConnection.send(cmd, bos.toByteArray());
+
//更新数据库状态
ContentValues contentValues = new ContentValues();
contentValues.put("state", 5);
}
+ /**
+ * 获取声音时长,代码来源于网络,单位:秒
+ *
+ * @param file
+ * @return
+ * @throws IOException
+ */
+ public int getAmrDuration(File file) throws IOException {
+ long duration = -1;
+ int[] packedSize = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0,
+ 0, 0};
+ RandomAccessFile randomAccessFile = null;
+ try {
+ randomAccessFile = new RandomAccessFile(file, "rw");
+ // 文件的长度
+ long length = file.length();
+ // 设置初始位置
+ int pos = 6;
+ // 初始帧数
+ int frameCount = 0;
+ int packedPos = -1;
+ // 初始数据值
+ byte[] datas = new byte[1];
+ while (pos <= length) {
+ randomAccessFile.seek(pos);
+ if (randomAccessFile.read(datas, 0, 1) != 1) {
+ duration = length > 0 ? ((length - 6) / 650) : 0;
+ break;
+ }
+ packedPos = (datas[0] >> 3) & 0x0F;
+ pos += packedSize[packedPos] + 1;
+ frameCount++;
+ }
+ // 帧数*20
+ duration += frameCount * 20;
+ } finally {
+ if (randomAccessFile != null) {
+ randomAccessFile.close();
+ }
+ }
+ return (int) ((duration / 1000) + 1);
+ }
+
public enum CMD_TYPE {
/*终端发起*/
//链路保持数据
}
private void voiceIn2Db(String amrFilepath, int type) {
+ //计算文件时长
+ int amrDuration = 0;
+ try {
+ File file = new File(amrFilepath);
+ amrDuration = getAmrDuration(file);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
ContentResolver resolver = mRuntime.getContextProxy().getResolver();
ContentValues contentValues = new ContentValues();
//路径
contentValues.put("type", type);
//状态:0表示未读
contentValues.put("state", 0);
+ //文件时长
+ contentValues.put("duration", amrDuration);
//接受还是发送,1表示来信
contentValues.put("in_or_out", 1);
//对方号码是没有的