zoukankan      html  css  js  c++  java
  • Java获取音频播放时长

    1. 获取.amr音频文件播放时长

    import java.io.File;
    import java.io.IOException;
    import java.io.RandomAccessFile;
     
    public class AmrFileTimeLengthSample {
    	public static long 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 duration;
        }
    }

    2. 获取.mp3,.wav,.wma音频文件播放时长

    
    import it.sauronsoftware.jave.Encoder;
    import it.sauronsoftware.jave.MultimediaInfo;
    import java.io.File;
    
    .................................
    
        File source = new File(fileUrl);
        Encoder encoder = new Encoder();
        MultimediaInfo m = encoder.getInfo(source);
        long duration = m.getDuration();
    苟利国家生死以, 岂因祸福避趋之
  • 相关阅读:
    APPlication,Session和Cookie的区别
    C# 中的Request对象的应用
    从字符串里提取一个列表(数组)
    UDP:用户数据报协议
    反射
    网络编程
    多线程
    final,finally和finalize的区别
    集合
    StringBuffer
  • 原文地址:https://www.cnblogs.com/chintsai/p/11829195.html
Copyright © 2011-2022 走看看