从MP3文件中获得ID3-Tag

解决方案 »

  1.   

    每一个MP3文件包含有一个ID3-Tag头, 它用来提供艺术家、标题、专辑、出版年和歌曲流派等信息。这个头总是128字节长并位于MP3文件末尾。ID3-Tag 结构是这样的: type 
    TID3Tag = packed record // 128 字节
       TAGID: array[0..2] of char; // 3 字节: 必须是TAG
       Title: array[0..29] of char; // 30 字节: 歌曲标题
       Artist: array[0..29] of char; // 30 字节: 歌曲的艺术家
       Album: array[0..29] of char; // 30 字节: 歌曲专辑
       Year: array[0..3] of char; // 4 字节: 出版年
       Comment: array[0..29] of char; // 30 字节: 评论
       Genre: byte; // 1 字节: 种类标识
    end; 为读取ID3-Tag信息并在一个对话框中显示,试试这个函数: procedure TForm1.Button1Click(Sender: TObject); 
    const
      _mp3file='G:\Mp3\Miscellaneous\ATC - Around The World.mp3'; 
    var
      id3tag: Tid3tag;
      mp3file: Tfilestream; 
    begin
      mp3file:=Tfilestream.create(_mp3file,fmOpenRead);
      try
        mp3file.position:=mp3file.size-128; // 跳到id3-tag
        mp3file.Read(id3tag,SizeOf(id3tag));
        showmessage(' Title: '+id3tag.title+#13+
                    ' Artist: '+id3tag.artist+#13+
                    ' Album: '+id3tag.album+#13+
                    ' Year: '+id3tag.year+#13+
                    ' Comment: '+id3tag.comment+#13+
                    ' Genre-ID: '+inttostr(id3tag.genre)
                    );
      finally
        mp3file.free;
      end; 
    end; 这个函数读取_mp3file描述的文件,跳到倒数第128个字节,读取并在对话框中显示信息。
      

  2.   

    type
      TID3Tag=Record
        ID:String[3];
       Title:String[30];
      Artist:String[30];
     Album:String[30];
       year:string[4];
      comment:string[30];
      Genre:byte;
    end;var
      ID3tag:TID3tag;
    procedure LESE_ID3Tag(fileName:String);
    var
      Buffer:array[1..128] of char;
      f:file;
    begin
      AssignFile(f,filename);
      Reset(f,1);
      seek(f,filesize(f)-128);
      BlockRead(f,Buffer,sizeof(buffer));
      closefile(f);
      with ID3tag do
       begin
        id:=copy(buffer,1,3);
        Title:=copy(Buffer,4,30);
        
       end