// Mit dem folgenden Code kann man die Informationen 
// einer MP3-Datei lesen und schreiben. Dazu setzt man auf 
// ein Formular zwei TButton und sieben TEdit. 
// Beim schreiben kann man das Genre als Zahl (Edit 6) 
// oder als Wort (Edit7) angeben, wobei dann die Schreibweise 
// beachtet werden muss. 

// Getestet mit D4 unter NT4 

unit mp3unit; 
 
interface 
 
uses 
  Windows, 
  SysUtils, 
  Classes, 
  Controls, 
  Forms, 
  Dialogs, 
  StdCtrls; 
 
type 
  TForm1 = class(TForm) 
    Edit1: TEdit; 
    Edit2: TEdit; 
    Edit3: TEdit; 
    Edit4: TEdit; 
    Edit5: TEdit; 
    Edit6: TEdit; 
    Edit7: TEdit; 
    Button1: TButton; 
    Button2: TButton; 
    procedure FormCreate(Sender: TObject); 
    procedure Button1Click(Sender: TObject); 
    procedure Button2Click(Sender: TObject); 
  private 
    { Private-Deklarationen} 
  public 
    function Lies_ID3_Tag(Datei: string): boolean; 
    function Schreib_ID3_Tag(Datei: string): boolean; 
  end; 
 
var 
  Form1: TForm1; 
 
implementation 
 
{$R *.DFM} 
 
type 
  ID3_Rec = packed record 
    Tag: array[0..2] of Char; 
    Titel, Kuenstler, Kommentar, Album: array[0..29] of Char; 
    Jahr: array[0..3] of Char; 
    Genre: Byte; 
  end; 
 
const 
  ID3_Genre: array[0..147] of string = ('Blues', 'Classic Rock', 'Country', 
    'Dance', 'Disco', 'Funk', 'Grunge', 'Hip-Hop', 'Jazz', 'Metal', 'New Age', 
    'Oldies', 'Other', 'Pop', 'R&B', 'Rap', 'Reggae', 'Rock', 'Techno', 
    'Industrial', 'Alternative', 'Ska', 'Death Metal', 'Pranks', 'Soundtrack', 
    'Euro-Techno', 'Ambient', 'Trip-Hop', 'Vocal', 'Jazz+Funk', 'Fusion', 
    'Trance', 'Classical', 'Instrumental', 'Acid', 'House', 'Game', 
    'Sound Clip', 'Gospel', 'Noise', 'AlternRock', 'Bass', 'Soul', 'Punk', 
    'Space', 'MEditative', 'Instrumental Pop', 'Instrumental Rock', 'Ethnic', 
    'Gothic', 'Darkwave', 'Techno-Industrial', 'Electronic', 'Pop-Folk', 
    'Eurodance', 'Dream', 'Southern Rock', 'Comedy', 'Cult', 'Gangsta', 
    'Top 40', 'Christian Rap', 'Pop/Funk', 'Jungle', 'Native American', 
    'Cabaret', 'New Wave', 'Psychadelic', 'Rave', 'Showtunes', 'Trailer', 
    'Lo-Fi', 'Tribal', 'Acid Punk', 'Acid Jazz', 'Polka', 'Retro', 
    'Musical', 'Rock & Roll', 'Hard Rock', 'Folk', 'Folk-Rock', 
    'National Folk', 'Swing', 'Fast Fusion', 'Bebob', 'Latin', 'Revival', 
    'Celtic', 'Bluegrass', 'Avantgarde', 'Gothic Rock', 'Progressive Rock', 
    'Psychedelic Rock', 'Symphonic Rock', 'Slow Rock', 'Big Band', 
    'Chorus', 'Easy Listening', 'Acoustic', 'Humour', 'Speech', 'Chanson', 
    'Opera', 'Chamber Music', 'Sonata', 'Symphony', 'Booty Bass', 'Primus', 
    'Porn Groove', 'Satire', 'Slow Jam', 'Club', 'Tango', 'Samba', 
    'Folklore', 'Ballad', 'Power Ballad', 'Rhythmic Soul', 'Freestyle', 
    'Duet', 'Punk Rock', 'Drum Solo', 'Acapella', 'Euro-House', 'Dance Hall', 
    'Goa', 'Drum & Bass', 'Club-House', 'Hardcore', 'Terror', 'Indie', 
    'BritPop', 'Negerpunk', 'Polsk Punk', 'Beat', 'Christian Gangsta Rap', 
    'Heavy Metal', 'Black Metal', 'Crossover', 'Contemporary Christian', 
    'Christian Rock', 'Merengue', 'Salsa', 'Trash Metal', 'Anime', 'Jpop', 
    'Synthpop'); 
 
procedure TForm1.FormCreate(Sender: TObject); 
begin 
  Edit1.MaxLength := 30; 
  Edit2.MaxLength := 30; 
  Edit3.MaxLength := 30; 
  Edit4.MaxLength := 30; 
  Edit5.MaxLength := 4; 
  Edit6.maxlength := 3; 
  Edit7.maxlength := 0; 
end; 
 
function TForm1.Lies_ID3_Tag(Datei: string): boolean; 
var 
  tfs: TFileStream; 
  tg: ID3_rec; 
  lg: integer; 
begin 
  result := false; 
  Edit1.Text := ''; 
  Edit2.Text := ''; 
  Edit3.Text := ''; 
  Edit4.Text := ''; 
  Edit5.Text := ''; 
  Edit6.text := ''; 
  Edit7.text := ''; 
  if (not FileExists(Datei)) 
    or (AnsiLowerCase(ExtractFileExt(Datei)) <> '.mp3') 
    then exit; 
  lg := SizeOf(tg); 
  tfs := TFileStream.Create(Datei, fmOpenRead); 
  try 
    tfs.position := tfs.size - lg; 
    tfs.Read(tg, lg); 
  except 
    tfs.free; 
    exit; 
  end; 
  try 
    if string(tg.Tag) = 'TAG' then begin 
      Edit1.Text := tg.Titel; 
      Edit2.Text := tg.Kuenstler; 
      Edit3.Text := tg.Kommentar; 
      Edit4.Text := tg.Album; 
      Edit5.Text := tg.Jahr; 
      Edit6.Text := IntToStr(tg.Genre); 
      if tg.Genre < 148 then Edit7.Text := ID3_Genre[tg.Genre]; 
    end; 
    result := true; 
  except end; 
  tfs.free; 
end; 
 
function TForm1.Schreib_ID3_Tag(Datei: string): boolean; 
var 
  tfs: TFileStream; 
  tg: ID3_rec; 
  x: Byte; 
  s: string; 
  lg: integer; 
  tst: array[0..2] of Char; 
begin 
  result := false; 
  if (not FileExists(Datei)) 
    or (AnsiLowerCase(ExtractFileExt(Datei)) <> '.mp3') 
  then exit; 
  lg := SizeOf(tg); 
  tfs := TFileStream.Create(Datei, fmOpenReadWrite); 
  try 
    tfs.position := tfs.size - lg; 
    tfs.ReadBuffer(tst, 3); 
  except 
    tfs.free; 
    exit; 
  end; 
  s := 'TAG'; 
  fillchar(tg, lg, #32); 
  try 
    move(Edit1.Text[1], tg.Titel, length(Edit1.Text)); 
    move(Edit2.Text[1], tg.Kuenstler, length(Edit2.Text)); 
    move(Edit3.Text[1], tg.Kommentar, length(Edit3.Text)); 
    move(Edit4.Text[1], tg.Album, length(Edit4.Text)); 
    move(Edit5.Text[1], tg.Jahr, length(Edit5.Text)); 
    move(s[1], tg.Tag, 3); 
    tg.Genre := 255; 
    if Trim(Edit6.Text) <> '' then begin 
      try 
        x := StrToInt(Trim(Edit6.Text)); 
        tg.Genre := x; 
        if x < 148 then Edit7.Text := ID3_Genre[x] else 
          Edit7.Text := ''; 
      except end; 
    end else 
      for x := 0 to high(ID3_Genre) do 
        if AnsiLowerCase(Trim(Edit7.text)) = AnsiLowerCase(ID3_Genre[x]) 
          then begin 
          tg.Genre := x; 
          Edit6.Text := IntToStr(x); 
          Edit7.Text := ID3_Genre[x]; 
          break; 
        end; 
    if tg.Genre = 255 then begin 
      Edit6.Text := '255'; 
      Edit7.Text := ''; 
    end; 
    if string(tst) <> s then 
      tfs.position := tfs.size 
    else 
      tfs.position := tfs.size - lg; 
    tfs.Write(tg, lg); 
    result := true; 
  except end; 
  tfs.free; 
end; 
 
// Beispielaufrufe 
 
procedure TForm1.Button1Click(Sender: TObject); 
begin 
  if not Lies_ID3_Tag('c:\flaschebier.mp3') 
    then showmessage('MP3-Tag konnte nicht gelesen werden.'); 
end; 
 
procedure TForm1.Button2Click(Sender: TObject); 
begin 
  if not Schreib_ID3_Tag('c:\flaschebier.mp3') 
    then showmessage('MP3-Tag konnte nicht geschrieben werden.'); 
end; 
 
end.


 

Zugriffe seit 6.9.2001 auf Delphi-Ecke