zoukankan      html  css  js  c++  java
  • Java 获取amr音频格式的音频长度

    import java.io.File;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    
    public class GetAmrDuration {
    
        /**
         * 得到amr的时长
         * 
         * @param file
         * @return
         * @throws IOException
         */
        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);
        }
        
        public static void main(String[] args) throws IOException {
            File source = new File("6.amr");
            System.out.println(getAmrDuration(source));
        }
    
    }
  • 相关阅读:
    Nmap帮助文档解释
    用servlet设置过滤器处理中文乱码
    Linux服务器远程连接window服务器并执行cmd命令
    java中的异常处理
    java的反射机制
    react入门
    多线程编程
    软件工程基本概念
    反射、类加载与垃圾回收
    数据库
  • 原文地址:https://www.cnblogs.com/wuyou/p/3736599.html
Copyright © 2011-2022 走看看