一人离去violet 2016-07-21 08:28 采纳率: 0%
浏览 1791

Java微信开发上传下载语音怎么获取amr语音的秒数,传给APP端

微信端发语音,微信显示语音的秒数,微信接收到语音也能显示秒数,怎么获得这个秒数?
查了很多资料例子 amr获取时长的例子,运行得到的与真实的秒数不符合:
用的方法是下面的:
public static 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++;
        }

        duration += frameCount * 20;// 帧数*20
    } finally {
        if (randomAccessFile != null) {
            randomAccessFile.close();
        }
    }
    return (int)((duration/1000)+1);
}
  • 写回答

1条回答 默认 最新

  • zqbnqsdsmd 2017-03-05 15:47
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable