add duration of voice message.
authorTrillGates <williamtrillgates@gmail.com>
Tue, 6 Apr 2021 02:48:36 +0000 (10:48 +0800)
committerTrillGates <williamtrillgates@gmail.com>
Tue, 6 Apr 2021 02:48:36 +0000 (10:48 +0800)
app/src/main/java/com/enqualcomm/support/provider/FZDDatabaseHelper.java
app/src/main/java/com/enqualcomm/support/service/WeTalkMsgObserver.java
app/src/main/java/com/ic/protocols/protocol_fzd.java

index 59f34f3..3563016 100644 (file)
@@ -60,6 +60,7 @@ public class FZDDatabaseHelper extends SQLiteOpenHelper {
     public static final String mInOrOut = "in_or_out";
     public static final String mCategory = "category";
     public static final String mRemoteNum = "remote_num";
+    public static final String mDuration = "duration";
     public static final String mCreateTime = "create_time";
     public static final String mUpdateTime = "update_time";
 
@@ -98,6 +99,8 @@ public class FZDDatabaseHelper extends SQLiteOpenHelper {
                 " integer, " +
                 mInOrOut +
                 " integer, " +
+                mDuration +
+                " integer, " +
                 mRemoteNum +
                 " text, " +
                 mCreateTime +
index 362885c..cd04a6f 100644 (file)
@@ -39,11 +39,14 @@ public class WeTalkMsgObserver extends ContentObserver {
                     new String[]{lastPathSegment}, null, null);
             if (query != null && query.moveToNext()) {
                 String content = query.getString(query.getColumnIndex("content"));
+                //对方的ID,如果这个为null,那么则是群聊
+                //否则是单聊
+                String remoteNum = query.getString(query.getColumnIndex("remote_num"));
                 ICLogger.d("content --- > " + content);
                 query.close();
                 //交给fzd协议吧,让它去发送消息
                 protocol_fzd fzd = protocolAPIHelper.getProtocolfzd_handler();
-                fzd.sendVoice(content, lastPathSegment);
+                fzd.sendVoice(remoteNum, content, lastPathSegment);
             }
         });
     }
index eaa0e99..4b6c506 100644 (file)
@@ -34,6 +34,8 @@ import com.enqualcomm.support.utils.regexUtil;
 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;
@@ -70,10 +72,11 @@ public class protocol_fzd {
     /**
      * 发送语音消息,微聊的,注意转码
      *
-     * @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:直接删除数据库记录
@@ -101,27 +104,51 @@ public class protocol_fzd {
                 //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);
@@ -146,6 +173,49 @@ public class protocol_fzd {
     }
 
 
+    /**
+     * 获取声音时长,代码来源于网络,单位:秒
+     *
+     * @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 {
         /*终端发起*/
         //链路保持数据
@@ -502,6 +572,14 @@ public class protocol_fzd {
     }
 
     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();
         //路径
@@ -510,6 +588,8 @@ public class protocol_fzd {
         contentValues.put("type", type);
         //状态:0表示未读
         contentValues.put("state", 0);
+        //文件时长
+        contentValues.put("duration", amrDuration);
         //接受还是发送,1表示来信
         contentValues.put("in_or_out", 1);
         //对方号码是没有的