回复人: drugon(personal) ( ) 信誉:100 格式的话,官方的idv3.2是有详细信息的,问题是有很多mp3没有idv3.2这个头,很乱啊。搞的现在我怀疑当初从格式下手是否正确了谢谢大家帮我up啊,期待知道的大哥出现

解决方案 »

  1.   

    我是这样想的~
    我没有研究过MP3的格式,但是我想信息中至少有采样率吧,然后文件大小已知,是不是就可以通过计算算出时间了。
    但是我觉得,WINDOWS中的属性中也可以看到时间,应该在文件中有记录三~
    -------------------------------------
    小弟发言在于抛砖引玉,其中的不当之处还望各位兄弟指出
      

  2.   

    WINDOWS中的属性中也可以看到时间,应该在文件中有记录三~
    对的对的……这也是我疑惑之处 难道还有什么其他地方记录了资料我不知道还是说有什么算法算出来的呢?
      

  3.   

    winamp是不是通过计算.mp3文件长度和当前计算机的速度大致计算出播放时长的呢?
      

  4.   

    突然发现要修改得比较好还是比较费时间的。找到一个讲得不错的地方,大家可以去看看。
    http://blog.csdn.net/airhand/articles/60005.aspx
      

  5.   

    package mp3format;import java.io.*;public class Mp3Reader {
      RandomAccessFile reader = null;
      File file = null;
      String header = null;
      byte version = 0;
      byte revision = 0;
      byte flag = 0;
      long tagSize = 0;  int mpgVersion = 0;
      int layerDescription = 0;
      int protection = 0;
      int bitrate = 0;
      int samplingRate = 0;
      int padding = 0;
      int privateBit = 0;
      int channelMode = 0;
      int modeExtension = 0;
      int copyright = 0;
      int original = 0;
      int emphasis = 0;  int[][] samplingRateIndex = {
          {
          11025, 12000, 8000, 0}
          , {
          0, 0, 0, 0}
          , {
          22050, 24000, 16000, 0}
          , {
          44100, 48000, 32000, 0}
      }; //[MPEG Audio version][bit]  int[][] bitrateIndex = {
          {}
          , {
          0, 8, 16, 24, 32, 64, 80, 56, 64, 128, 160, 112, 128, 256, 320, 0}
          , {
          0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0}
          , {
          0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0}
          , {}
          , {
          0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0}
          , {
          0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0}
          , {
          0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0}
      }; //[调用getBitrateIndex算出索引值][bit]  public Mp3Reader(File file) {
        this.file = file;
        try {
          reader = new RandomAccessFile(file, "r");
          try {
            byte[] buffer = new byte[3];
            reader.read(buffer);
            header = new String(buffer);
            version = reader.readByte();
            revision = reader.readByte();
            flag = reader.readByte();
            byte[] len = new byte[4];
            reader.read(len);
            tagSize = (len[0] & 0x7F) * 0x200000 + (len[1] & 0x7F) * 0x400 +
                (len[2] & 0x7F) * 0x80 + (len[3] & 0x7F);
          }
          catch (IOException ex1) {
          }
          try {
            byte[] frameInfo = new byte[4];
            reader.seek(reader.getFilePointer() + tagSize);
            reader.read(frameInfo);
            this.mpgVersion = (frameInfo[1] & 0x18) >> 3;
            this.layerDescription = (frameInfo[1] & 0x06) >> 1;
            this.protection = frameInfo[1] & 0x01;
            this.bitrate = (frameInfo[2] & 0xf0) >> 4;
            this.samplingRate = (frameInfo[2] & 0x0c) >> 2;
            this.padding = (frameInfo[2] & 0x02) >> 1;
            this.privateBit = frameInfo[2] & 0x01;
            this.channelMode = (frameInfo[3] & 0xc0) >> 6;
            this.modeExtension = (frameInfo[3] & 0x30) >> 4;
            this.copyright = (frameInfo[3] & 0x08) >> 3;
            this.original = (frameInfo[3] & 0x04) >> 2;
            this.emphasis = frameInfo[3] & 0x03;
          }
          catch (IOException ex2) {
          }    }
        catch (FileNotFoundException ex) {
        }
      }  /**
       * getHeader
       */
      public String getHeader() {
        return this.header;
      }  public long getTagSize() {
        return this.tagSize;
      }  private int getBitrateIndex() {
        return this.layerDescription + 4 * (this.mpgVersion % 2);
      }  public long getTime() {
        //每帧的长度
        long frameLength = ( ( (this.mpgVersion == 3 ? 144 : 72) *
                       this.bitrateIndex[this.getBitrateIndex()][this.bitrate]) *
                     1000 /
                     this.samplingRateIndex[this.mpgVersion][this.samplingRate]) +
            this.padding;
        long fileLength = this.file.length();
        //去头尾的标签
        long lengthOfAllFrames = fileLength-10-this.tagSize-128;
        long time = (lengthOfAllFrames/frameLength)*26;//每帧固定为26ms
        return time;
      }  public static void main(String[] args) {
        File file = new File("test.mp3");
        Mp3Reader mp3 = new Mp3Reader(file);
        long timeMS = mp3.getTime();
        int timeS = (int)timeMS/1000;
        int minite = timeS /60;
        int second = timeS % 60;
        String time = minite+":"+(second<10 ? "0"+second : Integer.toString(second));
        System.out.println(time);
      }}关于各个成员变量参阅
    http://blog.csdn.net/airhand/articles/60005.aspx
    而且本类也有不少冗余的部分,想实现其它功能的。但是下班咯~
      

  6.   

    duer(duer) 好样地~!太谢谢了,我只想到解析头尾,没想到去解析数据帧,duer让我眼前一亮,duer有没兴趣一起探讨下,我已经实现了asf、rm的解析,加上mp3基本圆满了,一起聊留下联系方法哦 ~嘿嘿 我一会再结帖了