Чтение информации из МР3-файла

Как из МРЗ-файла, выбранного в OpenDialog прочитать имя исполнителя и название песни?

code: #delphi
  1. {  
  2.   Byte 1-3 = ID 'TAG'  
  3.   Byte 4-33 = Titel / Title  
  4.   Byte 34-63 = Artist  
  5.   Byte 64-93 = Album  
  6.   Byte 94-97 = Jahr / Year  
  7.   Byte 98-127 = Kommentar / Comment  
  8.   Byte 128 = Genre  
  9. }
  10. type
  11.   TID3Tag = record
  12.     ID: string[3];
  13.     Titel: string[30];
  14.     Artist: string[30];
  15.     Album: string[30];
  16.     Year: string[4];
  17.     Comment: string[30];
  18.     Genre: Byte;
  19.   end;
  20. const
  21.  Genres : array[0..146] of string =
  22.     ('Blues','Classic Rock','Country','Dance','Disco','Funk','Grunge',
  23.     'Hip- Hop','Jazz','Metal','New Age','Oldies','Other','Pop','R&B',
  24.     'Rap','Reggae','Rock','Techno','Industrial','Alternative','Ska',
  25.     'Death Metal','Pranks','Soundtrack','Euro-Techno','Ambient',
  26.     'Trip-Hop','Vocal','Jazz+Funk','Fusion','Trance','Classical',
  27.     'Instrumental','Acid','House','Game','Sound Clip','Gospel','Noise',  
  28.     'Alternative Rock','Bass','Punk','Space','Meditative','Instrumental Pop',
  29.     'Instrumental Rock','Ethnic','Gothic','Darkwave','Techno-Industrial','Electronic',
  30.     'Pop-Folk','Eurodance','Dream','Southern Rock','Comedy','Cult','Gangsta',
  31.     'Top 40','Christian Rap','Pop/Funk','Jungle','Native US','Cabaret','New Wave',
  32.     'Psychadelic','Rave','Showtunes','Trailer','Lo-Fi','Tribal','Acid Punk',
  33.     'Acid Jazz','Polka','Retro','Musical','Rock & Roll','Hard Rock','Folk',
  34.     'Folk-Rock','National Folk','Swing','Fast Fusion','Bebob','Latin','Revival',
  35.     'Celtic','Bluegrass','Avantgarde','Gothic Rock','Progressive Rock',
  36.     'Psychedelic Rock','Symphonic Rock','Slow Rock','Big Band','Chorus',
  37.     'Easy Listening','Acoustic','Humour','Speech','Chanson','Opera',
  38.     'Chamber Music','Sonata','Symphony','Booty Bass','Primus','Porn Groove',
  39.     'Satire','Slow Jam','Club','Tango','Samba','Folklore','Ballad',
  40.     'Power Ballad','Rhytmic Soul','Freestyle','Duet','Punk Rock','Drum Solo',  
  41.     'Acapella','Euro-House','Dance Hall','Goa','Drum & Bass','Club-House',
  42.     'Hardcore','Terror','Indie','BritPop','Negerpunk','Polsk Punk','Beat',  
  43.     'Christian Gangsta','Heavy Metal','Black Metal','Crossover','Contemporary C',
  44.     'Christian Rock','Merengue','Salsa','Thrash Metal','Anime','JPop','SynthPop');  
  45.  
  46. var
  47.   Form1: TForm1;
  48. implementation
  49. {$R *.dfm}
  50. function readID3Tag(FileName: string): TID3Tag;  
  51. var
  52.   FS: TFileStream;  
  53.   Buffer: array [1..128] of Char;
  54. begin  
  55.   FS := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
  56.   try  
  57.     FS.Seek(-128, soFromEnd);
  58.     FS.Read(Buffer, 128);  
  59.     with Result do
  60.     begin  
  61.       ID := Copy(Buffer, 1, 3);
  62.       Titel := Copy(Buffer, 4, 30);  
  63.       Artist := Copy(Buffer, 34, 30);
  64.       Album := Copy(Buffer, 64, 30);  
  65.       Year := Copy(Buffer, 94, 4);
  66.       Comment := Copy(Buffer, 98, 30);  
  67.       Genre := Ord(Buffer[128]);
  68.     end;  
  69.   finally
  70.     FS.Free;  
  71.   end;
  72. end;  
  73.  
  74. procedure TForm1.Button1Click(Sender: TObject);
  75. begin
  76.  if OpenDialog1.Execute then
  77.   begin  
  78.     with readID3Tag(OpenDialog1.FileName) do
  79.     begin
  80.       LlbID.Caption := 'ID: ' + ID;
  81.       LlbTitel.Caption := 'Titel: ' + Titel;
  82.       LlbArtist.Caption := 'Artist: ' + Artist;
  83.       LlbAlbum.Caption := 'Album: ' + Album;
  84.       LlbYear.Caption := 'Year: ' + Year;
  85.       LlbComment.Caption := 'Comment: ' + Comment;
  86.       if (Genre >= 0) and (Genre <=146) then
  87.        LlbGenre.Caption := 'Genre: ' + Genres[Genre]
  88.       else
  89.        LlbGenre.Caption := 'N/A';
  90.     end;
  91.   end;
  92. end;
Поделиться:

Похожие статьи: